Ask Ben: Selecting Parts of a Date/Time Stamp In SQL

Posted July 26, 2006 at 6:27 PM by Ben Nadel

Tags: SQL, Ask Ben

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:

  • SELECT
  • id,
  • (
  • DATEPART(
  • mm, -- mm: Select only the month
  • birthday
  • )
  • ) AS birthday_month
  • FROM
  • girlfriends

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
weekday: dw
hour: hh
minute: mi, n
second: ss, s
millisecond: ms

You can learn more about this function at the MSDN website.



Reader Comments

Sep 18, 2009 at 8:15 AM // reply »
1 Comments

Hey...
cool!!
Thanks i used it in my project.


Sep 18, 2009 at 8:19 AM // reply »
10,640 Comments

@Yogesh,

Awesome. Glad to help.


Dec 1, 2009 at 3:20 PM // reply »
3 Comments

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.


Dec 15, 2009 at 3:13 PM // reply »
1 Comments

Thanks for this, made use of this today :)


Dec 15, 2009 at 6:06 PM // reply »
10,640 Comments

@Connie,

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.


May 28, 2010 at 10:25 AM // reply »
1 Comments

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


Jun 7, 2010 at 10:48 PM // reply »
10,640 Comments

@Praveen,

I am not sure off-hand. Typically, I'll do that level of formatting in the ColdFusion side of things.


Mar 10, 2011 at 4:18 PM // reply »
1 Comments

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)


Mar 29, 2011 at 12:51 PM // reply »
1 Comments

Is there any help for someone trying to do this with a query of queries? I tried the Substring() function as well.


May 24, 2011 at 10:52 AM // reply »
1 Comments

Good reference.
i like the name Ruth Bender!


Oct 18, 2011 at 10:41 AM // reply »
1 Comments

Hi Ben,

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!


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »