【问题标题】:SQL Query- Case & SubstrSQL 查询 - 案例和子字符串
【发布时间】:2018-12-04 07:42:20
【问题描述】:

我有一个使用 substr 函数得到的列,并且该列有字母。如何将字母转换为单词?

select orderid,
       substr(ordernumber,10,1) as Process
from Table1,
where XXXXXX

如果Process列中有“M”,则应将其替换为“Maintenance”,如果是“O”,则应为“Operations”。

Process ---> Process
M            maintenance
O            Operations

有人可以帮我如何更改 substr 语句来更新列吗?

【问题讨论】:

    标签: sql case substr


    【解决方案1】:

    您可以使用case 表达式:

    SELECT orderid,
           CASE SUBSTR(ordernumber, 10, 1) WHEN 'M' THEN 'maintenance'
                                           WHEN 'O' THEN 'Operations'
           END AS Process
    FROM   table1
    

    【讨论】:

      【解决方案2】:

      使用带有case 表达式的更新语句:

      update table 
           set Process = (case substr(ordernumber, 10, 1) 
                               when 'M' then 'maintenance'
                               when 'O' then 'Operations'
                          end)
      where substr(ordernumber, 10, 1) in ('M', 'O');
      

      【讨论】:

        猜你喜欢
        • 2014-11-10
        • 2017-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-05
        • 2021-04-14
        • 1970-01-01
        相关资源
        最近更新 更多