I have been tasked with migrating my organization's Oracle SQL Views over to SQL Server. I am a novice when it comes to SQL, and have been unable to figure out the error I am getting from the CASE statement.
In the CASE statement, I want to read in the field's value, which will be a street address such as 123 Main St. The plan is to remove the 'St' substring from the field, and return '123 Main'
It already works in Oracle, I am just having difficulty converting the syntax into SQL Server, and the documentation on SUBSTRING and CASE have not helped me solve the problem. I'm probably overlooking something very simple.
The code is:
SELECT c.objectid, p.SHAPE, c.APN_PARCEL_NO AS prc_parcel_no, b.addr_status, b.addr_st_nmbr AS st_number, b.addr_st_frac AS st_fraction, b.addr_st_pfx AS st_prefix
CASE
WHEN SUBSTRING (b.addr_st_name, CHARINDEX( ' ', b.addr_st_name, -1) + 1) = 'ALY'
THEN SUBSTRING (b.addr_st_name, 1, CHARINDEX( ' ', b.addr_st_name, -1) -1)
ELSE b.add_st_name
END
FROM dbo.PARCEL AS p INNER JOIN dbo.TBL_APN_PARCEL_LINK AS c ON p.PRC_PARCEL_NO = c.LAND_APN INNER JOIN dbo.TBL_SITE_ADDRESS_ALL AS b ON c.APN_PARCEL_NO = b.prc_parcel_no
and the error I get reads:
Error Source:.Net SqlClient Data Provider
Error Message: Incorrect syntax near the keyword 'CASE'
I apologize for any formatting problems, still learning how to properly write SQL. For your convenience, the CASE statement code in Oracle is:
CASE
WHEN SUBSTR (b.addr_st_name,
INSTR (b.addr_st_name, ' ', -1) + 1
) = 'ALY'
THEN SUBSTR (b.addr_st_name,
1,
INSTR (b.addr_st_name, ' ', -1) - 1
)
ELSE b.addr_st_name
END st_name,
tl;dr - Looking to convert Oracle view to SQL Server, error is:
Incorrect syntax near the keyword 'CASE'