Home > Back-end >  Using if in SQL Server based on IF function condition from Excel
Using if in SQL Server based on IF function condition from Excel

Time:10-04

I am looking for help to convert the IF format below to be used in SQL Server:

=IF((MID(A1,3,1))="1","SS"&(MID(A1,1,2)),"FW"&(MID(A1,1,2)))

The column of the table I am trying to use instead of A1 will be Data_Code, willing to have the Data_Code result will be either SS or FW, I did create a small column below for the original data:

|Data_Code|
|221484848|
|222848488|

CodePudding user response:

I think this =IF((MID(A1,3,1))="1","SS"&(MID(A1,1,2)),"FW"&(MID(A1,1,2))) comes from Excel. You can use CASE expression to check the value, and SUBSTRING function for MID like this:

SELECT CASE SUBSTRING(Data_Code, 3,1) 
         WHEN '1' THEN 'SS' 
         ELSE 'FW' 
       END   SUBSTRING(Data_Code, 1,2) [MyNewColumn]
FROM [ExampleTable]
  • Related