Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combining 2 columns

Status
Not open for further replies.

sparkbyte

Technical User
Sep 20, 2002
879
US
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?


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
 
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
 
That is perfect.

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

THANKS!!!!

Thanks

John Fuhrman
 
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)


 
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
 
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
 
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)


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top