解析字符串并将值与现有列进行比较

时间:2018-02-16 20:17:45

标签: sql sql-server

我的下表中标有“Remark”的字符串需要解析。需要从TotalBookedFare和Remark列中比较突出显示的票价。唯一的问题是我需要在Remark列下比较的值位于字符串的中间。我试图解析字符串,但我无法弄明白。我正在使用SQL Server 2008.正如您所看到的那样,第一行不匹配,而其他三行匹配。

理想情况下,我想将一个字符串“Remark”转换为下面列出的5列,这样我就可以将TotalBookedFare与“New”列进行比较.dionbennett

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为这应该有用

select substring(
remark, --string base
charindex ('/', 'xyz/57.77usd/zyx') + 1, 
--starting position is location one to the right of first instance of / character (5)
charindex ('u', 'xyz/57.77usd/zyx', charindex ('/', 'xyz/57.77usd/zyx')) - charindex ('/', 'xyz/57.77usd/zyx') - 1
--length is the location of the first instance of the u character
--starting from the location of first instance of the / character (10)
--then subtracted by the location of the first instance of the / character (4) 
--and then an additional 1 resulting in the length of the string to be extracted (5)
)

我放在那里的字符串只是一个更具体的例子,如果你用备注替换它,它应该提取每一行的子字符串。您甚至可以通过一些复制/粘贴来修改它,以获取您正在寻找的每个列。