开发者

SQL detecting null value and change query

开发者 https://www.devze.com 2023-01-10 23:36 出处:网络
i am using c# and mssql. i send parameters from c# to sql and execute a query but sometimes it comes null values. i m using sql like function. it is g开发者_高级运维reat for string values but has prob

i am using c# and mssql. i send parameters from c# to sql and execute a query but sometimes it comes null values. i m using sql like function. it is g开发者_高级运维reat for string values but has problems with bool. for example bool b = NULL query executes like that: .... WHERE B LIKE '%%' and it never returns a value. i want to detect null value and change query if b is null the query must be like that ...... WHERE B IS NOT NULL i could change this in c# code but there are some other parameters like that it could be easier in sql if it is possible

thanks


SQL server supports the standard SQL COALESCE function; try wrapping your boolean values in a coalesce call, e.g. WHERE COALESCE(bool_value, '') LIKE '%%'


Use parameterized queries and all of this is taken care of for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消