1

Have a problem with update some rows in table 'commission'. Now I have commission_number like bar/123/456. I'd like to rename it to John/123/456 etc, taking a name from 'user' table

commission                       user
-------------------------        -----------------
commission_number|user_id         username|user_id
-------------------------        -----------------
bar/123/456      | 1               John   | 1
bar/123/123      | 2               Bob    | 2
bar/456/123      | 3               Thomas | 3

Below is my query, working with 'baz'. But don't know how to place 'username' from 'user' table. I only need to replace 'baz', and use username from 'user', but don,t know how.

UPDATE commission
SET commission_number = overlay(commission_number placing 'baz' FROM POSITION('bar' IN commission_number) for 3)
WHERE commission_number in (1,2,3,4,5,6,7,8)
3
  • Why do you store the username in the commission_number at all? You shouldn't be storing data that can be derived from the existing content. Commented Mar 12, 2013 at 13:40
  • I found a bug in system in our company and have to rename it. I cant change table structure, I'm not a developer :( Commented Mar 12, 2013 at 13:44
  • 1
    Then tell whoever is responsible that this is a horrible table design (you may quote me). Commented Mar 12, 2013 at 13:47

4 Answers 4

3
update commission 
   set commission_number = u.username || '/' || left(commission_number, -strpos(commission_number, '/'))
from users u
  where u.user_id = commission.user_id
Sign up to request clarification or add additional context in comments.

Comments

1

I'd start by experimenting with something like...

UPDATE commission
SET commission_number = 
  (SELECT UserName FROM User WHERE user.user_id=commission.user_id)
   ||SubString(commission_number,4,8000)

There's also right(commission_number,-3) to get all but the first 3 chars from that.

I'm not sure what you're trying to do with
WHERE commission_number in (1,2,3,4,5,6,7,8) ?

3 Comments

Do you need to TRIM UserName before concatenation?
If the commission number has a different (longer) prefix than bar this will do the wrong thing.
Yes, if that was a requirement then you'd need to look towards using replace if you know what you're replacing, otherwise your technique of looking for the first / is a good one :)
1

This illustrates how the data will look after the update. I use this kind of select statement a lot when I'm working with string functions.

select c.commission_number, c.user_id, 
       (select username 
        from "user" 
        where "user".user_id = c.user_id) username,
       overlay(c.commission_number 
               placing (select username 
                        from "user" 
                        where "user".user_id = c.user_id) 
               from 1 for 3)
from commission c;

Now you can write the update statement in terms that you know will work.

update commission 
set commission_number = overlay(commission_number 
                                placing (select username 
                                         from "user" 
                                         where "user".user_id = commission.user_id) 
                                from 1 for 3);

Comments

1

Try this query:

 update commission
 set commission_number=user_table.user_name+
  SUBSTRING(SUBSTRING(commission.commission_number,CHARINDEX('/',commission.commission_number,1)+0,LEN(commission.commission_number))
                ,1
                ,CHARINDEX('/',SUBSTRING(commission.commission_number,CHARINDEX('/',commission.commission_number,1)+1,LEN(commission.commission_number)),1)+LEN(commission.commission_number))
 from commission
 inner join user_table on user_table.user_id=commission.user_id

Verify on Sql Fiddle

Took a bit of time to show on SQL Fiddle

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.