Author |
Topic |
dmilam
Posting Yak Master
185 Posts |
Posted - 2010-11-09 : 12:42:10
|
Wrong being undesired, but perhaps I'm overlooking something. Not sure why January is being returned here. Should be December. 12 is returned without DATENAME. Ideas?SELECT Current_TimeStamp;DECLARE @month INT, @year INT;SET @month = DATEPART(MONTH,Current_TimeStamp)+1; SET @year = DATEPART(YEAR,Current_TimeStamp); SELECT DATENAME(MONTH,@month) AS [Month];SELECT @year AS [Year]; |
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2010-11-09 : 13:01:25
|
it's not wrong. the 2nd paramater to the DATENAME function is a date, or an integer value corresponding to the # of days past the base date (19000101).you're passing a 12 -- 12 days past the base date. this will correspond to January. |
 |
|
Sachin.Nand
2937 Posts |
Posted - 2010-11-09 : 13:02:17
|
[code]SELECT Current_TimeStamp;DECLARE @month INT, @year INT;SET @month = DATEPART(MONTH,Current_TimeStamp)+1;SET @year = DATEPART(YEAR,Current_TimeStamp); SELECT DATENAME(mm,@month*30) AS [Month];SELECT @year AS [Year];[/code]PBUH |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-11-09 : 13:08:03
|
To add to what Russell said, if you look at BOL the syntax dor DATENAME is: DATENAME ( datepart , date )So you need to pass a DATE value.For example:SELECT DATENAME(MONTH, DATEADD(MONTH, 1, Current_TimeStamp)) AS [Month]; |
 |
|
dmilam
Posting Yak Master
185 Posts |
Posted - 2010-11-09 : 13:57:14
|
Interesting. I had tried this as well, which also yields January.select datename(month,(DATEPART(MONTH,Current_TimeStamp)+1)) Thanks, all! |
 |
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2010-11-09 : 14:03:05
|
again, you're passing a 12 as the second argument to the DateName() function. Pass a date. Or an integer corresponding to the number of days > basedate.in other words, you're passing January 13, 1900, and expecting the datename function to return December. |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-11-09 : 14:04:28
|
quote: Originally posted by dmilam Interesting. I had tried this as well, which also yields January.select datename(month,(DATEPART(MONTH,Current_TimeStamp)+1)) Thanks, all!
AS I stated, the DATENAME function is looking for a DATE. Which DATEPART(MONTH,Current_TimeStamp)+1 is not.Granted it can be converted to a date. |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
dmilam
Posting Yak Master
185 Posts |
Posted - 2010-11-09 : 14:22:33
|
quote: Originally posted by tkizer Is this what you want?select datename(month, dateadd(mm, 1, current_timestamp))
Yes, that'll do it. I had not looked at DATEADD returning a full date; I was fixed on the part.Thanks! |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
|
|