?
Solved

Count date in tsql with smalldatetime

Posted on 2014-11-21
3
Medium Priority
?
155 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 1200 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 800 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

777 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