If my result is null or empty I would like it not to add a comma in front of the string. How can I achieve that?
Update Companies
set address4 = concat(address4,','+ t.Eircode)
From companies c
Inner Join Temptbl1 t
on c.comp_id = t.Comp_ID
OR address4 = ''
Update :
Just to make it clear when I make this update
,V05 TTX1
Waterford,X01 B234
,B90 E902
Co Wexford,TD2 PVE2
It adds a comma regardless ,if it was empty or not
What I want to get back is this :
V05 TTX1
Waterford,X01 B234
B90 E902
Co Wexford,TD2 PVE2
If there is no value then just add the string without the comma ,in our case V05 TTX1
without ,
at the start of the string
CREATE TABLE [dbo].[companies](
[comp_id] [int] NOT NULL,
[address4] [varchar](32) NOT NULL
)
INSERT INTO [dbo].[companies]
([comp_id]
,[address4])
3 Answers 3
I assume t.Eircode is what concerns you. You can use a CASE statement:
SET address4 = concat(address4
,CASE WHEN NULLIF(address4 ,'') IS NOT NULL
THEN ','+ t.Eircode
ELSE ''
END)
I also added a NULLIF function that maps '' to null so that '' and null are treated as the same.
EDIT: Given the information that address4 is NOT NULL the test can be changed to:
SET address4 = concat(address4
,CASE WHEN address4 <> ''
THEN ','
ELSE ''
END) + t.Eircode
It is my understanding that t.Eircode should always be concatenated, and that it is the "," that should be conditionally concatenated so I moved it out from the CASE.
-
Just change
NULLIF(t.Eircode,'')
toNULLIF(t.address4,'')
Lucy– Lucy2018年04月30日 13:33:18 +00:00Commented Apr 30, 2018 at 13:33 -
@Lucy
t
has anaddress4
? I thought that column was only inc
? This is whyCREATE TABLE
andINSERT
to set up sample data are useful, as is always prefixing all columns with the table/alias they come from.Aaron Bertrand– Aaron Bertrand2018年04月30日 13:42:22 +00:00Commented Apr 30, 2018 at 13:42 -
NULLIF(t.Eircode,'')
toNULLIF(address4,'')
,sorry forgot to leave outt
,I'll add theCREATE TABLE
andINSERT
just to make it clearer to peopleLucy– Lucy2018年04月30日 13:47:10 +00:00Commented Apr 30, 2018 at 13:47 -
1@Lennart ,Yes that's true t.Eircode should always be thereLucy– Lucy2018年05月01日 08:00:08 +00:00Commented May 1, 2018 at 8:00
Since you are already combining CONCAT
and +
to concatenate strings you could just turn them around (although I agree Lennert's solution shows the intent a lot clearer).
Update Companies
-- set address4 = concat(address4,','+ t.Eircode)
set address4 = concat(address4 + ',', t.Eircode)
From companies c
Inner Join Temptbl1 t
on c.comp_id = t.Comp_ID
OR address4 = ''
This works because CONCAT
converts NULL
into an empty string of type varchar(1)
, but as long as CONCAT_NULL_YIELDS_NULL
is on concatenating a string with NULL will yield NULL.
See this dbfiddle for an example
-
I believe this would allow trailing commas if
t.Eircode
is NULL; presumably, the OP only wants a comma if bothaddress4
andt.Eircode
are populated.RDFozz– RDFozz2018年04月30日 15:06:09 +00:00Commented Apr 30, 2018 at 15:06 -
@RDFozz That was not in the sample code and the OP's edit to Lennarts answer seems to confirm that, but you could just use + for all three stringsTom V– Tom V2018年04月30日 15:08:38 +00:00Commented Apr 30, 2018 at 15:08
-
Combining and
CONCAT
and+
is indeed the way to go.wp78de– wp78de2019年10月24日 22:59:41 +00:00Commented Oct 24, 2019 at 22:59
I usually do this by nesting ISNULL
and NULLIF
:
UPDATE Companies
SET address4 = ISNULL(NULLIF(address4, '') + ',', '') + t.Eircode
FROM Companies c
INNER JOIN Temptbl1 t
ON c.comp_id = t.Comp_ID
OR address4 = ''
OR address4 = ''
supposed to be doing? This will actually corrupt your data by introducing many more matches than you probably intend.OR address4 = ''
The temptbl1 is just an excel sheet I imported from Sql server , that table won't be used ,I'm just using it to accomplish my task ,I was give a list of IDs where I had to update their addresses