943,807 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 15962
  • MS SQL RSS
Dec 10th, 2008
0

How to merge two columns ?

Expand Post »
Hi , I have 2 columns named

1. Tel_H
2. Tel_H_Code

How can i merge these two columns to make them one ??

I am clueless here...The best i can do is to view them as one

MS SQL Syntax (Toggle Plain Text)
  1. SELECT Tel_H_Code + Tel_H AS 'Tel_H'
  2. FROM Detailed_List
Similar Threads
cVz
Reputation Points: 29
Solved Threads: 7
Junior Poster
cVz is offline Offline
139 posts
since Mar 2008
Dec 11th, 2008
0

Re: How to merge two columns ?

maybe you can use select into statement, with it you merge two colums and take it another table like that
sql Syntax (Toggle Plain Text)
  1. SELECT Tel_H_Code + Tel_H AS 'Tel_H' INTO Table1
  2. FROM Detailed_List
so you can use it
select Tel_H from Table1
Last edited by peter_budo; Dec 12th, 2008 at 7:04 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
emircan1986 is offline Offline
1 posts
since Apr 2008
Dec 11th, 2008
0

Re: How to merge two columns ?

try this

sql Syntax (Toggle Plain Text)
  1. SELECT Tel_H_Code + ' ' + Tel_H AS Tel_H
  2. FROM Detailed_List
Last edited by peter_budo; Dec 12th, 2008 at 7:05 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 10
Solved Threads: 9
Junior Poster
sierrainfo is offline Offline
144 posts
since May 2008
Dec 28th, 2011
0
Re: How to merge two columns ?
Click to Expand / Collapse  Quote originally posted by sierrainfo ...
try this

sql Syntax (Toggle Plain Text)
  1. SELECT Tel_H_Code + ' ' + Tel_H AS Tel_H
  2. FROM Detailed_List
this command sums the values of fields!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chatbaz is offline Offline
2 posts
since Dec 2011
Dec 28th, 2011
0
Re: How to merge two columns ?
Let me start by saying that you have ressurected a 3 year old topic. You should have read the warning and let it be. Especially when you don't really know what you are talking about.

The plus symbol (+) has 2 functions:
a) to add when used with numbers
b) to concatenate when used with strings.

The post that you've commented includes a space within quotes, so at best it will produce an error if the Tel_H_Code or Tel_H is a number. If they have been declared as chars then it will just concatenate them.
Reputation Points: 204
Solved Threads: 121
Practically a Master Poster
adam_k is offline Offline
647 posts
since Jun 2011
Dec 29th, 2011
0
Re: How to merge two columns ?
yes you're right, it's an old topic but many people read this topic that doesn't have a true result.

I tried this command for two numeric fields and it summed them definitely! so the space within quotes didn't work!

the solution is:
MS SQL Syntax (Toggle Plain Text)
  1. SELECT COALESCE (CAST(Tel_H AS varchar), '') + ' - ' + COALESCE (CAST(Tel_H AS varchar), '') AS Concatenation
  2. FROM Detailed_List
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chatbaz is offline Offline
2 posts
since Dec 2011
3 Days Ago
0

Textbook response

This worked to combine 2 string values (first name & last name) from 2 separate columns (first_name, last_name) into 1 new column (full_name). Straight out of the textbook: SQL Fundamentals 3rd edition pg. 344:

select employee_id, first_name || ' ' || last_name AS full_name, hire_date
from l_employees;
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Todster is offline Offline
1 posts
since Feb 2012
Message:
Previous Thread in MS SQL Forum Timeline: How to handle special charters in query
Next Thread in MS SQL Forum Timeline: Basic Query





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC