views:

88

answers:

1

In my database I have a column named 'description' in this field There is an age at the beginning of the description ex: "Age 4+ This is a description for this row in the table".

I created a column called 'age' and I have been trying to figure out a way to move the age from the description in one row to its corresponding age column.

How do I go from:

| id |   description   | age |
------------------------------------------------   
|  1 |  Age 4+ This... |     |
|  2 |  Age 7+ Once... |     |

To:

| id |   description   | age |
------------------------------------------------   
|  1 |  This...        |  4+ |
|  2 |  Once...        |  7+ |

Also: Not every row has an age in the description, but any row that has an age is in the format: 'Age x+ '.

EDIT: Sorry, I forgot to add that the database is MySQL 5.0.51a

A: 

Using Sql Server, you could try something like

DECLARE @Table TABLE(
        ID INT,
        [description] VARCHAR(100),
        Age VARCHAR(10)
)

INSERT INTO @Table (ID, [description]) SELECT 1, 'Age 4+ This...'
INSERT INTO @Table (ID, [description]) SELECT 2, 'Age 7+ Once...'
INSERT INTO @Table (ID, [description]) SELECT 3, 'Foo...'

UPDATE @Table
SET [description] = REPLACE([description],LEFT([description],CHARINDEX(' ', [description], CHARINDEX(' ',[description])+1)), ''),
    Age = REPLACE(LEFT([description],CHARINDEX(' ', [description], CHARINDEX(' ',[description])+1)), 'Age ', '')
WHERE LEFT([description], 4) = 'Age '

SELECT *
FROm @Table
astander