使用 Charindex 获取字符左侧的数据
Using Charindex to get data left of a character
我有一个名为主题的字段,数据如下所示:
ALJ Diane Davis - WCF
我希望我的最终结果是:
ALJ Diane Davis
我正在尝试获取“-”左侧的所有数据
我正在使用 Advantage SQL,我也是新手。
下面使用 RIGHT
函数的示例将我的所有内容都移到了右边,如果我想要的话,它可以工作,但我并不总是知道我希望我的数据结束的方式的确切字符数像。
提前致谢
left(appts.subject,charindex('-',appts.subject)
left(appts.subject,char('-',appts.subject)-1)
right(rtrim(appts.subject),6)
这不行吗?
left(appts.subject, charindex('-', appts.subject) - 1)
如果因为不是所有受试者都有 -
而失败,那么:
left(appts.subject, charindex('-', appts.subject + '-') - 1)
以上在 Sybase 中有效。在 Advantage SQL 中,我认为您需要 location
:
left(appts.subject, locate('-', appts.subject) - 1)
这应该会给你结果。 Locate 是 Adavantage-Sql 中的功能。你可以使用这个 link
Function- LOCATE( str1, str2[, start] )
Return integer location
(1-based) of str1 in str2, with optional start starting point.
If str1 is not found in str2, 0 is returned.
SELECT SUBSTRING('ALJ Diane Davis - WCF', 1, locate('-', 'ALJ Diane Davis - WCF') - 1)
我有一个名为主题的字段,数据如下所示:
ALJ Diane Davis - WCF
我希望我的最终结果是:
ALJ Diane Davis
我正在尝试获取“-”左侧的所有数据 我正在使用 Advantage SQL,我也是新手。
下面使用 RIGHT
函数的示例将我的所有内容都移到了右边,如果我想要的话,它可以工作,但我并不总是知道我希望我的数据结束的方式的确切字符数像。
提前致谢
left(appts.subject,charindex('-',appts.subject)
left(appts.subject,char('-',appts.subject)-1)
right(rtrim(appts.subject),6)
这不行吗?
left(appts.subject, charindex('-', appts.subject) - 1)
如果因为不是所有受试者都有 -
而失败,那么:
left(appts.subject, charindex('-', appts.subject + '-') - 1)
以上在 Sybase 中有效。在 Advantage SQL 中,我认为您需要 location
:
left(appts.subject, locate('-', appts.subject) - 1)
这应该会给你结果。 Locate 是 Adavantage-Sql 中的功能。你可以使用这个 link
Function- LOCATE( str1, str2[, start] )
Return integer location (1-based) of str1 in str2, with optional start starting point. If str1 is not found in str2, 0 is returned.
SELECT SUBSTRING('ALJ Diane Davis - WCF', 1, locate('-', 'ALJ Diane Davis - WCF') - 1)