[英]Extract a substring from a text field
New to TSQL and SQL generally, please pardon if this is really basic: TSQL和SQL通常是新手,如果这确实很基础,请原谅:
I am working with a new-to-me-database that has ignored some best practices. 我正在使用一个新的数据库,该数据库忽略了一些最佳实践。 Relevant to this discussion, some data is stored in a generalized note field, including loyalty numbers. 与该讨论有关,一些数据包括忠诚度编号存储在广义注释字段中。 The good news is that the loyalty numbers are at least stored consistently within the note. 好消息是,忠诚度编号至少始终存储在注释中。
So, a simplified example from the note table might be: 因此,注释表中的一个简化示例可能是:
I have verified that every Loyalty Number is stored consistently ("Loyalty Number ####"), but obviously this is not ideal. 我已经验证了每个会员编号的存储方式都是一致的(“会员编号####”),但是显然这并不理想。 I want to extract the Loyalty Number for every primary key that has them, then create a new field that stores the Loyalty Number. 我想为每个拥有主号码的主键提取“忠诚度编号”,然后创建一个存储“忠诚度编号”的新字段。
What I'm having trouble with is the following: How do I run a query that will give me each primary key then, if there is a loyalty number return it, if not leave it null or say something like no result found. 我遇到的问题如下:我如何运行一个查询,该查询将为我提供每个主键,如果有忠诚度编号,请将其返回,如果不保留为空,或者说类似未找到结果的话。 Eg, turn the above into something like. 例如,将以上内容变为类似内容。
It's trivially easy to construct something like "select primary_key, note from note_table where note like '%Loyalty Number%', but that doesn't do the job of clipping down to just the loyalty number (and leaving out extraneous text). The uniformity of the data means I could probably do this in Excel, but I'm wondering if it's possible in TSQL. Thanks in advance for your help. 构造诸如“选择primary_key,从note_table中注释,其中注释如'%Loyalty Number%”这样的构造很容易,但这并不能简化为忠诚度数字(并省去多余的文本)。的数据意味着我可能可以在Excel中执行此操作,但是我想知道是否可以在TSQL中进行操作。在此先感谢您的帮助。
Give something like this a try using case
with substring
and charindex
: 给这样的一个尝试使用case
与substring
和charindex
:
select id,
case when note like '%Loyalty Number [0-9][0-9][0-9][0-9]%'
then 'Loyalty Number ' +
substring(note,
charindex('Loyalty Number', note) + Len('Loyalty Number ') + 1, 4)
end as Note
from note
The case
statement checks to see if Loyalty Number
exists in the data. case
语句检查数据中是否存在会员Loyalty Number
。 Substring
splits the note field using charindex
to find the starting position. Substring
使用charindex
拆分注释字段以查找起始位置。 This is hard coding a length of 4 characters for the loyalty number. 很难为会员编号编码4个字符的长度。 Given your comments, this should work. 鉴于您的意见,这应该可行。 If you have a dynamic number of characters, you'll need to modify this slightly. 如果您拥有动态数量的字符,则需要对此进行一些修改。
Building on @ segeddes answer, here's the rest of the code, that will update your new LoyaltyNumber
column. 在@ segeddes答案的基础上,这是其余代码,它将更新您的新LoyaltyNumber
列。
Working SQL Fiddle: http://sqlfiddle.com/#!3/36e46/8 有效的SQL Fiddle: http ://sqlfiddle.com/#!3 / 36e46 / 8
UPDATE note_table
SET LoyaltyNumber =
CASE
WHEN note LIKE '%Loyalty Number [0-9][0-9][0-9][0-9]%'
THEN SUBSTRING(note, CHARINDEX('Loyalty Number', note)
+ LEN('Loyalty Number ') + 1, 4)
ELSE 'Regular Customer'
END
FROM note_table
CREATE TABLE note_table (
id int identity(1,1),
Note VarChar(500),
LoyaltyNumber varchar(20)
)
Insert Into note_table(Note) Values
('Customer Since 2012. Loyalty Number 4747'),
('Loyalty Number 2209'),
('Loyalty Number 2234.Customer Since 2009'),
('Pending Order');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.