问题描述
我有一个名为主题的字段,数据如下所示:
I have a field called subjects and the data looks like this:
ALJ Diane Davis - WCF
我希望我的最终结果是:
I want my end result to be:
ALJ Diane Davis
我正在尝试将所有数据放在-"的左侧我正在使用 Advantage SQL,我也是新手.
I am trying to get all the data to left of the "-"I am using Advantage SQL which I am new too.
下面使用 RIGHT
函数的示例让我一切都正确最终像.
The example below using the RIGHT
function gets me everything to the right which works if i wanted that, but i dont always know the exact number of characters for the way that i am wanting my data to end up like.
提前致谢
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)
如果这失败是因为并非所有科目都有 -
,那么:
If this fails because not all subjects have a -
, then:
left(appts.subject, charindex('-', appts.subject + '-') - 1)
以上在 Sybase 中有效.在 Advantage SQL 中,我认为您需要 location
:
The above works in Sybase. In Advantage SQL, I think you need location
:
left(appts.subject, locate('-', appts.subject) - 1)
这篇关于使用 Charindex 获取字符左侧的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!