Wednesday, 19 April 2017

MSSQL - Fix Error - Conversion failed when converting date and/or time from character string.



Msg 241, Level 16, State 1, Line 5
Conversion failed when converting date and/or time from character string.

Watch solution on YouTube



To fix it replace
Declare @SQL varchar(max)
Declare @DateFrom Datetime = '1/1/2000'

Set @SQL = 'Select * FROM Users Where StartDate > ' +  @DateFrom

Exec  ( @SQL )

With


Declare @SQL varchar(max)
Declare @DateFrom Datetime = '1/1/2000'

Set @SQL = 'Select * FROM Users Where StartDate > ' + Convert(Varchar, @DateFrom, 101)

Exec  ( @SQL )

No comments:

Post a Comment