TSQL CONVERT + CASE函数

时间:2017-10-06 01:02:33

标签: sql sql-server-2012

我在下面的查询中遇到了问题。当字段留空时,我使用大小写输入值--。我可以将--的值更新为0,但我仍然会在查询执行时收到错误消息,我怀疑它是因为添加无法完成null / blank值。

我在UPDATE语句中遇到同样的问题。

INSERT:

INSERT INTO AME_GridF18
(SubmissionID,TS_Added,ProjectNumber,ProjectName,TypeofWork,Mile,Toll,Park,MonRT,MonOT,TuesRT,TuesOT,WedsRT,WedsOT,ThursRT,ThursOT,FriRT,FriOT,Saturday,Sunday,Total)
VALUES
(
'[querystring:SubmissionID]'
,'[DateTime:Now]'
,case when '[ProjectNumber]' = '' then '--' else '[ProjectNumber]' end
,case when '[ProjectName]' = '' then '--' else '[ProjectName]' end
,case when '[TypeofWork]' = '' then '--' else '[TypeofWork]' end
,case when '[Mile]' = '' then '--' else '[Mile]' end
,case when '[Toll]' = '' then '--' else '[Toll]' end
,case when '[Park]' = '' then '--' else '[Park]' end
,case when '[MonRT]' = '' then '--' else '[MonRT]' end
,case when '[MonOT]' = '' then '--' else '[MonOT]' end
,case when '[TuesRT]' = '' then '--' else '[TuesRT]' end
,case when '[TuesOT]' = '' then '--' else '[TuesOT]' end
,case when '[WedsRT]' = '' then '--' else '[WedsRT]' end
,case when '[WedsOT]' = '' then '--' else '[WedsOT]' end
,case when '[ThursRT]' = '' then '--' else '[ThursRT]' end
,case when '[ThursOT]' = '' then '--' else '[ThursOT]' end
,case when '[FriRT]' = '' then '--' else '[FriRT]' end
,case when '[FriOT]' = '' then '--' else '[FriOT]' end
,case when '[Saturday]' = '' then '--' else '[Saturday]' end
,case when '[Sunday]' = '' then '--' else '[Sunday]' end
,CONVERT(Varchar, ([MonRT] + [MonOT] + [TuesRT] + [TuesOT] + [WedsRT] + [WedsOT] + [ThursRT] + [ThursOT] + [FriRT] + [FriOT] + [Saturday] + [Sunday])
))

更新:

UPDATE AME_GridF18
SET
SubmissionID = '[querystring:SubmissionID]',
TS_Added = '[DateTime:Now]',
ProjectNumber = '[ProjectNumber]',
ProjectName = '[ProjectName]',
TypeofWork = '[TypeofWork]',
Mile = '[Mile]',
Toll = '[Toll]',
Park = '[Park]',
MonRT = '[MonRT]',
MonOT = '[MonOT]',
TuesRT = '[TuesRT]',
TuesOT = '[TuesOT]',
WedsRT = '[WedsRT]',
WedsOT = '[WedsOT]',
ThursRT = '[ThursRT]',
ThursOT = '[ThursOT]',
FriRT = '[FriRT]',
FriOT = '[FriOT]',
Saturday = '[Saturday]',
Sunday = '[Sunday]',
Total = CONVERT(Varchar, ([MonRT] + [MonOT] + [TuesRT] + [TuesOT] + [WedsRT] + [WedsOT] + [ThursRT] + [ThursOT] + [FriRT] + [FriOT] + [Saturday] + [Sunday]))
WHERE ID = [ID]

2 个答案:

答案 0 :(得分:1)

也许你正试图这样做?

DECLARE
@SubmissionID INT
,@TS_Added SMALLDATETIME
,@ProjectNumber VARCHAR(MAX)
,@ProjectName VARCHAR(MAX)
,@TypeofWork VARCHAR(MAX)
,@Mile FLOAT
,@Toll FLOAT
,@Park FLOAT
,@MonRT FLOAT
,@MonOT FLOAT
,@TuesRT FLOAT
,@TuesOT FLOAT
,@WedsRT FLOAT
,@WedsOT FLOAT
,@ThursRT FLOAT
,@ThursOT FLOAT
,@FriRT FLOAT
,@FriOT FLOAT
,@Saturday FLOAT
,@Sunday FLOAT
,@Total FLOAT

INSERT INTO AME_GridF18
(
SubmissionID
,TS_Added
,ProjectNumber
,ProjectName
,TypeofWork
,Mile
,Toll
,Park
,MonRT
,MonOT
,TuesRT
,TuesOT
,WedsRT
,WedsOT
,ThursRT
,ThursOT
,FriRT
,FriOT
,Saturday
,Sunday
,Total
)
SELECT
@SubmissionID
,GETDATE()
,CASE WHEN ISNULL(@ProjectNumber,'') = '' THEN '--' ELSE @ProjectNumber END
,CASE WHEN ISNULL(@ProjectName,'') = '' THEN '--' ELSE @ProjectName END
,CASE WHEN ISNULL(@TypeofWork,'') = '' THEN '--' ELSE @TypeofWork END
,CASE WHEN ISNULL(@Mile,0) =0 THEN '--' ELSE CAST(@Mile AS VARCHAR(30)) END
,CASE WHEN ISNULL(@Toll,0) = 0 THEN '--' ELSE CAST(@Toll AS VARCHAR(30)) END
,CASE WHEN ISNULL(@Park,0) = 0 THEN '--' ELSE CAST(@Park AS VARCHAR(30)) END
,CASE WHEN ISNULL(@MonRT,0) = 0 THEN '--' ELSE CAST(@MonRT AS VARCHAR(30)) END
,CASE WHEN ISNULL(@MonOT,0) = 0 THEN '--' ELSE CAST(@MonOT AS VARCHAR(30)) END
,CASE WHEN ISNULL(@TuesRT,0) = 0 THEN '--' ELSE CAST(@TuesRT AS VARCHAR(30)) END
,CASE WHEN ISNULL(@TuesOT,0) = 0 THEN '--' ELSE CAST(@TuesOT AS VARCHAR(30)) END
,CASE WHEN ISNULL(@WedsRT,0) = 0 THEN '--' ELSE CAST(@WedsRT AS VARCHAR(30)) END
,CASE WHEN ISNULL(@WedsOT,0) = 0 THEN '--' ELSE CAST(@WedsOT AS VARCHAR(30)) END
,CASE WHEN ISNULL(@ThursRT,0) = 0 THEN '--' ELSE CAST(@ThursRT AS VARCHAR(30)) END
,CASE WHEN ISNULL(@ThursOT,0) = 0 THEN '--' ELSE CAST(@ThursOT AS VARCHAR(30)) END
,CASE WHEN ISNULL(@FriRT,0) = 0 THEN '--' ELSE CAST(@FriRT AS VARCHAR(30)) END
,CASE WHEN ISNULL(@FriOT,0) = 0 THEN '--' ELSE CAST(@FriOT AS VARCHAR(30)) END
,CASE WHEN ISNULL(@Saturday,0) = 0 THEN '--' ELSE CAST(@Saturday AS VARCHAR(30)) END
,CASE WHEN ISNULL(@Sunday,0) = 0 THEN '--' ELSE CAST(@Sunday AS VARCHAR(30)) END
,CAST((@MonRT + @MonOT + @TuesRT + @TuesOT + @WedsRT + @WedsOT + @ThursRT + @ThursOT + @FriRT + @FriOT + @Saturday + @Sunday) AS VARCHAR(MAX))

答案 1 :(得分:0)

我使用CASE的原因是因为如果用户没有为任何工作日指定值,则需要0。我的问题是在INSERT计算期间,如果用户将任何输入留空,则SQL将运行如下:1+1++1+1而不是1+1+0+1+1并且会抛出错误。

我最终将所有周字段切换为整数并在表格上设置自动计算以计算总字段,并在我的INSERT语句中忽略该字段,该语句可根据需要运行。