I'm trying to write a SQL Server trigger that concats 3 columns, but inserts a string in place of the third column based on a condition.
For example when col 1
is inserted concat col 1
, col 2
and col 3
. But when col 3 = 'DR'
concat Drive
or col 3 = 'ST'
concat Street
etc.
I can write the trigger for all 3 columns, but having trouble with the conditional.
CREATE TRIGGER updateAddress
ON [ARCHIVENEW].dbo.a11
AFTER UPDATE
AS BEGIN
SET NOCOUNT ON;
IF UPDATE(STRNAME)
SELECT CASE
WHEN a11.STRTYPE = 'DR' THEN dbo.a11.STRFNAME = cast(dbo.a11.STRADDLF as varchar(2)) + ' ' + STRNAME + ' ' + 'Drive'
WHEN a11.STRTYPE = 'RP' THEN dbo.a11.STRFNAME = cast(dbo.a11.STRADDLF as varchar(2)) + ' ' + STRNAME + ' ' + 'Ramp'
WHEN a11.STRTYPE = 'EX' THEN dbo.a11.STRFNAME = cast(dbo.a11.STRADDLF as varchar(2)) + ' ' + dbo.a11.STRNAME + ' ' + 'Express Way'
ELSE dbo.a11.STRFNAME = cast(dbo.a11.STRADDLF as varchar(2)) + ' ' + dbo.a11.STRNAME
END
Sorry it took a few days to get back to this and add the code sample. I'm getting a syntax error near the '=' where I try and set my STRFNAME to the concatenated string. To clarify further, col1 is "1234" col2 is "Main" and col3 is "ST". When a row is inserted I'd like col4 to say "1234 Main Street".