INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Combining 2 columns

Combining 2 columns

(OP)
I need to combine 2 columns in the same DB and table. (MS SQL Server 2008 ent.)

If columnA has a value AND ColumnB isNull Then use ColumnA value
If columnB has a value AND ColumnA isNull Then use ColumnB value
If both ColumnA AND ColumnB have values then use ColumnA value

This query gives me results I expect, but what would be the best way to do this?


CODE --> SQL

Select 
	 case when A_FILE Is not null then A_FILE
			when A_FILE Is null then LONG_A_FILE
		end as upper(FileNumCombined)
From dbo.COMPLETED 

Thanks

John Fuhrman

RE: Combining 2 columns

CODE

SELECT
    COALESCE(ColumnA, ColumnB) 'Result'
FROM dbo.COMPLETED 

If you look at the COALESCE function, it should be what you want. You can provide any number of inputs and the COALESCE function returns the FIRST NON-NULL value.

Robert "Wizard" Johnson III
U.S. Military Vets MC
CSM, CSPO, MCPD, CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
Senior Database Developer

RE: Combining 2 columns

(OP)
That is perfect.

Now how do I insert the result set into a new column?

THANKS!!!!

Thanks

John Fuhrman

RE: Combining 2 columns

He already showed you. "Result" is the name of the new column.

==================================
adaptive uber info galaxies (bigger, better, faster, and more adept than cognitive agile big data clouds)


RE: Combining 2 columns

(OP)
I guess I was being silly.

I will have to use the COALESCE in an update statement and create a new column to put the combined result set into.

Thanks

Thanks

John Fuhrman

RE: Combining 2 columns

What have you already tried? I'm more than willing to help someone learn a new idea or way of doing something, but the only way I can assist your learning is to know where you currently are. I'm trying to not just "give" you the answer in rote. At least for me, I find the most rewarding learning experience is when I am prompted in the right direction but still complete the idea or process myself. This also solidifies my understanding and helps me retain the information better.

So to try to assist you:

If you're struggling with the UPDATE statement, there are plenty of resources for that available to you. Web searches, SQL Server Management Studio Help/Books On Line, etc. I'll even provide a sample UPDATE statement here:

CODE

UPDATE dbo.COMPLETED
SET ColumnA = 'SomeValue'; 

This little statement will enter the COMPLETED table and (assuming the ColumnA is a datatype of VARCHAR or a variant) will set the column value to SomeValue for every record in the table.

It's possible to use other statements, functions, columns etc in place of the 'SomeValue' value to get a different value for each record. You can even concatenate columns in the statement.

Hopefully that helps you find a solution to fit your needs. If not, please let us know what you've tried and what is not working so we can help fine tune your attempt.

Robert "Wizard" Johnson III
U.S. Military Vets MC
CSM, CSPO, MCPD, CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
Senior Database Developer

RE: Combining 2 columns

ALTER TABLE your_table ADD COLUMN your_new_column VARCHAR(xx) NULL;
UPDATE your_table SET your_new_column = COALESCE(ColumnA, ColumnB);
commit;

==================================
adaptive uber info galaxies (bigger, better, faster, and more adept than cognitive agile big data clouds)


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close