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 »
11,246 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 »
11,246 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 »
11,246 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 »
3 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!


Din
Mar 9, 2012 at 4:33 AM // reply »
1 Comments

tks, its working....


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
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 24, 2013 at 5:39 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Adam Oops! My mistake! I hadn't gotten that far in my testing - I'm still baby stepping my way through the process. ... read »
May 24, 2013 at 5:13 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
Hi Jason, Thanks for checking up on that, but I still stand firm on my position. :) There are actually two listLast()'s in use, and you're right that the one using a space as a delimiter is fine. ... read »
May 24, 2013 at 4:45 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Ben I have been lurking your site for quite some time, and haven't stepped up to comment until today. Thanks for all the great info - keep it up! @Adam I believe you are mistaken... as the commen ... read »
May 24, 2013 at 11:21 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, Ha ha, let's us never speak of justifying "##" notation again :P ... read »
May 24, 2013 at 11:18 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Ah, so it was indeed how I vaguely remembered it to be: A direct assignment value = users.id[ i ] causes value to retain the sticky datatype of the query column. Although unnecessary in ... read »
May 24, 2013 at 9:11 AM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Brandon, Hi, No, I haven't been able to do that. I have just kept it as it is. ... read »
May 23, 2013 at 9:52 PM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Muhmmadibn Did you figure out a solution to launching PDFs? I am running into the same issues myself. There is no way to close the PDF or go back once you launch it. Thanks in advance! ... read »
May 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools