Ignoring fields in SQL Where Statement

5 pts.
Tags:
SQL
SQL WHERE statement
SQL/400
WHERE statement
Can fields in a where statment be ingnored. Such as Where name = :name and date = :date, but if I just pass in a name I'd like the where to select only on the name and ignore the date.

Answer Wiki

Thanks. We'll let you know when a new response is added.

In oracle exists a function NVL(a,b) which will return b if a is null.
In sql server I think you can achieve the same by using COALESCE or isNull functions.

I don’t know if you can use some of these functions in AS/400. I think COALESCE is based on the ANSI SQL standard, so it is possible that you can use it.

If that’s the case, the query should be constructed like this:

<pre>select *
from your_table
where name = :name
and date = coalesce(:date,date)</pre>

This way, if :date is null, the date field will be compared to itself, which produces the same result as if the condition did not exist.

Hope this helps.

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Gilly400
    Hi, I normally do something along these lines :- Select xxxxx From xxxxx Where (name = :name or :name = '') and (date = :date or :date = 0) May not be the nicest way of doing this, but it works. Regards, Martin Gilbert.
    23,730 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following