开发者

Set Values in column B based on values in Column A in SQL2005?

开发者 https://www.devze.com 2023-02-15 03:53 出处:网络
I\'m trying to take an existing column and parse each row for certain words in a string, i.e. Sheet, Page, Card, and based on that word (only one instance of one of these words is in a row) populate a

I'm trying to take an existing column and parse each row for certain words in a string, i.e. Sheet, Page, Card, and based on that word (only one instance of one of these words is in a row) populate a new column in the same table with a value. If it did not find any of those words, leave Column B blank.

I.E. Column A contains the word "Sheet", populate Column B with the letter "S"

So the table would be something like:

Column A  Column B
Sheet        S
Page         开发者_StackOverflowP
Card         C

Any help would be appreciated!


UPDATE YourTable
    SET ColumnB = CASE WHEN ColumnA LIKE '%Sheet%' THEN 'S'
                       WHEN ColumnA LIKE '%Page%' THEN 'P'
                       WHEN ColumnA LIKE '%Card%' THEN 'C'
                       ELSE NULL /* or '' if you prefer */
                  END


;WITH mappings (ColumnA, ColumnB) AS
(
 SELECT 'Sheet','S' UNION ALL
 SELECT 'Page','P' UNION ALL
 SELECT 'Card','C' 
)
UPDATE y
SET y.ColumnB= m.ColumnB
FROM YourTable y
JOIN mappings m ON y.ColumnA LIKE '%' + m.ColumnA + '%'


update myTable 
set columnB = substring(columnA, 1, 1) 
0

精彩评论

暂无评论...
验证码 换一张
取 消