0

I know this question was asked many times, but i am not able to find a solution for it

This is my code

  string query = @"SELECT *
    FROM SMSMessage
    WHERE (respondCode  IS @respondCode)
and (sentOn > '08/26/2016')
       ";
                //string query = "select * from SMSMessage";
                SqlConnection con = new SqlConnection(Utilities.getConnectionString());
                SqlCommand cmd = new SqlCommand(query, con);
                cmd.Parameters.AddWithValue("@respondCode", DBNull.Value);

I want the responseCode to be null,

I am getting error:

syntax error near @responseCode

when I do this responseCode is NULL, there is no syntax error, but the query for some reaonse doesn't bring any result

Help please

5
  • If the NULL is fixed then you don't need a parameter. Do you want to find all records from SMSMessage where RespondCode IS null and sent after that date? Commented Aug 27, 2016 at 13:21
  • @Steve yes exactly Commented Aug 27, 2016 at 14:02
  • Then why not WHERE (respondCode IS NULL) AND .... Pay attention to the string format for your Date constant also because the correct interpretation of that constant depends on the sql server localization Commented Aug 27, 2016 at 14:07
  • @Steve that is the thing, you got me, please look at my comment to the answer below, the code is no working, it gives the same results always. do you mean the and should be capital letter? Commented Aug 27, 2016 at 14:13
  • @Steve the format of the SentOn is date time Commented Aug 27, 2016 at 14:14

2 Answers 2

2

I would use directly IS NULL and not passing any parameter, but the most important change is how do you apply the date constant in your query statement.
Assuming you use the Italian locale in your sql server database I would use

string query = @"SELECT * SMSMessage
                 WHERE respondCode  IS NULL
                 AND (sentOn > CONVERT(DateTime, '26/08/2016', 105))

T-SQL Convert docs

On the contrary I would look carefully to the value passed for the sentOn condition. If this value changes dynamically it is better to use a parameter for this value. In this way the query optimizer of sql server will be able to build a better (faster) execution plan

string query = @"SELECT * SMSMessage
                 WHERE respondCode  IS NULL
                 AND sentOn > @dateLimit";

SqlConnection con = new SqlConnection(Utilities.getConnectionString());
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.Add("@dateLimit", SqlDbType.DateTime).Value = new DateTime(2016, 8, 26);
Sign up to request clarification or add additional context in comments.

10 Comments

what does 105 mean please ?
i understood from the first answer that i can't pass "null" as it is in this case because i have two conditions, are you sure that it is correct the way you passed null please ?
I am not passing NULL, there is no parameter in the query above.
yes there is respondCode parameter in my code, that i want to pass null for it, in your query you passed null as string, and in the first query the guy said that if i have two conditions, i can't pass null as string. you got me please ?
The point is: if you search always for records that have NULL in the respondCode then there is no need of a parameter there. Just write the IS NULL in the query and that's all.
|
1

I guess you want this

Where (respondCode = @respondCode or @respondCode is null)
  and sentOn > '08/26/2016'

When a value is passed to @respondCode parameter the records will be filter based on @respondCode and sentOn > '08/26/2016'.

When nothing is passed to @respondCode parameter (ie) NULL, then records will be filtered only based on sentOn > '08/26/2016'

As mentioned in comments by Steve, If you need records only when respondCode is NULL then no need of that variable just hardcode the NULL condition in Where clause

Where respondCode is null
  and sentOn > '08/26/2016'

8 Comments

It is not working, and it is not throwing an error, it allows gives the same results, even though the results are not correct
@MarcoDinatsoli Can you add some sample data and expected result
when i check the count of the returning rows from that query it is always the same, though i am changing the responseCode to a value not null
@MarcoDinatsoli - can you post the query you are using
i posted it it is in the question
|

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.