Frage

I'm using the below sql query and I get unexpected results. It is as if the sql is making a row number over string not over DateTime even though I have casted the generated sstring to DateTime. Can you help me align the indecies correctly? Thank you.

THE SQL QUERY

SELECT 
ROW_NUMBER() OVER (ORDER BY ((SELECT CAST(CAST(emd.DateOfMeasure AS DATE) AS NVARCHAR) + 
                ' ' + CAST(DATEPART(HOUR, DateOfMeasure) AS NVARCHAR) +
                ':' + CAST(DATEPART(MINUTE, DateOfMeasure) AS NVARCHAR) + ':00' AS DATETIME))) AS RowNum, 
CAST((SELECT CAST(CAST(emd.DateOfMeasure AS DATE) AS NVARCHAR) + 
                ' ' + CAST(DATEPART(HOUR, DateOfMeasure) AS NVARCHAR) +
                ':' + CAST(DATEPART(MINUTE, DateOfMeasure) AS NVARCHAR) + ':00' AS DATETIME) AS DATETIME) AS 'Date'
            from Data emd

THE UNEXPECTED RESULT

Index      DateTime
1   2013-10-16 00:30:00.000
2   2013-10-16 00:45:00.000
3   2013-10-16 01:00:00.000
4   2013-10-16 01:15:00.000
5   2013-10-16 01:30:00.000
6   2013-10-16 01:45:00.000
7   2013-10-16 10:00:00.000
8   2013-10-16 10:15:00.000
    .
    .
    .
45  2013-10-16 19:30:00.000
46  2013-10-16 19:45:00.000
47  2013-10-16 02:00:00.000
48  2013-10-16 02:15:00.000
War es hilfreich?

Lösung

As indicated in the comment, I think that this is a simpler formulation of your query:

SELECT 
ROW_NUMBER() OVER (
      ORDER BY DATEADD(minute,DATEDIFF(minute,0,DateOfMeasure),0)) AS RowNum, 
DATEADD(minute,DATEDIFF(minute,0,DateOfMeasure),0) AS 'Date'
from Data emd

Does it also exhibit the same strangeness?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top