C# SQL Server compare date with string

后端 未结 2 1237
悲哀的现实
悲哀的现实 2021-01-15 17:36

I have to compare a table field that is a date stored as varchar in the format \'dd/MM/yyyy\' with a date value, but the comparison fails. I have exception

2条回答
  •  情话喂你
    2021-01-15 18:37

    Use Parameter to pass date values refer @Zohar Peled post. This is the proper method handling date values. OR You can pass the date value in ISO format, refer the below code.

    string dateFormat = date.ToString("yyyy/MM/dd");
    
    
    string sql = "select * from TB_RICHIESTE where CONVERT(DATE, Date) <= CONVERT(DATE, '" + dateFormat + "')";
    

提交回复
热议问题