I have a table of users which has a username column consisting of a six digit number e.g 675381, I need to append a zero to each of these usernames e.g. 0675381 would be the final output of the previous example, is there a query that could handle this?
Thanks, user brian got it first but I should have said prepend rather than append, he seems to have deleted his post now though! Cheers.
Swanny
2008-10-01 08:13:58
you provided an example: 675381 should become 0675381
daniels
2008-10-01 09:35:36
A:
what type is the column of?
if it's string type, try something like this:
UPDATE your_table SET column_name=concat('0',column_name);
f13o
2008-10-01 08:07:29
A:
You mean "prepend" ? i.e. add it on the front?
Is the column numeric? Do you always want 7 characters output?
Assuming that, something like this would work for a query:
select LPAD(CONVERT(username, CHAR), 7, '0')
If the column is characters, the CONVERT() part is unnecessary, just LPAD the username.
If you want to permanently modify the value in the table, you'll need to ensure the column is a character type and UPDATE using the above.
Mike Woodhouse
2008-10-01 08:09:59
Yeah, I meant prepend, it was just a varchar so daniels solution is perfect. Thanks.
Swanny
2008-10-01 08:19:33
A:
You might want to use CONCAT_WS('', '0', Username) because if there is a null value, then you'll end up with NULL instead of '0'. This probably isn't a problem, but something I've learnt the hard way.
Darryl Hein
2008-10-01 22:43:54