Thread: another simple SQL question
Ok here is another simple question from a novice.... Here is what my table looks like firstname lastname fullname ---------- ---------- ----------- smith, john green, susan white, jeff How can I break the fullname field into firstname lastname fields so it looks like the following: firstname lastname fullname --------- --------- --------- john smith smith, john susan green green, susan jeff white white, jeff Please let me know. Sorry for such simple novice questions, I appreciate your support. THANKS!
On Mon, June 25, 2007 11:28 am, Joshua said: > Ok here is another simple question from a novice.... > > Here is what my table looks like > > firstname lastname fullname > ---------- ---------- ----------- > smith, john > green, susan > white, jeff > > > How can I break the fullname field into firstname lastname fields so it > looks like the following: > > firstname lastname fullname > --------- --------- --------- > john smith smith, john > susan green green, susan > jeff white white, jeff > > Please let me know. Sorry for such simple novice questions, I appreciate > your support. The best way to handle this would actually be to break it _before_ putting it in to the database. In fact, you can just have the first and last name fields then and get the 'fullname' from combining the two. (Assuming you don't care about middle names, appliations, or the anything else.) That's just a comment: I don't know what stage you are in on this database project. Assuming you have the database and need to work with it my response to the above would be to write a perl script to go through and populate the first and last name fields. There may be an easier way though. Daniel T. staal --------------------------------------------------------------- This email copyright the author. Unless otherwise noted, you are expressly allowed to retransmit, quote, or otherwise use the contents for non-commercial purposes. This copyright will expire 5 years after the author's death, or in 30 years, whichever is longer, unless such a period is in excess of local copyright law. ---------------------------------------------------------------
On Tue, 26 Jun 2007 01:28:40 am Joshua wrote: > Ok here is another simple question from a novice.... > > Here is what my table looks like > > firstname lastname fullname > ---------- ---------- ----------- > smith, john > green, susan > white, jeff > > > How can I break the fullname field into firstname lastname fields so it > looks like the following: > > firstname lastname fullname > --------- --------- --------- > john smith smith, john > susan green green, susan > jeff white white, jeff > > Please let me know. Sorry for such simple novice questions, I appreciate > your support. > update yourtable set firstname= substring(fullname,1,position(',' in fullname)-1) ,lastname= substring(fullname, position(', ' in fullname)+2, char_length(fullname)); glenn > THANKS! > > ---------------------------(end of broadcast)--------------------------- > TIP 6: explain analyze is your friend
Try this: SELECT substr(fullname, 1, position(',' IN fullname)-1) AS first, trim(substr(fullname, position(',' IN fullname)+1, length(fullname))) AS last FROM table_name; Joshua wrote: > Ok here is another simple question from a novice.... > > Here is what my table looks like > > firstname lastname fullname > ---------- ---------- ----------- > smith, john > green, susan > white, jeff > > > How can I break the fullname field into firstname lastname fields so it > looks like the following: > > firstname lastname fullname > --------- --------- --------- > john smith smith, john > susan green green, susan > jeff white white, jeff > > Please let me know. Sorry for such simple novice questions, I appreciate > your support. > > THANKS! > > ---------------------------(end of broadcast)--------------------------- > TIP 6: explain analyze is your friend > > >
hey joshua On Tue, 26 Jun 2007 02:04:20 am Derrick Betts wrote: > Try this: > SELECT substr(fullname, 1, position(',' IN fullname)-1) AS first, > trim(substr(fullname, position(',' IN fullname)+1, length(fullname))) > AS last > FROM table_name; > do you understand the difference between what derrick has put here, and my post? Derricks displays the data, mine sets it so that you can then just use a simple select, so make sure you pick which will be useful for your usecase. If you can use the update form to populate first and last names, then get rid of the 'fullname' field and only populate the first and last names from now on, that would probably also be smart. (we call this normalising... getting rid of redundant data) also note derricks use of TRIM to clean of the white space... thats also a good idea i didnt use in my example glenn > Joshua wrote: > > Ok here is another simple question from a novice.... > > > > Here is what my table looks like > > > > firstname lastname fullname > > ---------- ---------- ----------- > > smith, john > > green, susan > > white, jeff > > > > > > How can I break the fullname field into firstname lastname fields so it > > looks like the following: > > > > firstname lastname fullname > > --------- --------- --------- > > john smith smith, john > > susan green green, susan > > jeff white white, jeff > > > > Please let me know. Sorry for such simple novice questions, I appreciate > > your support. > > > > THANKS! > > > > ---------------------------(end of broadcast)--------------------------- > > TIP 6: explain analyze is your friend > > ---------------------------(end of broadcast)--------------------------- > TIP 9: In versions below 8.0, the planner will ignore your desire to > choose an index scan if your joining column's datatypes do not > match