SQL服务器时间相关

时间:2014-03-24 08:34:48

标签: sql-server-2008

我想在sql server 2008中将表格从晚上9点到11点从一个表中取出一个月。

我试过

 SELECT * FROM your_table 
WHERE DateCol between  (DateCol=cast (GETDATE() as DATE) AND TimeCol<'21:00:00') and 
(DateCol=cast (GETDATE() as DATE) AND TimeCol<'11:00:00') and Datecol between '2014-01-01 00:00:00.000' and '2014-01-31 23:59:59.760'

一个月,从晚上9点到凌晨11点的字段

1 个答案:

答案 0 :(得分:0)

试试这个!

 ;with cte as
    (
    select *,cast(Datetime_col as time) as X1,cast(Datetime_col as date) as X2 from table001
    )

    select * from cte where X1 between 21:00:00 and 11:00:00  and X2 between 01-jan-2014 and 31-jan-2014

Read this for date formats