How can I select the month part of date value in SQL server?
I get this type of question a lot: How can I select the hours from the date in SQL? How can I get the day of the year from a date? How can I get the day of the month from a date? For all of these, the easiest solution would be to use SQL Server's built in date/time function: DatePart(). It allows you to select individual parts of a date:
- mm, -- mm: Select only the month
- ) AS birthday_month
It's a fairly straight forward function. You can of course do more than getting the months (as in the example). You can get all different parts of the date by specifying a different first argument:
year: yy, yyyy
quarter: qq, q
month: mm, m
dayofyear: dy, y
day: dd, d
week: wk, ww
minute: mi, n
second: ss, s
You can learn more about this function at the MSDN website.
Looking For A New Job?
- Lead Programmer 7+ Years Experience- ColdFusion/Expert Web Designer at AtPrime Media Services
- Senior Developer at Quality Bicycle Products
- ColdFusion Developer at WRIS Web Services
- Coldfusion Developer at Cavulus
- Web Developer at Townsend Communications, Inc.
Thanks i used it in my project.
Awesome. Glad to help.
Problem is, datePart is not recognized in CF Query of Queries. Any other simple way to get the same results? I need the day, month and year as seperate columns in my QofQ results.
Thanks for this, made use of this today :)
Not sure there is a nice way to do that in Query of Queries. You'd be better off performing that in the original query.
hey thanks for this.... helped a lot...
just 1 quick question.Though am using "mm", if the month is less than 10, i get "m" i.e "1" or "2" ..something like that.Just wondering if there is a way i can get "01" "02" ...a two digited stuff....
Thanks once again for your help
I am not sure off-hand. Typically, I'll do that level of formatting in the ColdFusion side of things.
This probably isn't the most elegant way to get a 2 digit formatted month in sql, but I've used something like this in the past. Convert the date to a string, then pull out the parts you need as a substring.
Select SubString(Convert(Char(10), GetDate(), 102), 6, 2)
Is there any help for someone trying to do this with a query of queries? I tried the Substring() function as well.
i like the name Ruth Bender!
How can I select the entire date portion of a "dd/mm/yyyy hh:mm:ss AM/PM" date/time stamp? One catch is that the Len() function can't be used because sometimes the day, month, or any component of the time is one digit (i.e., no 'filler' zeros are used). I haven't been able to find any forum on how to extract the ENTIRE date portion of a date/time stamp - i.e. only removing the time. Any suggestions? Thanks in advance!
tks, its working....