Search This Blog & Web

Thursday, July 30, 2009

Returning month name from Date

One of my colleage asked about getting month name and month value for date time value.

Here is the example to do that.

For Month,Year and Day value use following datetime functions.
Select DAY(GETDAET()); -- returns day part of date
Select MONTH(GETDAET()); -- returns year part of date
Select YAR(GETDAET()); -- returns year part of date

there is another function to do this
Select DATEPART(mm,GETDAET());

use
'mm', 'm' for month
'dd' for day
'y','yyyy' for year


For Month name value use following datetime functions.
Select DATENAME(mm,GETDAET()); -- returns month part of date like July

using custome code we can achive this using case statement

Select
case month(getdate())
when 1 then 'January'
when 2 then 'February'
when 3 then 'March'
when 4 then 'April'
when 5 then 'May'
when 6 then 'June'
when 7 then 'July'
when 8 then 'August'
when 9 then 'September'
when 10 then 'October'
when 11 then 'November'
when 12 then 'December'
End

No comments: