2

In a given database table "Animals",a column called 'anim_name" existed as shown in the picture below enter image description here

to update the anim_name so that there won't be a '-' .

Example:

'TE-MAEWA 04000' should update as 'TE MAEWA 04000' 
'TE-MAEWA 04354 CHAROLAIS CROSS' should update as 'TE MAEWA 04354 CHAROLAIS CROSS'

The following update statement is wrong

 update animals 
    set anim_name = 'TE MAEWA%'
  where soc_code = 'AUDV' 
    and anim_name like 'TE-MAEWA%'

because this would update every animal name to 'TE MAEWA%'. what would be the right update statement.

1
  • I would create a CTE that selects the uuid of the row, and the anim_name without the -. Then use that CTE to do an update with a join. Have a look here: stackoverflow.com/a/25358642/4080476 Commented Oct 19, 2016 at 23:59

1 Answer 1

5

Use the function replace():

update animals
set anim_name = replace(anim_name, 'TE-MAEWA', 'TE MAEWA')
where soc_code = 'AUDV' and anim_name like 'TE-MAEWA%';
Sign up to request clarification or add additional context in comments.

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.