我有以下代码来处理电话号码,例如澳大利亚国家代码+61
,61
,001161
等。我遇到的问题我无法插入任何{{ 1}}声明在CASE
它说案例表达式只能嵌套到10级
如何简化此TSQL以便我可以添加更多CASE?
CASE WHEN LEFT(@BPartyNo, 4) = '+610'
答案 0 :(得分:14)
它们根本不需要嵌套:
SELECT @Return = CASE WHEN LEFT(@BPartyNo, 4) = '+610' THEN SUBSTRING('0' + SUBSTRING(@BPartyNo, 5, LEN(@BPartyNo)), 11, LEN( '0' + SUBSTRING(@BPartyNo, 5, LEN(@BPartyNo)) ) )
WHEN LEFT(@BPartyNo, 3) = '+61' THEN SUBSTRING('0' + SUBSTRING(@BPartyNo, 4, LEN(@BPartyNo)), 11, LEN( '0' + SUBSTRING(@BPartyNo, 4, LEN(@BPartyNo)) ) )
WHEN LEFT(@BPartyNo, 2) = '61' THEN SUBSTRING('0' + SUBSTRING(@BPartyNo, 3, LEN(@BPartyNo)), 11, LEN( '0' + SUBSTRING(@BPartyNo, 3, LEN(@BPartyNo)) ) )
WHEN LEFT(@BPartyNo, 6) = '001161' THEN SUBSTRING('0' + SUBSTRING(@BPartyNo, 7, LEN(@BPartyNo)), 11, LEN( '0' + SUBSTRING(@BPartyNo, 7, LEN(@BPartyNo)) ) )
WHEN ( LEFT(@BPartyNo,2) = '01' AND LEN(@BPartyNo) > 10 ) THEN SUBSTRING(@BPartyNo, 11, LEN(@BPartyNo))
WHEN ( LEFT(@BPartyNo,2) = '02' AND LEN(@BPartyNo) > 10 ) THEN SUBSTRING(@BPartyNo, 11, LEN(@BPartyNo))
WHEN ( LEFT(@BPartyNo,2) = '03' AND LEN(@BPartyNo) > 10 ) THEN SUBSTRING(@BPartyNo, 11, LEN(@BPartyNo))
WHEN ( LEFT(@BPartyNo,2) = '04' AND LEN(@BPartyNo) > 10 ) THEN SUBSTRING(@BPartyNo, 11, LEN(@BPartyNo))
WHEN ( LEFT(@BPartyNo,2) = '07' AND LEN(@BPartyNo) > 10 ) THEN SUBSTRING(@BPartyNo, 11, LEN(@BPartyNo))
WHEN ( LEFT(@BPartyNo,2) = '08' AND LEN(@BPartyNo) > 10 ) THEN SUBSTRING(@BPartyNo, 11, LEN(@BPartyNo))
ELSE ''
END
就个人而言,我会重新构建您的代码,以便确保在输入时清理数据,而不是现在尝试清理它(当它显然已经太晚了......)。或者至少用你的客户语言进行转换(即在调用这个sproc中),这有希望比T-SQL更适合字符串操作的任务。
答案 1 :(得分:10)
您不需要嵌套case语句,可以有很多WHEN ... THEN
CASE
WHEN @x = 1 THEN 1
WHEN @x = 2 THEN 2
WHEN @x = 3 THEN 3
ELSE 4
END
答案 2 :(得分:0)
这是嵌套案例场景的解决方法,不推荐使用。在Coalesce中有前9个CASES,然后用else null结束它,并将下一个案例放在下一个块中。
COALESCE ( (CASE WHEN TRY1 = 1 THEN TRY1
ELSE CASE WHEN TRY2 = 1 THEN TRY2
ELSE CASE WHEN TRY3 = 1 THEN TRY3
ELSE NULL END END
END),
CASE WHEN TRY11 = 1 THEN TRY11
ELSE CASE WHEN TRY12 = 1 THEN TRY12
ELSE CASE WHEN TRY13 = 1 THEN TRY13
ELSE NULL END END END
),0) AS MyValue
答案 3 :(得分:0)
尽管SQL Server v17允许使用10个以上的嵌套case表达式,但以前的版本似乎不允许它们。当我从v17服务器将一个大写的语句粘贴回v16 SQL Server环境时,我发现了困难的方法。 我的解决方案是建立一个临时表或虚拟表,并在子查询中使用它。例如,我必须替换以下代码:
if object_id( 'tempdb..#ILEDocType' ) is not null
drop table #ILEDocType ;
create table #ILEDocType
( [Code] [int]
, [Name] [varchar](50)
)
;
insert into #ILEDocType
values
( 1 , 'Sales Shipment' )
, ( 2 , 'Sales Invoice' )
, ( 3 , 'Sales Return Receipt' )
, ( 4 , 'Sales Credit Memo' )
, ( 5 , 'Purchase Receipt' )
, ( 6 , 'Purchase Invoice' )
, ( 8 , 'Purchase Return Shipment' )
, ( 9 , 'Purchase Credit Memo' )
, ( 10 , 'Transfer Shipment' )
, ( 11 , 'Transfer Receipt' )
, ( 12 , 'Service Shipment' )
, ( 13 , 'Service Invoice' )
, ( 14 , 'Service Credit Memo' )
, ( 15 , 'Posted Assembly' )
--else 'Other'
;
select [Document Type Desc] =
isnull( ( select t.[Name]
from #ILEDocType as t
where t.[Code] = ile.[Document Type] ) , 'Other' )
from dbo.[Item Ledger Entry] as ile
;
.. for the following code that gave the subject error:
select
( case ile.[Document Type]
when 1 then 'Sales Shipment'
when 2 then 'Sales Invoice'
when 3 then 'Sales Return Receipt'
when 4 then 'Sales Credit Memo'
when 5 then 'Purchase Receipt'
when 6 then 'Purchase Invoice'
when 8 then 'Purchase Return Shipment'
when 9 then 'Purchase Credit Memo'
when 10 then 'Transfer Shipment'
when 11 then 'Transfer Receipt'
when 12 then 'Service Shipment'
when 13 then 'Service Invoice'
when 14 then 'Service Credit Memo'
when 15 then 'Posted Assembly'
else 'Other' end ) as [Document Type Desc]
from dbo.[Item Ledger Entry] as ile
;
答案 4 :(得分:-1)
我为一个视图编写了12个嵌套的IIF语句,该视图错误为“嵌套案例错误”。我正在使用12列(1月-12月)保存数值。视图是将每个月转换为一行。然后,我意识到我可以将嵌套的IIF分成两组,每组六个,然后将它们加在一起!奏效了!
select AP.Year, AP.Period, RPA.Company, RPA.Contract, RPA.Description, RPA.PM, RPA.ProjectManager,
IIF(AP.Period=1, RPA.JanNetBilled, IIF(AP.Period=2, RPA.FebNetBilled, IIF(AP.Period=3, RPA.MarNetBilled, IIF(AP.Period=4, RPA.AprNetBilled, IIF(AP.Period=5, RPA.MayNetBilled,
IIF(AP.Period=6, RPA.JunNetBilled, 0)))))) +
IIF(AP.Period=7, RPA.JulNetBilled, IIF(AP.Period=8, RPA.AugNetBilled, IIF(AP.Period=9, RPA.SepNetBilled, IIF(AP.Period=10, RPA.OctNetBilled,
IIF(AP.Period=11, RPA.NovNetBilled, IIF(AP.Period=12, RPA.DecNetBilled, 0)))))) as BilledAmt
from AccountPeriod AP
INNER JOIN REVENUE_PROJECTION_ANALYSIS RPA ON RPA.YEAR = AP.Year