K_U K_U -4 years ago 63
MySQL Question

How to change the structure of a name field in mysql

I have the following problem.

My table, say tab1, has name column as follows "LastName, FirstName". I want to make it so that the column becomes "FirstName LastName".

Any ideas on how this is to be done? Note that there is no comma present, but i guess that can be easily removed once I figure out how to actually flip the first and the last names.

Any help would be appreciated.

Thanks.

Answer Source

replace @NAME with your real value:

SELECT TRIM(SUBSTR(@NAME, LOCATE(",", @NAME) + 1)) AS prename, TRIM(SUBSTR(@NAME, 1, LOCATE(",", @NAME) - 1)) AS surename

This will extract the prename and surename part, now you can insert/modify the data as you want to.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download