TSQL - Placing 2 columns side-by-side

Hi Experts,

I am doing this:
[Field1] + [Field2]
15 + 20 = 35

I do not want that, it is adding them both together

I need the correct syntax to do this:
1520

thanks for helping....
Amour22015Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

YZlatCommented:
[Field1] + ' + ' + [Field2]

will display 20 + 15

or

[Field1] + '  ' + [Field2]

will display 20 15
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Guessing Field1 and Field2 are numeric data types, so if you wish to concatenate instead of add you have to convert them to character data types first...

SELECT CAST([Field1] as varchar(10))  + CAST([Field2] as varchar(10))
Declare @no1 int = 15, @no2 int = 20

-- This returns 35
SELECT @no1 + @no2

-- This returns 1520
SELECT CAST(@no1 as varchar(10)) + CAST(@no2 as varchar(10)) 

Open in new window

0
Tapan PattanaikSenior EngineerCommented:
Hi Amour22015,

SELECT CONCAT(Field1, Field2) as FieldName FROM TableName

Regards,
Tapan
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Amour22015Author Commented:
I do not want to do this:
[Field1] + ' + ' + [Field2]

will display 20 + 15

or

[Field1] + '  ' + [Field2]

will display 20 15

I want to:
2015


And when I do this:
Declare @no1 int = [SmallYR], @no2 int = [Num]
I get a syntax error:
Msg 207, Level 16, State 1, Line 1
Invalid column name 'SmallYR'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Num'.

Please help and thanks
0
Amour22015Author Commented:
And when I do this:
CONCAT(Field1, Field2) as FieldName I get syntax error
Msg 195, Level 15, State 10, Line 3
'CONCAT' is not a recognized built-in function name.


Please help and thanks
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
The code I posted works on my SSMS, as I don't have access to your data source so can't test it with [Field1], [Field2], and [YourTableNameGoesHere].

<air code>
SELECT CAST([Field1] as varchar(10)) + CAST([Field2] as varchar(10)) 
FROM YourTableNameGoesHere

Open in new window

The CONCAT solution will work too as long as you're running SQL 2012 or higher.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Amour22015Author Commented:
I should say that I am using 2008 R2  so the ConCat will not work
0
Tapan PattanaikSenior EngineerCommented:
HI Amour22015,

Please check the attached screen shot,  "CONCAT" is working fine.
Concat.PNG
0
Amour22015Author Commented:
Great this works - CAST([Field1] as varchar(10)) + CAST([Field2] as varchar(10))
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the grade.  Good luck with your project.  -Jim
0
Amour22015Author Commented:
I am jumping from project to project between lots of different clients.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.