2017-05-21 44 views
0

我在表中有兩列。兩者是不同長度的nvarchar,讓我們說第一個欄包含(代表國家)的顏色和其他列包含與其他文本如國名在SQL Server 2008中使用SQL根據其他列提取特定字符串

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 
+0

只要選擇'text',只需用空字符串替換'choosen country:'即可。 – DigiFriend

+0

是固定的文本列格式嗎?即「選擇的國家:」總是存在的? –

+0

hi Vkp,是選擇的國家總是存在 – Vicky

回答

0

可以提取國家的名字從你的例子:

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

您可以連接其他任何你想要爲:

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

HI Gordon,我已經改變了這個問題,有點國家名稱後面還有一些文字 – Vicky

相關問題