Link to home
Start Free TrialLog in
Avatar of Amour22015
Amour22015

asked on

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....
Avatar of YZlat
YZlat
Flag of United States of America image

[Field1] + ' + ' + [Field2]

will display 20 + 15

or

[Field1] + '  ' + [Field2]

will display 20 15
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

Hi Amour22015,

SELECT CONCAT(Field1, Field2) as FieldName FROM TableName

Regards,
Tapan
Avatar of Amour22015
Amour22015

ASKER

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
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
ASKER CERTIFIED SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I should say that I am using 2008 R2  so the ConCat will not work
HI Amour22015,

Please check the attached screen shot,  "CONCAT" is working fine.
Concat.PNG
Great this works - CAST([Field1] as varchar(10)) + CAST([Field2] as varchar(10))
Thanks for the grade.  Good luck with your project.  -Jim
I am jumping from project to project between lots of different clients.