开发者

How create a String value for represent some values in select results?

开发者 https://www.devze.com 2023-04-03 16:10 出处:网络
In my Select i have this result: name | type | -----+------+ booo | A| xooo | B| dooo | A| I need a way in SQL SERVER to replace A for \"Abstract\" and开发者_开发技巧 B for \"Ball\"

In my Select i have this result:

name | type |
-----+------+
booo | A    |
xooo | B    |
dooo | A    |

I need a way in SQL SERVER to replace A for "Abstract" and开发者_开发技巧 B for "Ball" Thanks, Celso


Create another table with this mapping and use joins.

type | long_name |
-----+-----------+
  A  | Abstract  |
  B  | Ball      |
  C  | Cat       |


WITH Strings(type,description)
AS
(
SELECT 'A', 'Abstract' UNION ALL
SELECT 'B', 'Ball' 
)
SELECT y.foo, s.description
FROM YourTable y
JOIN Strings S ON S.type = y.type


You also might look at the CASE function in SQL Server, depending on what you're trying to do.

http://msdn.microsoft.com/en-us/library/ms181765.aspx


select name as "name",

   (case type
    when 'A' then 'Abstract'
    when 'B' then 'Ball'
    end) as "Type"

from MyTable

order by name

0

精彩评论

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

关注公众号