Solved

Count date in tsql with smalldatetime

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

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

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

803 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