1

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

Conversion failed when converting date and/or time from character string. I tried converting the date to compare i nstring, like this

string dateFormat = date.ToString("dd/MM/yyyy");

and then write the query like this:

string sql = "select * from TB_RICHIESTE where CONVERT(DATE, Date) <= CONVERT(DATE, '" + dateFormat + "')";

But I have this excpetion. Someone can help me? Thanks

2
  • 6
    Don't. Use the proper type for the field and the parameter. And don't construct SQL statements by concatenating strings. Use parameterized queries Commented Oct 26, 2017 at 9:03
  • If the table has a date in the form dd/MM/yyyy there's a very serious bug. The field's type should be corrected to date. You can't sort by such a field or search for date ranges, eg dates after a specific date Commented Oct 26, 2017 at 9:06

2 Answers 2

5

First, you should not store dates as strings.
As Panagiotis Kanavos wrote in his comment - this is a serious bug. You can't sort by such a column, you can't search for date ranges, and most important - you can't control if someone enters an invalid value - nothing is stopping someone from entering "Alfredo" to that column.
For more information, read Aaron Bertrand's Bad habits to kick : choosing the wrong data type.

Second, you should not pass dates from .Net to Sql server as strings. you should pass instances of DateTime as parameters. The .Net DateTime maps directly to SQL Server's Date.

If you can't change the data type of the column, you can at least convert it to date using the proper convert style (103 in your case).

Here is a better way to do it:

var sql = "select * from TB_RICHIESTE where CONVERT(DATE, [Date], 103) <= @Date";

Then you add the @Date parameter to the SqlCommand:

com.Parameters.Add("@Date", SqlDbType.Date).Value = date.Date;
Sign up to request clarification or add additional context in comments.

8 Comments

Slightly better: ... .Value = date.Date;. Then it works even if the type in the database is(or will be) DateTime.
@TimSchmelter I'm already converting the data type of the column to Date, and the data type of the parameter is also Date. I don't see how sending date.Date instead of simply date would change anything.
Perfect solution. Thanks
Glad to help :-)
@ZoharPeled: it wouldn't change and it wouldn't hurt. Your code would hurt if OP changes to DateTime and then changes SqlDbType.Date to SqlDbType.DateTime. With my little change that won't cause any harm. You are relying on the database, i'm relying on database and C#
|
0

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 + "')";

2 Comments

First, this is not ISO format. If you have to pass string literals for dates, use yyyy-mm-dd or better yet yyyymmdd. Second, concatenating strings into SQL statements is risky, since it's an open door for SQL Injection attacks. Might not be the case here but it's always better to use parameters instead of concatenating strings.
If the real ISO or the unseparated format are used there's no reason to convert the parameter. Converting the field is a bad idea that prevents the database from using indexes and results in a full table scan

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.