[英]Extracting a specific string from a column based on other column using SQL in SQL server 2008
I have two columns in a table. 我在表中有两列。 Both are
nvarchar
of different lengths, let's say first columns contains the color (representing a country) and other column contains the country name with other text eg 两者都是长度不同的
nvarchar
,假设第一列包含颜色(代表一个国家),另一列包含国家名称和其他文本,例如
Color text
green choosen country: UNITED STATES *some text no fixed length upto 1000 chars*
black chossen country: S AFRICA *some text no fixed length upto 1000 chars*
red choosen country: INDIA *some text no fixed length upto 1000 chars*
Now I want color and the country name concatenated in my output, as 现在我想在输出中连接颜色和国家名称,如下
green,UNITED STATES
black,S AFRICA
red,INDIA
You can extract the country name as from your example: 您可以从示例中提取国家/地区名称:
select stuff(text, 1, charindex(': ', text + ': ') + 2, '') as country_name
You can concatenate anything else you want as: 您可以将其他任何内容串联为:
select color + ',' + stuff(text, 1, charindex(': ', text + ': ') + 2, '') as country_name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.