我想在当前有两列的表中创建新列:“ dx”和“ dxlength”。根据“ dx”中字符的长度,新列将是“ dx”列中字符的子字符串,该长度可在“ dxlength”列中找到。
例如,如果dxlength为> = 3,那么我想将前三个字符放入名为“ first3”的列中。
我正在尝试使用case when语句,但是遇到一些错误。我需要更改我的代码吗?
我尝试使用case when语句。请参见下面的示例代码。
select
dx, dxlength,
case when dxlength >= 3 then substring('dx', 1, 3) as first3,
case when dxlength >= 4 then substring('dx', 1, 4) as first4,
case when dxlength >= 5 then substring('dx', 1, 5) as first5,
case when dxlength >= 5 then substring('dx', 5, 1) as fifth,
case when dxlength >= 6 then substring('dx', 6, 1) as sixth,
case when dxlength < 7 then '!' as seventh,
case when dxlength = 7 then substring('dx', 7, 1) as seventh
into
table2
from
table1
该表将有7个附加列,其中包含基于“ dx”中字符数的“ dx”列中字符的子字符串,可以在“ dx length”列中找到。