If you’ve ever used SQL Server, you know that there’s no such thing as a Date or Time, only Datetime. According to SQL Server Books Online: “If only a time is specified when setting a datetime or smalldatetime value, the date defaults to January 1, 1900. If only a date is specified, the time defaults to 12:00 A.M. (Midnight).”, which is all is fine and good but it becomes an issue when you want just the date or just the time from a column that stores both. Say for instance that my boss wants to see the number of orders per day over the last couple years to see which days have the most orders (‘cyber monday!‘). Unfortunately, there’s no function called date() or time() which returns just the date portion of the datetime or just the time portion of the datetime:
-- doesn't work...
SELECT date(mydate) as thedate, count(id) as perday
FROM orders
GROUP by thedate
ORDER by perday DESC
Turns out there’s a hack that does though:
SELECT distinct convert(varchar,mydate,111) as thedate, count(id) as perday
FROM orders
group by convert(varchar,mydate,111)
order by perday DESC
Hope that makes your day.
What’s Going On Here?
My name is Aaron Johnson and I created this blog both for me (mostly) and sometimes you. I've been saving mydeliciouspinboard.in links here and blogging since 2002. During the week (and at night and some weekends and well.. most of the time), I work in engineering at Jive Software. When I'm not working, I'm hanging out with my amazing wife, ourdinosaurStar Wars loving son and four chickens in the burbs outside of Portland, Oregon.See Also
Monthly Archives
- May 2013 (3)
- April 2013 (4)
- March 2013 (3)
- February 2013 (5)
- January 2013 (7)
- December 2012 (1)
- November 2012 (4)
- October 2012 (5)
- September 2012 (3)
- August 2012 (3)
- July 2012 (7)
- June 2012 (5)
- May 2012 (3)
- April 2012 (5)
- March 2012 (5)
- February 2012 (9)
- January 2012 (9)
- December 2011 (10)
- November 2011 (6)
- October 2011 (6)
- September 2011 (5)
- August 2011 (5)
- July 2011 (8)
- June 2011 (13)
- May 2011 (3)
- April 2011 (10)
- March 2011 (6)
- February 2011 (2)
- January 2011 (4)
- December 2010 (8)
- November 2010 (12)
- October 2010 (9)
- September 2010 (6)
- August 2010 (4)
- July 2010 (8)
- June 2010 (9)
- May 2010 (4)
- April 2010 (9)
- March 2010 (6)
- February 2010 (9)
- January 2010 (10)
- December 2009 (10)
- November 2009 (10)
- October 2009 (6)
- September 2009 (10)
- August 2009 (13)
- July 2009 (12)
- June 2009 (11)
- May 2009 (8)
- April 2009 (4)
- March 2009 (7)
- February 2009 (2)
- January 2009 (3)
- December 2008 (4)
- November 2008 (7)
- October 2008 (10)
- September 2008 (6)
- August 2008 (7)
- July 2008 (9)
- June 2008 (15)
- May 2008 (9)
- April 2008 (10)
- March 2008 (8)
- February 2008 (6)
- January 2008 (15)
- December 2007 (10)
- November 2007 (9)
- October 2007 (6)
- September 2007 (9)
- August 2007 (12)
- July 2007 (9)
- June 2007 (6)
- May 2007 (8)
- April 2007 (10)
- March 2007 (14)
- February 2007 (12)
- January 2007 (17)
- December 2006 (11)
- November 2006 (11)
- October 2006 (8)
- September 2006 (11)
- August 2006 (14)
- July 2006 (11)
- June 2006 (13)
- May 2006 (11)
- April 2006 (8)
- March 2006 (5)
- February 2006 (7)
- January 2006 (8)
- December 2005 (6)
- November 2005 (6)
- October 2005 (9)
- September 2005 (3)
- August 2005 (11)
- July 2005 (12)
- June 2005 (11)
- May 2005 (4)
- April 2005 (5)
- March 2005 (8)
- February 2005 (5)
- January 2005 (3)
- December 2004 (6)
- November 2004 (7)
- October 2004 (4)
- September 2004 (9)
- August 2004 (5)
- July 2004 (10)
- June 2004 (12)
- May 2004 (4)
- April 2004 (13)
- March 2004 (10)
- February 2004 (9)
- January 2004 (13)
- December 2003 (8)
- November 2003 (9)
- October 2003 (17)
- September 2003 (28)
- August 2003 (21)
- July 2003 (24)
- June 2003 (31)
- May 2003 (43)
- April 2003 (30)
- March 2003 (48)
- February 2003 (45)
- January 2003 (43)
- December 2002 (28)
- November 2002 (30)
- October 2002 (34)
- September 2002 (41)
- August 2002 (35)
- July 2002 (20)
- June 2002 (1)
Try DatePart – a function in your SQL statement:
DatePart(type, date)
Example:
DatePart(“q”, datetimefiled) for Quarter.
DatePart(“yyyy”, field) for year as four digit date field.
Here are some things you can check against (types):
yyyy year
q Quarter
m Month
y Day of Year
d Day
w Weekday
ww Week
h Hour
n minute
s Second
So you might have a query like this:
Select “Valid Years are: ” & DatePart(“yyyy”, orderDate) WHERE DatePart(“yyyy”,orderDate) > 2001
Hope this made sense.
I think most databases support this in your SQL.
Good luck -
Greg
I know someone out there will rant about how I’m denormalizing the data, but … I normally cheat by creating extra columns in my database:
foo_date (date + time)
foo_day (date + 12am)
foo_month (date – days + 12am)
foo_year (smallint for year)
And, if I need it:
foo_time (1/1/01 + time)
foo_hour (1/1/01 + time – minutes)
I then have a set of triggers that I cut and paste that fill in the other fields when foo_date is updated.
Yes, that seems completely crazy … but it works. And it’s blindingly fast, as I never have to use any aggregate functions to futz with the data. (Some might argue that the data bloat leads to page hits, but RAM is cheap.) Also, the front-end logic is easier to read as you’re not building strange DatePart statements, just substituting different postfixes.
I tried to look up some old queries in which I grouped by day, because I thought I found a better way to do this… then realized I’m grouping by convert(varchar,…, 101) too
Realy very help foul code
Nice fix Aaron. I just wanted to add that if you wan’t to be able to manipulate the output of the query as a smalldatetime, cast Aaron’s data back to a smalldatetime in your query. So effectively, you’re assigning all time portions of the data type to 00:00:00.
how can i run a quert in sql server to get value between two date range
you saved me! hehehehe… ty very much! ^^
Thanks a ton, you saved my life! ^^
THANK YOU SO MUCH FOR THIS CODE!
Thank you. That\’s just what i was searching for!
My day has been made.
Freaking dates.
Nice job on this – had the issue come up at work today and this helped me complete my task. Needed number of uniqe web visitors per day. Not the lceanest code, but here it is:
SELECT COUNT (DISTINCT(CONVERT(VARCHAR(40), sessionid))) AS ‘NumberofHits’,
CONVERT(VARCHAR(12),LoginTime) AS ‘Date’
FROM web_session
WHERE ((LoginTime >= CONVERT(SMALLDATETIME,@reportBeginDate)
AND LoginTime
Nice tip! Thanks.
That makes my day, thanks.
Yep, made my day. Two hours with Google and yours was the winner! Works great.
Very nice indeed… Thanks a million!
The result from this query is a bit cleaner (in that it returns actual “dates” and discards the distinct construct) but unsure about the cost on the database?
Anyway, just putting it out there …
SELECT dateadd(dd,0, datediff(dd,0,mydate)) as thedate, count(id) as perday
FROM orders
group by dateadd(dd,0, datediff(dd,0,mydate))
order by perday DESC
Just splendid. Thanks a lot!
Hi,
I understood the concept.But,SQL server 2005,throws
“Column Some_Date’ is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause”.
Is it possible to use Date functions in Group By clause ?.Please help me to get clear Idea.
Thanks,
Mathi
Hi,
Sorry for previous post.It works fine while adding date convert function in selection list too.
Thanks,
Mathi
Thanks Aaron and gs!
good job
Thanks!
Thank you for this code, Thanks a million!
SELECT YEAR(datefield) Y, MONTH(datefield) M, DAY(datefield) D FROM yourtable
GROUP BY DAY(datefield)
Awesome… Thanks for the post.
Very helpful!
Very Thanks …It’ great!
SELECT CAST(CAST(CAST(dtmStart AS FLOAT) AS INT) AS DATETIME) AS dtmStart
FROM Class
WHERE ClassID = 92134
nice example…..
From SQL SERVER 2008 simply:
SELECT convert(Date,DATEPLUSTIME) As OnlyDate FROM DATATABLE
GROUP BY convert(Date,DATEPLUSTIME)
Data typ Date is without time information
love it
perfect
Great! just what I was looking for.
Is that query can be converted to linq? (I tried to figure it out by myself …)