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,314 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,314 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,314 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
Jun 18, 2013 at 9:20 PM
Mapping AngularJS Routes Onto URL Parameters And Client-Side Events
I couldn't find examples of passing multiple arguments using the when() routing statement so figured out through trial and error that you can pass multiple arguments using the following format: .whe ... read »
Jun 18, 2013 at 3:39 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
Hi Ben, THANKS! While not bleeding edge, it is new to me & I like learning new things every day! ... read »
Jun 18, 2013 at 12:30 PM
Disabling Auto-Correct And Auto-Capitalize Features On iPhone Inputs
Also spellcheck="false" should be mentioned as part of html5 specs ... read »
Jun 18, 2013 at 8:40 AM
Using Named Functions Within Self-Executing Function Blocks In Javascript
Hi Ben, you forgot to mention the most important thing for named self-executing functions - they can be referenced by name ONLY inside their execution context (which is parens in this case), it mean ... read »
dee
Jun 18, 2013 at 7:01 AM
My Safari Browser SQLite Database Hello World Example
hai ben, this program is really good i could understand the concept but i dint know how to save it and how to open it as you have done in the video can u give that details pls ... read »
Jun 18, 2013 at 6:04 AM
Clearing Inline CSS Properties With jQuery
Thanks a lot for for post! It helped me a lot... after being stuck since 24 hrs.. found solution from your post. Thanks again! ... read »
Jun 18, 2013 at 2:31 AM
SOTR 2013 - The Best Conference I Never Went To
I keep watching it, should keep me happily distracted until SotR14 ;) ... read »
Jun 17, 2013 at 9:45 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, As I was reading what you wrote, it occurred to me that maybe I do something similar to that in some of my client-side code. In an application I'm working on, there are a bunch of unrelated ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools