Solved

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

Posted on 2008-10-06
8
1,407 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
 
LVL 14

Expert Comment

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

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now