【问题标题】:Convert date to datetime2(7) in sql在sql中将日期转换为datetime2(7)
【发布时间】:2016-05-16 09:05:40
【问题描述】:

在我的表值函数中,我有一个日期类型的变量。我需要将它(在 where 子句中)与表中列的值进行比较,其类型为 datetime2。任何人都可以帮我解决这个问题,因为演员表不适合我。

这是我的代码:

select * 
from table1 
where [Date1] >= cast(@Todate as datetime2)

这里,[Date1]table1datetime2 类型的列,@Todatedate 类型的列。

【问题讨论】:

    标签: sql sql-server datetime casting


    【解决方案1】:

    使用下面的查询。

    select * 
    from table1 
    where [Date1] >= convert(datetime2, @Todate)
    

    【讨论】:

    • 这不起作用... :( 我不想在 where 子句中转换它,因为有大量记录。所以,我放了一个 datetime2 类型的变量,并且将此与 [Date1] 列进行比较。DECLARE @ToDtTm DATETIME2 = Convert(DATETIME2, @toDate)
    • 我得到了解决方案。问题是,日期部分被转换为 datetime2,但时间为 00:00:00。然后我按照以下方式做了,它奏效了。 select * from Table1 where [Date1] >= DATEADD(MS,-1,CAST(DATEADD(DD,1,@toDate) AS datetime2))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 2016-11-30
    • 2022-08-17
    • 2020-10-03
    相关资源
    最近更新 更多