Solved

Count date in tsql with smalldatetime

Posted on 2014-11-21
3
142 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
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now