简体   繁体   English

从SQL的表列中提取2个字母的大写子字符串

[英]Extracting 2-letter capital substrings from table column in sql

I have an SQL table on users where particular accounts are tagged with country code (2 letter words in uppercase) while other substrings in the tags (all separated by commas) are either in lowercase or more than 2 letters long. 我有一个关于用户的SQL表,其中特定的帐户用国家代码标记(大写2个字母词),而标记中的其他子字符串(均用逗号分隔)要么小写,要么超过2个字母长。

In user table Eg: 在用户表Eg中:

id               User_tags
1                alu,US,ATD
2                GB,xx
3                ol,tuds,FR

Users 1,2 and 3 are tagged to countries US, GB and FR and I need to extract them from the user_tags column. 用户1,2和3被标记到美国,GB和FR国家,我需要从user_tags列中提取它们。 I understand that regex functions are needed but I am not able to make them work in an SQL query. 我知道需要使用正则表达式功能,但无法在SQL查询中使用它们。

Create a country code ref table and join to using the below. 创建国家代码参考表,并使用以下内容加入。 I don't have sqlserver open, so unable to double check syntax , but it should work. 我没有打开sqlserver,因此无法再次检查语法,但应该可以。

Select * 
From yourtable y left join refcountry r
On charindex (r.code+’,’,y.string+’,’)>0

Note this might be slow for a large dataset 请注意,这对于大型数据集可能很慢

If not sqlserver find the equiv function for charindex in your rdbms 如果不是,sqlserver在您的rdbms中找到charindex的等值函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM