Working with datetime with dynamic query in SQL Server

后端 未结 7 2778
野性不改
野性不改 2021-02-20 11:56

I am using a dynamic query wherein I want to use the variable which holds the datetime, whenever I execute the query it says cannot convert datetime from string, when I cast tha

7条回答
  •  南笙
    南笙 (楼主)
    2021-02-20 11:57

    Run this example and adapt it to your code. (There are not 3 contiguous single quotes)

    Declare @FromDATE  datetime
    ;Declare @ToDATE datetime
    ;set @FromDATE = getdate() 
    ;set @ToDATE = @FromDATE 
    
    ;Print 'WHERE TillDate BETWEEN ' + char(39) + CONVERT(VARCHAR(10),@FromDATE, 101) 
    + char(39) + ' and ' + char(39) + CONVERT(VARCHAR(10),@ToDATE, 101) + char(39)
    

提交回复
热议问题