我有一些JSON字符串,其中一些与以下字符串匹配:
{"name":"Example diagnosis 1","code":"111609001","table":"SNOMEDCT","addedby":"EDnurse","dateadded":1548080768169,"qualifier":[{"name":"Qualifier","value":"Confirmed Diagnosis","code":"410605003","prefix":"[C] "},{"name":"Left/Right","value":"Bilateral","code":"51440002","suffix":" - Bilateral"}],"prefix":"[C] ","suffix":" - Bilateral"},{"name":"Example diagnosis 2","code":"371162008","table":"SNOMEDCT","addedby":"EDnurse","dateadded":1548080778007,"qualifier":[{"name":"Qualifier","value":"Confirmed Diagnosis","code":"410605003","prefix":"[C] "},{"name":"Left/Right","value":"Bilateral","code":"51440002","suffix":" - Bilateral"}],"prefix":"[C] ","suffix":" - Bilateral"}
我建立了以下case语句,该语句部分起作用,并返回以下带有回车符和换行符的字符串,替换逗号:
{"name":"Example diagnosis 1","code":"111609001","table":"SNOMEDCT","addedby":"EDnurse","dateadded":1548080768169,"qualifier":[{"name":"Qualifier","value":"Confirmed Diagnosis","code":"410605003","prefix":"[C] "},{"name":"Left/Right","value":"Bilateral","code":"51440002","suffix":" - Bilateral"}],"prefix":"[C] ","suffix":" - Bilateral"}
{"name":"Example diagnosis 2","code":"371162008","table":"SNOMEDCT","addedby":"EDnurse","dateadded":1548080778007,"qualifier":[{"name":"Qualifier","value":"Confirmed Diagnosis","code":"410605003","prefix":"[C] "},{"name":"Left/Right","value":"Bilateral","code":"51440002","suffix":" - Bilateral"}],"prefix":"[C] ","suffix":" - Bilateral"}
这是我的CASE声明:
select
CASE
WHEN notevalue LIKE '%"suffix":" - Bilateral"},%'
THEN REPLACE(substring(LEFT(cast(notevalue as varchar(8000)),DATALENGTH(cast(notevalue as varchar(8000)))-1), 2, 8000),'"suffix":" - Bilateral"},','"suffix":" - Bilateral"}'+CHAR(13)+CHAR(10))
ELSE substring(LEFT(cast(notevalue as varchar(8000)),DATALENGTH(cast(notevalue as varchar(8000)))-1), 2, 8000)
END as test
from clinicalnotesdata
where notekey = 'ed diagnosis'
and visitid = '6948'
但是,我还需要它在我的CASE语句中添加其他2个实例,以捕获其他字符串(所有字符串都单独工作)。一旦我结合了该案例的3个场景,它就明显与第一个场景匹配并结束。但是有时字符串会匹配所有3种情况。
WHEN notevalue LIKE '%"prefix":"[[]C] "},%'
THEN REPLACE(substring(LEFT(cast(notevalue as varchar(8000)),DATALENGTH(cast(notevalue as varchar(8000)))-1), 2, 8000),'"prefix":"[C] "}],"prefix":"[C] "},','"prefix":"[C] "}],"prefix":"[C] "}'+CHAR(13)+CHAR(10))
WHEN notevalue LIKE '"%prefix":"[[]C] ","suffix":" - Right"},%'
THEN REPLACE(substring(LEFT(cast(notevalue as varchar(8000)),DATALENGTH(cast(notevalue as varchar(8000)))-1), 2, 8000),'"prefix":"[C] ","suffix":" - Right"},','"prefix":"[C] ","suffix":" - Right"}'+CHAR(13)+CHAR(10))
本质上,我需要用CHAR(13)+ CHAR(10)替换特定的逗号,以满足以上3条条件中的全部或部分条件。
有什么想法吗?谢谢。
答案 0 :(得分:0)
解决此问题的最佳方法是使用我称之为与APPLY进行别名的技巧;这是保持代码DRYer的一种方式(DRY =不要重复自己)。您可以像这样简化代码:
SELECT
CASE
WHEN CHARINDEX(a.stxt+',',t.notevalue) > 0 --WHEN t.notevalue LIKE '%"suffix":" - Bilateral"},%'
THEN REPLACE(a2.string,a.stxt+',',a.stxt+CHAR(13)+CHAR(10))
ELSE a2.string
END as test
FROM @clinicalnotesdata AS t
CROSS APPLY (VALUES(CAST(t.notevalue AS VARCHAR(8000)),
DATALENGTH(t.notevalue)-1, '"suffix":" - Bilateral"}')) AS a(nval,dl,stxt)
CROSS APPLY (VALUES(SUBSTRING(LEFT(a.nval,DATALENGTH(a.nval)-1), 2, 8000))) AS a2(string)
WHERE t.notekey = 'ed diagnosis'
AND t.visitid = '6948';
此代码将执行完全相同的操作,并且如您所见,它更易于理解和重用。例如,CAST(t.notevalue AS VARCHAR(8000)
的所有四个实例已被a.nval
替换,DATALENGTH(t.notevalue)-1
的两个实例已被a.dl替换。 substring(LEFT(cast(notevalue as varchar(8000)),DATALENGTH(cast(notevalue as varchar(8000)))-1), 2, 8000)
的两个实例现已删除。
如您所见,我正在使用VALUES构造函数和APPLY创建内联函数,这些内联函数只是对重复的代码块进行别名。相信我,像这样开始编写代码,做起来会容易得多,否则会导致您费劲。