MS SQL Server的COALESCE函數是從一系列表達式中返回第一個NOT NULL的值。 檢查[B],[Q],[S],[T],[U]的值: 檢查順序[B]->[Q]->[S]->[T]->[U],只要一遇上NOT NULL時,即刻返回。 IF OBJECT_ID('tempdb.dbo.# ...
MS SQL Server的COALESCE函數是從一系列表達式中返回第一個NOT NULL的值。
檢查[B],[Q],[S],[T],[U]的值:
檢查順序[B]->[Q]->[S]->[T]->[U],只要一遇上NOT NULL時,即刻返回。
IF OBJECT_ID('tempdb.dbo.#Part_summary') IS NOT NULL DROP TABLE #Part_summary CREATE TABLE #Part_summary ( [Item] NVARCHAR(40), [B]DECIMAL(18,2), [Q]DECIMAL(18,2), [S]DECIMAL(18,2), [T]DECIMAL(18,2), [U]DECIMAL(18,2) ) INSERT INTO #Part_summary ([Item],[B],[Q],[S],[T],[U]) VALUES ('098-SSSS1-WS0098-5526',NULl,NULl,500.00,NULl,NULl), ('54F-ART43-6545NN-2514',NULl,NULl,934.39,NULl,NULl), ('872-RTDE3-Q459PW-2323',NULl,346.43,NULl,452.44,NULl), ('B78-F1H2Y-5456UD-2530',234.22,NULl,NULl,115.06,NULl), ('I32-GG443-QT0098-0001',NULl,NULl,423.65,NULl,NULl), ('I38-AA321-WS0098-0506',470.87,NULl,NULl,NULl,345.41), ('K38-12321-5456UD-3493',200.28,NULl,NULl,398.55,NULl), ('PO0-7G7G7-JJY098-0077',NULl,871.33,543.00,NULl,NULl), ('RVC-43ASE-H43QWW-9753',NULl,564.96,NULl,NULl,555.19), ('X3C-SDEWE-3ER808-8764',NULl,607.88,NULl,NULl,NULl) SELECT [Item],[B],[Q],[S],[T],[U] FROM #Part_summary SELECT [Item],COALESCE([B],[Q],[S],[T],[U]) AS [result] FROM #Part_summarySource Code
再列一個例子:
IF OBJECT_ID('tempdb.dbo.#Part_summary') IS NOT NULL DROP TABLE #Part_summary CREATE TABLE #Part_summary ( [Item] NVARCHAR(40), [B]DECIMAL(18,2), [Q]DECIMAL(18,2), [S]DECIMAL(18,2), [T]DECIMAL(18,2), [U]DECIMAL(18,2) ) INSERT INTO #Part_summary ([Item],[B],[Q],[S],[T],[U]) VALUES ('54F-ART43-6545NN-2514',NULl,NULl,NULl,NULl,934.39), ('872-RTDE3-Q459PW-2323',NULl,NULl,NULl,452.44,NULl), ('B78-F1H2Y-5456UD-2530',NULl,NULl,115.06,NULl,NULl), ('I32-GG443-QT0098-0001',NULl,607.88,NULl,NULl,NULl), ('I38-AA321-WS0098-0506',470.87,NULl,NULl,NULl,NULl) SELECT [Item],[B],[Q],[S],[T],[U] FROM #Part_summary SELECT [Item],COALESCE([B],[Q],[S],[T],[U]) AS [result] FROM #Part_summarySource Code