Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 DATENAME returning wrong month

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.
Go to Top of Page

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

Go to Top of Page

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];


Go to Top of Page

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!
Go to Top of Page

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.
Go to Top of Page

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.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-11-09 : 14:05:31
Is this what you want?

select datename(month, dateadd(mm, 1, current_timestamp))

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

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!
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-11-09 : 14:26:42
quote:
Originally posted by tkizer

Is this what you want?

select datename(month, dateadd(mm, 1, current_timestamp))

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog



Sounds like it:

quote:
Originally posted by Lamprey

For example:
SELECT DATENAME(MONTH, DATEADD(MONTH, 1, Current_TimeStamp)) AS [Month];

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-11-09 : 14:42:26
Sorry Lamprey, I didn't see you'd posted it already. I only briefly scanned the thread when I posted it.



Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-11-09 : 14:46:18
quote:
Originally posted by tkizer

Sorry Lamprey, I didn't see you'd posted it already. I only briefly scanned the thread when I posted it.



Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

hehe, no worries.
Go to Top of Page
   

- Advertisement -