Sql-server – Select count of timestamp formatted field in short date format to give number of rows

dateselectsql servertimestamp

I have a field titled DTUpdated and it is a timestamp field.

I am trying to select a count of the dates in the rows that are xx old. i.e. 0-3 months, 3-6 months etc.

I am not at the writing date selection criteria for the selection yet as i am stuck getting the first part of the query to simply count the occurrences of each day in a results pane.

The query i have so far is as follows but it is not grouping the data by the days as returned.

SELECT CONVERT(VARCHAR(10),[SCAData].[dbo].[Camera].[DTUpdated],105) AS Date_Updated,
COUNT(DTUpdated) AS No_of_Rows 
FROM [SCAData].[dbo].[Camera]
GROUP BY DTUpdated  
ORDER BY DTUpdated; 

Result:

Date_Updated    No_of_Rows
03-10-2006      1
03-10-2006      1
05-10-2006      1
05-10-2006      1
05-10-2006      1
05-10-2006      1
05-10-2006      1
05-10-2006      1
05-10-2006      1
06-10-2006      1
06-10-2006      1
06-10-2006      1
06-10-2006      1
06-10-2006      1

Can someone please help or also suggest how to then get these back based on date selections of last 3 months, 3-6 months?

Many thanks in advance.

Best Answer

Since the column type is a datetime and this is SQL Server 2016, converting it to a date is easy :

SELECT CONVERT(Date,[SCAData].[dbo].[Camera].[DTUpdated]) AS Date_Updated,
COUNT(DTUpdated) AS No_of_Rows 
FROM [SCAData].[dbo].[Camera]
GROUP BY CONVERT(Date,[SCAData].[dbo].[Camera].[DTUpdated])  
ORDER BY 1; 

If you really want to do it via an VARCHAR field, just do it like that :

SELECT CONVERT(VARCHAR(10),[SCAData].[dbo].[Camera].[DTUpdated],105) AS Date_Updated,
COUNT(DTUpdated) AS No_of_Rows 
FROM [SCAData].[dbo].[Camera]
GROUP BY CONVERT(VARCHAR(10),[SCAData].[dbo].[Camera].[DTUpdated],105)  
ORDER BY 1;