需要帮助...以下查询在尝试使用IF Else时出错。 SERIAL_NUMBER字段中包含两位数的年份,我想将其解压缩并在其自己的字段中显示为4位数年份。
SELECT
[START_TIMESTAMP]
,[END_TIMESTAMP]
,[SERIAL_NUMBER]
,[MODEL]
,[TESTING_ACTION_RESULT]
,[FAILED_STEP]
,[DISPOSITION]
,[TesterId]
,[Location]
,[Customer]
,IF CAST(substring([SERIAL_NUMBER],3,2)AS INT)>90
'19'+substring([SERIAL_NUMBER],3,2) AS MFG_YYYY
ELSE
'20'+substring([SERIAL_NUMBER],3,2) AS MFG_YYYY
FROM [DATABASE_1].[dbo].[TBL_Unit_Test]
GO
答案 0 :(得分:1)
您不能在SELECT语句中使用IF,而是使用CASE表达式:
SELECT
[START_TIMESTAMP]
,[END_TIMESTAMP]
,[SERIAL_NUMBER]
,[MODEL]
,[TESTING_ACTION_RESULT]
,[FAILED_STEP]
,[DISPOSITION]
,[TesterId]
,[Location]
,[Customer]
,case
when CAST(substring([SERIAL_NUMBER],3,2)AS INT)>90
then '19'+substring([SERIAL_NUMBER],3,2)
else '20'+substring([SERIAL_NUMBER],3,2) end AS MFG_YYYY
FROM [DATABASE_1].[dbo].[TBL_Unit_Test]
答案 1 :(得分:0)
您可以使用CASE expression
,例如
SELECT
[START_TIMESTAMP]
,[END_TIMESTAMP]
,[SERIAL_NUMBER]
,[MODEL]
,[TESTING_ACTION_RESULT]
,[FAILED_STEP]
,[DISPOSITION]
,[TesterId]
,[Location]
,[Customer]
, MFG_YYYY =
CASE
WHEN CAST(substring([SERIAL_NUMBER],3,2)AS INT)>90
THEN '19'+substring([SERIAL_NUMBER],3,2)
ELSE '20'+substring([SERIAL_NUMBER],3,2)
END
FROM [DATABASE_1].[dbo].[TBL_Unit_Test]
GO