Skip to main content
Ben Nadel at InVision In Real Life (IRL) 2019 (Phoenix, AZ) with: Keeley Hammond
Ben Nadel at InVision In Real Life (IRL) 2019 (Phoenix, AZ) with: Keeley Hammond ( @keeleyhammond )

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

By on
Tags: ,

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

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

Want to use code from this post? Check out the license.

Reader 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.



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


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)


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!

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel