我正在运行一个简单的查询:
Select "ID", "Original_ID" from table;
这让我知道了:
ID | Original_ID
56917659 | #56906230 Captain
56781961 | #56754164 Shipment Management
56794542 | #56620028 Team Member
56655028 | #50817965 Commercial Owner
56258156 | Process Expert
55780912 | Process Expert
然后我可以使用它:
select "ID", substr("Original_ID",2,8)as "Original_ID" from table;
ID | Original_ID
56917659 | 56906230
56781961 | 56754164
56794542 | 56620028
56655028 | 50817965
56258156 | rocess E
55780912 | rocess E
我需要的是第3列" True_ID"它将substr(" Original_ID",2,8)显示为" True_ID"但只有" Original_ID"以'#'开头如果不是只显示" ID"中的值。我正在使用dashDB。
答案 0 :(得分:1)
使用CASE WHEN
来确定Original_ID
的第一个字符是否为#
:
select
ID
, substr(Original_ID,2,8) as Original_ID
, CASE WHEN substr(Original_ID, 1, 1) = '#'
THEN substr(Original_ID, 2, 8)
ELSE 'ID' END as True_ID
from table;
答案 1 :(得分:1)
您可以在CASE中完成LEFT功能:
SELECT
ID,
SUBSTR(Original_ID,2,8) as Original_ID,
CASE WHEN LEFT(Original_ID, 1) = '#'
THEN SUBSTR(Original_ID, 2, 8)
ELSE ID
END AS True_ID
FROM table;