Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split column into two columns and copy over values

Tags:

sql

sybase

I have a Sybase table with a column storing char(6) values. I would like to drop this column and create two char(3) columns, copying over the first three chars of the original column to column 1 and the last 3 chars to column 2. What would be best way to do this?

like image 316
Graeme Avatar asked Mar 03 '26 02:03

Graeme


2 Answers

ALTER TABLE YourTable ADD FirstHalf CHAR (3)
ALTER TABLE YourTable ADD SecondHalf CHAR (3)

UPDATE YourTable
SET
    FirstHalf = LEFT(OriginalColumn, 3),
    SecondHalf = RIGHT(OriginalColumn, 3)

ALTER TABLE YourTable DROP COLUMN OriginalColumn
like image 79
Michael Fredrickson Avatar answered Mar 05 '26 16:03

Michael Fredrickson


Create your two new columns.

UPDATE the table with something like

UPDATE
  table
SET
  new1 = LEFT(old, 3),
  new2 = RIGHT(old, 3)

Remove the old column.

like image 32
MatBailie Avatar answered Mar 05 '26 16:03

MatBailie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!