Solved

Count date in tsql with smalldatetime

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

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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

690 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