我有一个包含电话号码和相应客户的表[CLIData]:
Number Customer
01234567890 GeoffLtd
01234567891 FredLtd
01234567892 1JimLtd
01234567893 21DaveLtd
01234567894 297AhmedLtd
01234567895 FrankLtd
以数字开头的客户是Group1的一部分,以字母开头的客户是Group2的一部分。
我希望创建一个将插入另一个表[CustomerData]的查询,如下所示:
Number Group
01234567890 Group2
01234567891 Group2
01234567892 Group1
01234567893 Group1
01234567894 Group1
01234567895 Group2
有人可以帮我解决这个问题吗?有没有一种简单的方法来查询第一个字符是数字的位置?
由于
答案 0 :(得分:0)
INSERT INTO CustomerData(Number, [Group])
SELECT Number,
CASE WHEN ISNUMERIC(SUBSTRING(Customer, 1, 1)) = 1
THEN 'GROUP1'
ELSE 'GROUP2'
END [GROUP]
FROM CLIData
答案 1 :(得分:0)
试试这个:
select
*
,gr = case when patindex('[0-9]%',customer)>0 THEN 'Group1' else 'Group2' end
from (
select '01234567890' as number,'GeoffLtd' as customer union all
select '01234567891','FredLtd' union all
select '01234567892','1JimLtd' union all
select '01234567893','21DaveLtd' union all
select '01234567894','297AhmedLtd' union all
select '01234567895','FrankLtd'
) x