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....
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....
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))
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))
Hi Amour22015,
SELECT CONCAT(Field1, Field2) as FieldName FROM TableName
Regards,
Tapan
SELECT CONCAT(Field1, Field2) as FieldName FROM TableName
Regards,
Tapan
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
[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
ASKER
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I should say that I am using 2008 R2 so the ConCat will not work
ASKER
Great this works - CAST([Field1] as varchar(10)) + CAST([Field2] as varchar(10))
Thanks for the grade. Good luck with your project. -Jim
ASKER
I am jumping from project to project between lots of different clients.
will display 20 + 15
or
[Field1] + ' ' + [Field2]
will display 20 15