?
Solved

How do i concatenate two fields as one in a sql query?

Posted on 2008-10-06
8
Medium Priority
?
1,415 Views
Last Modified: 2013-12-17
I have field 1 (string) and field 2 (int)  i would like to return them as field 3 (Sting concatenation of field1 and 2.  I cant get it to work.

Thanks for any help.

0
Comment
Question by:bmanmike39
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 14

Expert Comment

by:CtrlAltDl
ID: 22654909
This will concatenate the Field1 and Field2 together as Field3:

SELECT Field1, Field2, (Field1 + Field2) AS Field3 FROM myTable
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22654911
this should do:
select field1, field2, field1 + cast(field2 as varchar(20)) as field3 
  from yourtable

Open in new window

0
 
LVL 14

Expert Comment

by:CtrlAltDl
ID: 22654917
If you need a space between them you can do this:
SELECT Field1, Field2, (Field1 + ' ' + Field2) AS Field3 FROM myTable
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 14

Expert Comment

by:CtrlAltDl
ID: 22654939
Actually Angellll is right you'll need to CAST the INT field first.
0
 

Author Comment

by:bmanmike39
ID: 22655497
it still returning 2 columns  i want them to return as one column together without a space between them
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 22656141
None of the solutions posted return 2 columns, they all return 3 columns (field1, field2 and field3), so why don't you post your query and we can point out the problem, instead of spending all day guessing.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 22656967
the suggested code was just "repeating" the field1 and field2 for "demonstration"...
select field1 + cast(field2 as varchar(20)) as field3 
  from yourtable

Open in new window

0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 1000 total points
ID: 22665245
you will need to cast or convert the int to a varchar to make it work
concatenation of a varchar field and an int field without cast or convert will throw an exception always

select
    'returnedcolumnname' = stringfield + ' ' + convert (varchar, intfield)
from
    tablename
0

Featured Post

DFW AZURE MEETUP TONIGHT FRI 6PM

We will be discussing what Azure Stack is, how does it fit into the suit of offerings that Azure has currently, and where can it fit into your organizations technology stack. We will also be discussing limitations of the platform while covering various applicable scenarios.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question