Reply
SQL Copying one columns content into another
Old 09-23-2009, 06:51 PM SQL Copying one columns content into another
downliner's Avatar
Extreme Talker

Posts: 206
Name: Will
Location: Scotland
Trades: 1
I can't seem to figure this one and would appreciate some pointers as Im not convinced Im even using the correct command Assume the following basic setup:

------------------------------
| Product_inventory
------------------------------
| Name | Description |
------------------------------
| Item 1 | <p>Blah blah
| Item 2 | <p>Filler content
| Item 3 | <p>Text text
| Item 4 | <p>More random

What I would like to do is automatically copy each rows 'Name' into the 'Description' column to preceed the already existing text. So the Description for Item 1 would read as "Item 1 <p>Blah blah"

I've been playing with the replace command and whilst it allows me to insert text at the beginning of each description I haven't figured out the correct SQL to copy the 'Name' fields (its just printing "Name" instead of the actual product title). This method also doesn't account for a description containing multiple <p>

Code:
UPDATE Product_inventory SET `Description` = replace(`Description`, "`Name` <p>", "<p>")
Am I on the right path? Cheers
downliner is offline
Reply With Quote
View Public Profile Visit downliner's homepage!
 
 
When You Register, These Ads Go Away!
Old 09-30-2009, 01:42 AM Re: SQL Copying one columns content into another
Super Talker

Posts: 123
Name: vikas
Trades: 0
you can use insert into table from ( select * from other table) this is how I use when I have to move data between two tables
__________________
Free Online Books Collection of free online books and free ebooks |Online pdf Books - Free online pdf books and free pdf eBooks
vikas1234 is offline
Reply With Quote
View Public Profile
 
Old 09-30-2009, 03:19 AM Re: SQL Copying one columns content into another
tripy's Avatar
Do not try this at home!

Posts: 3,176
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
It depends of the db you use, but guessing it's mysql, use the concat() function:
Code:
drop table test;
create table test( name varchar(20), description varchar(100));

insert into test values ('item 1','<p> blah');
insert into test values ('item 2','<p>Filler content');
insert into test values ('item 3','<p>Text text');
insert into test values ('item 4','<p>More random');

select * from test;

update test
set description=concat(name,' ',description)

select * from test;
which result into:
Code:
mysql> select * from test;
+--------+--------------------------+
| name   | description              |
+--------+--------------------------+
| item 1 | item 1 <p> blah          |
| item 2 | item 2 <p>Filler content |
| item 3 | item 3 <p>Text text      |
| item 4 | item 4 <p>More random    |
+--------+--------------------------+
4 rows in set (0.00 sec)
For postgresql (and I think oracle too, but I have no oracle instance to test), for example, the command would be
Code:
update test
set description=name||' '||description;
and for ms sql server, it would be
Code:
update test
set description=name+' '+description;
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 09-30-2009 at 03:23 AM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to SQL Copying one columns content into another
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.11151 seconds with 13 queries