2

I am a beginner and got stuck. Any help will be very much appreciated.

I have a text field in the database, the values in the field are as follow, i.e.: "John: 20; Dan: 30; Greg: 80; Sam: 66; Coco: 97;" this all in one field.

The result that I would like to get is as follows: John is 20 years old Dan is 30 years old Greg is 80 years old

Thank you

5
  • Please provide example DB structure. Also please clarify records, are semi-colons separating rows in your example or is that all one row? Commented Sep 30, 2017 at 16:08
  • @chris85 he wrote "this all in one field", so I assume, it's all in one field ;o). Vladimir: want the solution to be all mysql, php or does it matter anyway? Commented Sep 30, 2017 at 16:12
  • @Jakumi It'd be better if reproducible code was provided. As is we are guessing here. Commented Sep 30, 2017 at 16:15
  • @chris85 I assume there is no code yet. Commented Sep 30, 2017 at 16:28
  • Store your data in two columns (name, age). That would make your live easier. Your next question could be "How do I sort the data by age?" - Which would be easy to answer with two columns. Commented Sep 30, 2017 at 16:40

2 Answers 2

2

Please run below code

UPDATE your_table
SET your_field = REPLACE(your_field, ':', ' is ')

and this replaces ':' character with ' is'

UPDATE your_table
SET your_field = REPLACE(your_field, ';', 'years old')

and this replaces ';', 'years old'

At the end of this query

John: 20; Dan: 30; Greg: 80; Sam: 66; Coco: 97;

above string will be below string

John is 20 years old Dan is 30 years old Greg is 80 years old  Sam is 66 years old Coco is 97 years old
Sign up to request clarification or add additional context in comments.

1 Comment

REPLACE() also works with SELECT, So without updating the column value it is possible to SELECT from the text field as per the output required.
0
SELECT name + ' is ' + age + ' years old' 
FROM your_table_name

Here name is the column name of people(John, Dan, Grek) and age is the column name of ages (20, 30, 80)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.