簡體   English   中英

在SQL Server 2008中使用SQL從基於其他列的列中提取特定字符串

[英]Extracting a specific string from a column based on other column using SQL in SQL server 2008

我在表中有兩列。 兩者都是長度不同的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*

現在我想在輸出中連接顏色和國家名稱,如下

green,UNITED STATES
black,S AFRICA
red,INDIA

您可以從示例中提取國家/地區名稱:

select stuff(text, 1, charindex(': ', text + ': ') + 2, '') as country_name

您可以將其他任何內容串聯為:

select color + ',' + stuff(text, 1, charindex(': ', text + ': ') + 2, '') as country_name

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM