在isnull中改变值

时间:2015-08-07 07:42:14

标签: sql stored-procedures sql-server-2012 where-clause isnull

我有一个存储过程,如果参数不为null,我正在为列的一部分进行搜索。

问题是我正在搜索的值混合在一长串其他值中。以下是行的外观示例:

'...value=153139,example=xyz,param=15,morestuff=otherthings...' //ignore the actual names, it's just examples for SO

所以这不会起作用,因为它可能会选择错误的东西(value=153139会导致匹配@param = '15'):

WHERE
    [column] LIKE ISNULL(@param, [column])

这也不起作用:

WHERE
    [column] LIKE '%param=' + ISNULL(@param, [column]) + '%'

对我而言,完美的事情就像ISNULL(check_expression, replacement_if_null, replecement_if_not_null),但据我所知,这不存在......

P.S。我知道信息的存储方式并不是最优的,但它是系统使用的,我必须使用它= /所以没有评论像“你应该拆分列以便它只包含一个值”请< / p>

1 个答案:

答案 0 :(得分:1)

您是否尝试过案例陈述:

case when check_expression is null then replacement_if_null else replecement_if_not_null end

 [column] LIKE '%param=' + ISNULL(@param, [column]) + ',%' -- comma added before the last %

如果有必要,请转换@param,并确保没有值周围的隐藏空格(rtrim或ltrim可用于删除它们)