我有以下SUBSTRING命令:
RTRIM(LTRIM(SUBSTRING(FileName, CHARINDEX('-', FileName, 0)+1, CHARINDEX('-', FileName, 0)-7 )))
并拥有如下文件路径:
"C:\James\AAA(9) - V0001514 MR J BLOGGS.pdf" returns "V0001514"
"C:\James\test(100) - V0001585 MRS A SMITH.pdf returns "V0001585 MR"
第一个返回正确的结果,但第二个返回额外的字符。有没有人看到确保V *号总是正确返回的方法?
答案 0 :(得分:2)
问题是您已将数据长度修正了-7
你可以尝试:
SELECT SUBSTRING (@DATA, CHARINDEX('-', @DATA)+1,
CHARINDEX(' ', LTRIM(SUBSTRING(@DATA, CHARINDEX('-', @DATA)+1, LEN(@DATA)))))
结果:
"C:\James\AAA(9) - V0001514 MR J BLOGGS.pdf" returns "V0001514"
"C:\James\test(100) - V0001585 MRS A SMITH.pdf returns "V0001585"
答案 1 :(得分:2)
您可以使用此公式。
RTRIM(LTRIM(SUBSTRING(FileName, CHARINDEX('-', FileName, 0)+1, 7 )))
样品:
DECLARE @FileName1 VARCHAR(255) = 'C:\James\AAA(9) - V0001514 MR J BLOGGS.pdf" returns "V0001514'
DECLARE @FileName2 VARCHAR(255) = 'C:\James\test(100) - V0001585 MRS A SMITH.pdf returns "V0001585 MR'
SELECT
RTRIM(LTRIM(SUBSTRING(@FileName1, CHARINDEX('-', @FileName1, 0)+1, 7 ))) F1,
RTRIM(LTRIM(SUBSTRING(@FileName2, CHARINDEX('-', @FileName2, 0)+1, 7 ))) F2
结果:
F1 F2
------- -------
V00015 V00015
答案 2 :(得分:0)
尝试以下操作,希望它对您有所帮助:
DECLARE @FileName VARCHAR(100)='C:\James\AAA(9) - V0001514 MR J BLOGGS.pdf'
SELECT @FileName=SUBSTRING(@FileName,CHARINDEX('-',@FileName)+2,LEN(@FileName)-CHARINDEX('-',@FileName))
SELECT LEFT(@FileName,CHARINDEX(' ',@FileName)-1)