jef*_*eff 2 sql case oracle-sqldeveloper
我有一个这样的代码:
SELECT
'"35933-14",' ||
'"' || us_1.gr_UniqueName || '",' ||
'"' || (CASE WHEN us_1.mls0_PrimaryString = '' THEN 'This is empty'
WHEN CAST(Length(us_1.mls0_PrimaryString) AS INT) < 4 THEN ('Less than 4: '|| SUBSTR(us_1.mls0_PrimaryString,1,10000))
ELSE SUBSTR(us_1.mls0_PrimaryString,1,10000) END) || '",' ||
'"",' ||
'"",' ||
'""'
FROM
us_GroupTab us_1
WHERE (us_1.gr_Active = 1)
AND (us_1.gr_PurgeState = 0)
AND (us_1.gr_PartitionNumber = 0)
AND (us_1.gr_UniqueName IN ('US_HARDWARE_1', 'US_HARDWARE_2','GROUP_NULL'));
Run Code Online (Sandbox Code Playgroud)
基本上问题是并非所有空字符串都被处理,一些用户只输入第一个 case 语句不处理的多个空格。有什么办法可以做到这一点,我尝试过使用 TRIM 功能,但它不起作用。
谢谢!
空字符串与 Oracle 中的 null 相同,并且不能将任何内容与 null 进行比较。您需要使用is null而不是= nullor = ''。
CASE WHEN TRIM(us_1.mls0_PrimaryString) IS null THEN 'This is empty' ...
Run Code Online (Sandbox Code Playgroud)
您也不需要cast检查长度int。12c 之前的a 的最大长度varchar2是 4000 个字符,因此在您的substr. 事实上,第一个substr无论如何都不会做任何事情,因为你已经知道长度小于 4。
如果您想在检查之前删除新行和回车符 - 这也许是您应该在客户端执行的操作,除非您也想存储它们 - 那么您可以先替换它们:
CASE WHEN TRIM(REPLACE(REPLACE(us_1.mls0_PrimaryString, CHR(10)), CHR(13))) IS null
THEN ...
Run Code Online (Sandbox Code Playgroud)
或者更一般地删除所有会捕获制表符等的空格:
CASE WHEN REGEXP_REPLACE(us_1.mls0_PrimaryString, '[[:space:]]') IS NULL THEN ...
Run Code Online (Sandbox Code Playgroud)
或者:
CASE WHEN REGEXP_LIKE(us_1.mls0_PrimaryString, '^[[:space:]]*$') THEN ...
Run Code Online (Sandbox Code Playgroud)
请注意,不需要单独的trimwith regexp_replace。