Sql where case when null




















Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually.

Visit chat. Related Hot Network Questions. Amirhossein 2 2 gold badges 10 10 silver badges 31 31 bronze badges. Your welcome, please read help center and get tour for give best feedback. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.

Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually.

Related Hot Network Questions. You had the same idea as I did. However, you do need the parentheses. I'm not sure whether you do or don't need the parentheses, but I'm a big advocate of parentheses in complex boolean expressions. Not having them leaves it up to the compiler to do order-of-operations and is rather scary, if you ask me. Just testing your first answer now — Russ Cam. I also tried it and CASE doesn't work this way. My suggestion to use the CASE statement to solve this problem see my answer is therefore wrong.

Show 1 more comment. BobbyShaftoe BobbyShaftoe Let's say that I need to filter by a string. If the string is null I should return all the records from a table, if it is not null I should return all the records that match the string or part of it LIKE operator.

How would you solve that? Chad Grant Chad Grant Deviant - I think your missing the question slightly. Another option, and I think somebody mentioned before, is to create a calculated column, and create and index by it. If the value is selective enough, then the optimizer could opt for using the index. I think that I understand your concerns.

However, as presented,. EVERY row has to be evaluated. Both sides of the AND are tested. You could, perhaps use a nested CASE structure -but I doubt if that would actually save any processing time. There is no guarantee that the database engine will evaluate [AND] criteria in any particular order. I also didn't realize the query analyzer will NOT short circuit logical statements. I am also wondering about performance and I'm going to have to break down and test a few things in a sample table.

The expression you used is more readable than using the case function, so even if it yields same execution plan, the next developer to change the code, will be grateful that you used this version.

Sign in.



0コメント

  • 1000 / 1000