I am trying to concatenate values into one string value and what i have so far is progress, but I want to encapsulate my variables with single quotes. The below gives me e.g. Karen,Cox,Oakland,MN,95573 when what I need is 'Karen','Cox','Oakland','MN','95573':
p.firstname + ',' +
p.lastname + ',' +
i.city + ',' +
t.stateAbbrveation + ',' +
Convert(char(5), c.peopleId)
as insert_string
'''' + p.firstname + ''',''' +
p.lastname + ''',''' +
i.city + ''',''' +
t.stateAbbrveation + ''',''' +
Convert(char(5), c.peopleId) + ''''
as insert_string
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
March 19, 2020 at 5:22 pm
You may be interested in using the CONCAT() function – it's tidier (IMO) than all those plus signs, does datatype conversion and handles NULLs for you.
Also note that you have misspelled abbreviation.
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
March 19, 2020 at 5:59 pm
Look at ConCat(), but what Scott did was escape out the single quotes with extras. That's a common technique for adding them to strings.
March 19, 2020 at 7:12 pm
I'd make it less cluttered by using a variable for the literal single quote:
DECLARE @H CHAR(1) = '''';
the CONCAT version then becomes something like this:
CONCAT(
@H
,p.firstname
,@H
,','
,@H
,p.lastname
,@H
,','
,@H
,i.city
,@H
,','
,@H
,t.stateAbbrveation
,@H
,','
,@H
,CONVERT(CHAR(5), c.peopleId)
,@H
);
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
March 19, 2020 at 7:53 pm
I'd make it less cluttered by using a variable for the literal single quote:
DECLARE @H CHAR(1) = '''';the CONCAT version then becomes something like this:
CONCAT(
@H
,p.firstname
,@H
,','
,@H
,p.lastname
,@H
,','
,@H
,i.city
,@H
,','
,@H
,t.stateAbbrveation
,@H
,','
,@H
,CONVERT(CHAR(5), c.peopleId)
,@H
);
I wouldn't. On principle I never use a single-char name, since it's by definition meaningless. I might create, say, @quote and @quotes_with_comma. Btw, w.t.heck does H stand for in that "name" anyway??
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
March 19, 2020 at 8:38 pm
Btw, w.t.heck does H stand for in that "name" anyway??
That is my mistake! For some reason, I had the word 'hyphen' in my mind when I wrote the code, and that's where it came from.
In this instance, I used the single letter to reduce the real estate/clutter created by masses of variable repeats.
I always use meaningful variable names, unless, as here, there is reason not to.
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
March 23, 2020 at 11:24 am
Also note that you have misspelled abbreviation.
That may not be the OP's fault. It might be inherited from whenever the tables were created.
I've run across it many times in my work. (I vary from excusing it because someone might have English as a second language with grumbling because someone obviously wasn't paying attention.)
Often it's easier to repeat the error than to try to find and correct every place the misspelled field name might be used.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply