Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2008 选择期间的SQL IF ELSE语句?_Sql Server 2008 - Fatal编程技术网

Sql server 2008 选择期间的SQL IF ELSE语句?

Sql server 2008 选择期间的SQL IF ELSE语句?,sql-server-2008,Sql Server 2008,需要帮助。。。以下查询在尝试使用IF-Else时出错。 SERIAL_NUMBER字段中有两位数的年份,我想提取它并在它自己的字段中显示为4位数的年份 SELECT [START_TIMESTAMP] ,[END_TIMESTAMP] ,[SERIAL_NUMBER] ,[MODEL] ,[TESTING_ACTION_RESULT] ,[FAILED_STEP] ,[DISPOSITION] ,[TesterId] ,[Location] ,[Cus

需要帮助。。。以下查询在尝试使用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    

不能在SELECT语句中使用IF,而是使用表达式:

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]

不能在SELECT语句中使用IF,而是使用表达式:

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]
你可以用,比如

你可以用,比如