Tesseract
Tesseract

Reputation: 1587

How do i find the total number of records created on a given day using T-SQL?

I need to find out the total number of records that were created on a given day.

e.g.

ID CreatedDate
1 17/07/2009
2 12/07/2009
3 17/07/2009
4 05/07/2009
5 12/07/2009
6 17/07/2009

Output:

3 Records were created on 17/07/2009
2 Records were created on 12/07/2009
1 Record was created on 05/07/2009

EDIT

Upon testing the second suggestion made by Chris Roberts of including the formatting in the SQL i get this error:

Syntax error converting the varchar value ' Records were created on ' to a column of data type int.

Does anyone know how to rework the solution so that it works?

Upvotes: 1

Views: 583

Answers (3)

Chris Roberts
Chris Roberts

Reputation: 18802

You should be able to get the data you're after with the following SQL...

SELECT COUNT(ID), CreatedDate FROM MyTable GROUP BY CreatedDate

Or - if you want to do the formatting in the SQL, too...

SELECT CONVERT(varchar, COUNT(ID)) + ' Records were created on ' + CONVERT(varchar, CreatedDate) FROM MyTable GROUP BY CreatedDate

Good luck!

Upvotes: 6

Brian Yarger
Brian Yarger

Reputation: 1975

select count(*), CreatedDate from table group by CreatedDate order by count(*) DESC

Upvotes: -2

Ghoti
Ghoti

Reputation: 2380

Is the column actually a timestamp? In which case you will need to apply a function to remove the time component, e.g.:

SELECT COUNT(*), date(CreatedDate) FROM MyTable GROUP BY date(CreatedDate)

I don't know what the function is in T-SQL, it's date() in MySQL and trunc() in Oracle. You may even have to do a to_string and remove the end of the string and group by that if you lack this.

Hope this helps.

Upvotes: 1

Related Questions