Pregunta

I am trying to calculate the number of days that an event category has occurred in T-SQL in SSMS 2008. How do I write this expression? This value is stored as a datetime, but I want to count the day portion only. For example, if my values were:

2013-01-05 19:20:00.000
2013-01-06 17:20:00.000
2013-01-06 18:20:00.000
2013-01-06 19:20:00.000
2013-01-03 16:15:00.000
2013-01-04 12:55:00.000

Then although there are 6 unique records listed above, I would want to count this as only 4, since there are 3 records on 1/6/2013. Make sense?

This is what I'm trying now that doesn't work:

select 
count(s.date_value) 
From
    table_A s 
¿Fue útil?

Solución

Cast the datetime value as a date. Also if you want only unique values, use DISTINCT:

SELECT COUNT(DISTINCT CAST(date_value AS date)) FROM table_A

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top