Ask Ben: Selecting Parts of a Date/Time Stamp In SQL
Posted July 26, 2006 at 6:27 PM by Ben Nadel
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.
What Other People Are Searching For
[ local search ]
select only month part from a date in sql server
[ local search ] get hours from date time stamp
[ local search ] get minutes from date teme stamp
[ local search ] sql server day part from date
[ local search ] sql date only want day of month
[ local search ] day part of getdate
[ local search ] sql getting day of year
[ local search ] sql datetime get hours
[ local search ] getting year from timestamp
[ local search ] ms sql datepart
[ local search ] getting the month from date in sql
- Wanted: Full-Time ColdFusion Developer at Intoria Internet Architects
- Cold Fusion Senior Developer at Edge Information Management
- Back-End Web Developer-Information Technologist at Michigan State University
- ColdFusion Developer at Nonfat Media
- Mid-to-Senior Level Web Application Developer at SiteVision, 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....