List All MMM-YYYY Combination for Year
How do I list out such data in DB2?
FiscalMonthYear
----------
Dec-2010
Jan-2011
Feb开发者_StackOverflow社区-2011
..
Nov-2011
The very first MM-YYYY combination is always 'Dec' + '-' + CHAR(CURRENT YEAR - 1)
First off, I don't recommend joining on a (formatted) text column like this (especially something that is very language-dependant).
Generally, try joining month/years like this:
SELECT columnList
FROM tableA
JOIN tableB
ON tableB.numericMonth = MONTH(tableA.date)
AND tableB.numericYear = YEAR(tableA.date)
Although it should be noted that it would be preferred to have two date columns to join on, rather than separated columns like this.
Generating the (separated, numeric) range is fairly simple:
WITH date_range (month, year) as (SELECT 12, 2011
FROM sysibm/sysdummy1
UNION ALL
SELECT month - 1, 2012
FROM date_range
WHERE month > 1)
Although, hopefully you would really be joining (if possible) on an actual date, at which point the statement should look something like this:
SELECT columnList
FROM tableA
WHERE tableDate BETWEEN '2011-12-01' and '2012-11-30'
There is also a MONTHNAME()
function in DB2 which will return the (long) name of the month. Generates these results:
December
November
October
September
August
July
June
May
April
March
February
January
... Which you could substring to get the first 3 characters. Keep in mind that the value returned is language dependant (based on the system settings of the running job), so it's a terrible thing to use for a join column (which is why I don't recommend using month-name as a join condition - use month number).
精彩评论