0

I have this query, which works perfect in the codeline and in my MySQL Manager:

SELECT field1, field2, field3
FROM Mytable
WHERE date_start >= STR_TO_DATE(' 01/07/2014 ', '%d/%m/%Y')  

Now, in the CodeBehind using parameter it doesn't return a single row...

sql = @"SELECT field1, field2, field3
    FROM Mytable
    WHERE date_start >= STR_TO_DATE('@data', '%d/%m/%Y')"

MySQLCOMMAND.Parameters.Add(new MySqlParameter("@data", MySqlDbType.Date)).Value = data;  

Where data = 01/07/2014.

Why it doesn't work with parameter?

1 Answer 1

1

Your parameter is already a date. You don't need to convert it:

SELECT field1, field2, field3
FROM Mytable
WHERE date_start >= @data;
Sign up to request clarification or add additional context in comments.

5 Comments

I HAVE TO use STR_TO_DATE as the value I'm passing, isn't in the format yyyy/mm/dd.
@PlayHardGoPro . . . The value that you are passing is a date as defined by MySqlDbType.Date. The conversion is done at the application layer not at the database layer.
I don't know man, but without STR_TO_Date, it returns me every row on the database =s not just those included in the filter =s
The query you're using in MySQL Manager is not equivalent to the query you're generating in C#, because the type you're using in C# is already a Date - if you want to pass a string parameter as the database type, you should use VarChar or Text as the MySqlDbType.
If you need it in yyyy/mm/dd format try using DateTime.TryParseExact method shown in example 3 here http://www.dotnetperls.com/datetime-tryparse

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.