为此方案建议查询

slwdgvem  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(363)

在SQLServer中,“我在数据库表:mastersupport中有一个字段(d1)为101、102、103。
我有这个问题

select right(D1, R) 
from mastersupport.

它将返回像1,2,3这样的结果
但我的要求是,我只想把结果显示为a,b,c,而不是1,2,3“。请提出一个问题。
我试着用下面的,但得到语法错误。

SELECT DISTINCT
    REPLACE(REPLACE((RIGHT(D1, 1)), '1' , ‘A’), '2', ‘B’, ) AS ExtractString 
FROM
    master_support;

将结果导出为a、b、c的任何其他查询。。。。。。

xggvc2p6

xggvc2p61#

你可以使用 case 表达式:

select case right(d1, 1)
    when '1' then 'A'
    when '2' then 'B'
    when '3' then 'C'
    ...
end as extract_string
from master_support

注意,如果 d1 属于数字数据类型,使用算术似乎是一种更自然的方法:

select case d1 % 10
    when 1 then 'A'
    when 2 then 'B'
    when 3 then 'C'
    ...
end extract_string
from master_support

相关问题