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

Posted on 2008-10-06
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.

Question by:bmanmike39
LVL 14

Expert Comment

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

SELECT Field1, Field2, (Field1 + Field2) AS Field3 FROM myTable
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

LVL 14

Expert Comment

ID: 22654917
If you need a space between them you can do this:
SELECT Field1, Field2, (Field1 + ' ' + Field2) AS Field3 FROM myTable
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

LVL 14

Expert Comment

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

Author Comment

ID: 22655497
it still returning 2 columns  i want them to return as one column together without a space between them
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.
LVL 143

Accepted Solution

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

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

    'returnedcolumnname' = stringfield + ' ' + convert (varchar, intfield)

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

685 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