SQL 服务器:替换为 NULL
SQL Server: replace by NULL
SELECT REPLACE('hello', 'e', NULL)
-- returns NULL
SELECT REPLACE('hello', '5', NULL)
-- also returns NULL -- here I'd expect it does nothing since there is no '5' in 'hello'
documentation 明确指出:
Returns NULL if any one of the arguments is NULL.
所以行为得到了解释。
是否有解决方法,意思是如果在字符串中找到模式,例如 hello 中的 'e' ;发出 NULL 值?
似乎 REPLACE
不是您想要的。一种方法是使用 CASE
和 LIKE
:
SELECT CASE WHEN 'hello' NOT LIKE '%5%' THEN 'hello' END;
或者您可以使用 CHARINDEX
:
SELECT CASE WHEN CHARINDEX('5','hello') = 0 THEN 'hello' END;
SELECT REPLACE('hello', 'e', NULL)
-- returns NULL
SELECT REPLACE('hello', '5', NULL)
-- also returns NULL -- here I'd expect it does nothing since there is no '5' in 'hello'
documentation 明确指出:
Returns NULL if any one of the arguments is NULL.
所以行为得到了解释。
是否有解决方法,意思是如果在字符串中找到模式,例如 hello 中的 'e' ;发出 NULL 值?
似乎 REPLACE
不是您想要的。一种方法是使用 CASE
和 LIKE
:
SELECT CASE WHEN 'hello' NOT LIKE '%5%' THEN 'hello' END;
或者您可以使用 CHARINDEX
:
SELECT CASE WHEN CHARINDEX('5','hello') = 0 THEN 'hello' END;