Solved

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

Posted on 2008-10-06
8
1,408 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
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 142

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 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 250 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

809 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