Working with datetime with dynamic query in SQL Server

后端 未结 7 2781
野性不改
野性不改 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 12:09

    This is late, but may be it help someone

    What you need is a quotes around your date, You already got your answer.

    Below is an example of what I usually put in my query

    '(CONVERT(DATETIME,CONVERT(varchar,gd.CreatedDate),106) <= CONVERT(DATETIME,'''+CONVERT(varchar, @EndDate ) +''',106))'
    

    note that @EndDate is of type Datetime here

提交回复
热议问题