Solved

Count date in tsql with smalldatetime

Posted on 2014-11-21
3
151 Views
Last Modified: 2014-11-24
I'm trying to update this query and return a column with the date/month/year along with the count of how many records are in each day.
SubmitDate datatype is smalldatetime
Part of the problem is that the smalldatetime also includes also includes hour/minutes/seconds.

SELECT COUNT(SubmitDate) AS CountDate
FROM MyTable
GROUP BY DATEADD(dd, 0, DATEDIFF(dd, 0, SubmitDate))

Open in new window


Thanks.
0
Comment
Question by:javierpdx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Accepted Solution

by:
Richard Obenchain earned 300 total points
ID: 40458856
Why not just put the grouping value in the select list?

Like so:

SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, SubmitDate)) as Day, COUNT(SubmitDate) AS CountDate
FROM MyTable
GROUP BY DATEADD(dd, 0, DATEDIFF(dd, 0, SubmitDate))

Open in new window


Or did you need separate columns for each value year, month, and day?
0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 200 total points
ID: 40458871
As you have SQL 2012 in your tags you could use CAST(SubmitDate as date)
and then also use FORMAT() to get the desired display

SELECT  FORMAT(CAST(SubmitDate as date),'dd/MM/yyyy') as OnDate, COUNT(SubmitDate) AS CountDate
FROM MyTable
GROUP BY  CAST(SubmitDate as date)

Open in new window


While DATEADD(dd, 0, DATEDIFF(dd, 0, SubmitDate)) is very useful for older versions of SQL Server it doesn't have an advantage here as all we want is the date. As indicated above it's not clear if you just want one column in dd/MM/yyyy format or 3 columns.
0
 

Author Closing Comment

by:javierpdx
ID: 40462612
I appreciate the help!
These solutions worked great.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question