SQL Parse字符串以获取特定值

时间:2015-10-07 15:11:29

标签: sql

嗨,我在列

中有这个值
11-1-11111-349b65eda2-4f8e-413d-b76a-6a2c13d6e494OctWed0800422015-request-response.xml

我将如何使用SQL解析此内容:

11-1-11111-349b65eda2-4f8e-413d-b76a-6a2c13d6e494OctWed0800422015

1 个答案:

答案 0 :(得分:1)

在SQL-Server中,如果右侧始终为-request-response.xml

,则可以使用此方法
SELECT FileName,
       Leftpart = LEFT(FileName,CHARINDEX('-request-response.xml',FileName)-1)
FROM MyTable

Demo

这更加安全无虞:

WITH CTE AS
(
   SELECT FileName,
          Length = CHARINDEX('-request-response.xml',FileName) -1
    FROM MyTable
)
SELECT LeftPart = CASE WHEN Length < 0 THEN NULL 
                   ELSE LEFT(FileName, Length) END
FROM CTE