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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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
Jim HornMicrosoft SQL Server Data DudeCommented:
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

Tapan PattanaikSenior EngineerCommented:
Hi Amour22015,

SELECT CONCAT(Field1, Field2) as FieldName FROM TableName

Regards,
Tapan
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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
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
Jim HornMicrosoft SQL Server Data DudeCommented:
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.

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
Tapan PattanaikSenior EngineerCommented:
HI Amour22015,

Please check the attached screen shot,  "CONCAT" is working fine.
Concat.PNG
Amour22015Author Commented:
Great this works - CAST([Field1] as varchar(10)) + CAST([Field2] as varchar(10))
Jim HornMicrosoft SQL Server Data DudeCommented:
Thanks for the grade.  Good luck with your project.  -Jim
Amour22015Author Commented:
I am jumping from project to project between lots of different clients.
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.