简体   繁体   中英

SQL Server check if value is substring inside isnull

I have a field in UI interface that passes to a stored procedure a null value (when field is unfilled) or a contract number when it is filled. Substrings of the contract number are accepted as input. Inside the procedure, I need to filter the results by this parameter. I need something similar to this:

SELECT * FROM tableName tn
WHERE
tn.ContractNumber LIKE ISNULL('%' + @contractNumber + '%', tn.ContractNumber)

What do you think it is the best approach? Problem is that using a condition like this does not return values.

Simply:

SELECT * 
FROM   tableName tn
WHERE  tn.ContractNumber LIKE '%' + @contractNumber + '%'
    OR @contractNumber IS NULL

You are really checking multiple condition, so having them separated reads more intuitive (for most people, anyway).

I assume this is just a sample query, and you are not selecting * in reality...

Another one:

SELECT * 
FROM   tableName tn
WHERE  tn.ContractNumber LIKE '%' + ISNULL(@contractNumber, '%') + '%'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM