• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1423
  • Last Modified:

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

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
bmanmike39
Asked:
bmanmike39
2 Solutions
 
CtrlAltDlCommented:
This will concatenate the Field1 and Field2 together as Field3:

SELECT Field1, Field2, (Field1 + Field2) AS Field3 FROM myTable
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
this should do:
select field1, field2, field1 + cast(field2 as varchar(20)) as field3 
  from yourtable

Open in new window

0
 
CtrlAltDlCommented:
If you need a space between them you can do this:
SELECT Field1, Field2, (Field1 + ' ' + Field2) AS Field3 FROM myTable
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
CtrlAltDlCommented:
Actually Angellll is right you'll need to CAST the INT field first.
0
 
bmanmike39Author Commented:
it still returning 2 columns  i want them to return as one column together without a space between them
0
 
Anthony PerkinsCommented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
Anurag ThakurTechnical ManagerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now