我正在使用以下查询从表中解析字符串。
SELECT
CASE
WHEN jobdata LIKE '%DocFtpPassword%'
THEN SUBSTRING(jobdata,
CHARINDEX('DefColId=', jobdata) + 9,
CHARINDEX('|DocFtpPassword=', jobdata) - CHARINDEX('DefColId', jobdata) - 9)
ELSE 'No Import Library'
END AS 'Import_Library'
FROM
jobscompleted
然后我要做的是获取每一行并通过另一个表提取另一个值。我知道我可以将同一条语句写两次,如
SELECT
CASE
WHEN jc.jobdata LIKE '%DocFtpPassword%'
THEN SUBSTRING(jc.jobdata,
CHARINDEX('DefColId=', jc.jobdata) + 9,
CHARINDEX('|DocFtpPassword=', jc.jobdata) - CHARINDEX('DefColId', jc.jobdata) - 9)
ELSE NULL
END AS 'Import_Library',
c.collectionname
FROM
jobscompleted jc
JOIN
collection c ON CASE
WHEN jc.jobdata LIKE '%DocFtpPassword%'
THEN SUBSTRING(jc.jobdata,
CHARINDEX('DefColId=', jc.jobdata) + 9,
CHARINDEX('|DocFtpPassword=', jc.jobdata) - CHARINDEX('DefColId', jc.jobdata) - 9)
ELSE NULL
END = c.collectionid
但这显然是多余的。有没有一种方法可以更简洁,更易读,并且请记住,我对存储过程或变量一无所知(尽管总有一段不错的学习时间)。
答案 0 :(得分:1)
不确定这是否是您要的。如果没有,请提供有关该问题的更多详细信息。
SELECT * FROM
(SELECT
CASE
WHEN jobdata LIKE '%DocFtpPassword%'
THEN SUBSTRING(jobdata,
CHARINDEX('DefColId=', jobdata) + 9,
CHARINDEX('|DocFtpPassword=', jobdata) - CHARINDEX('DefColId', jobdata) - 9)
ELSE 'No Import Library'
END AS 'Import_Library'
FROM
jobscompleted ) AS jc
INNER JOIN
collection c ON jc.Import_Library = c.collectionid