Domanda

Here is my table

T_idx          T_Tag         T_Date  
1              x             2014-03-15 23:58:00.000
2              y             2014-03-15 23:57:00.000
.               .                  .
.
.               .                  .
.
555            xy            2014-03-15 5:10:00.000

From the above table, I want to see the data in between 5:30 AM to 6:30 AM every day

Could you please any one suggest me the query to get the data.

È stato utile?

Soluzione

You can do it this way in SQL Server 2005:

select * from mytable 
where convert(datetime, convert(char(5), T_Date, 108)) >= '5:30'
and convert(datetime, convert(char(5), T_Date, 108)) <= '6:30'

In SQL Server 2008 and above, you can simply cast it to time data type:

select * from mytable 
where convert(time, T_Date) >= '5:30'
and convert(time, T_Date) <= '6:30'

Altri suggerimenti

Try this

SET @start_date = DATEADD(minutes,30,DATEADD(hour, 5, DATEDIFF(DAY, 2, GETDATE())))
SET @end_date = DATEADD(hour, -1, @start_date)

SELECT 
    T_idx, T_Tag
FROM 
    YourTable
WHERE
    T_Date BETWEEN @start_date AND @end_date 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top