| Author |
Topic |
|
albertkohl
Aged Yak Warrior
740 Posts |
Posted - 2007-09-27 : 17:21:38
|
| Okay, so i've been working in foxpro for a while, but now we're moving on to bigger stronger faster. my question is, in foxpro, i used to be able to fix some name formatting issues with two commands:replace all fname with subs(name,1,at(' ',name))replace all lname with subs(name,(at(' ',name)+1),(30-at(' ',name))) this basically locates the space in the name on each record, and replaces the first name with 0-space and the last with space-end of fieldlittle cude, but it gets the job done. so basically i'm looking for the same solution but for t-sql, i tried something like: update t1set name=substring(name,1,at(' ',name))from temp t1with no luck, AT() is not an expression in sql, so what is out there that's like it? |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-09-27 : 17:26:47
|
| Didnt get you.. can you pose some sample data and how you want it to look after the update?Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
albertkohl
Aged Yak Warrior
740 Posts |
Posted - 2007-09-27 : 17:35:38
|
| say my name for example, i have some dirty data that in the table it's like this:|Fname |Lname |Name || | |Albert Kohl |but i want to correct it to: |Fname |Lname |Name ||Albert |Kohl |Albert Kohl |follow me? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-09-27 : 17:36:52
|
| Could you post your data with code tags around it to retain formatting rather than what you used?Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
albertkohl
Aged Yak Warrior
740 Posts |
Posted - 2007-09-27 : 17:42:10
|
| [code]|Fname |Lname |Name || | |Albert Kohl |but i want to correct it to: |Fname |Lname |Name ||Albert |Kohl |Albert Kohl |[/code]guess i should have previewed :o( |
 |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-09-27 : 17:43:19
|
| Would the name always have only 2 words or can there be names like "Albert Kohl Jr." or "Albert Kohl III"?Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
albertkohl
Aged Yak Warrior
740 Posts |
Posted - 2007-09-27 : 17:45:24
|
| could have suffixes, but for the most part, it's 2 words. honestly, if i can just get the albert part into the fname and the rest into lname, it would work for now. but i'm open to more elaborate parsing |
 |
|
|
albertkohl
Aged Yak Warrior
740 Posts |
Posted - 2007-09-27 : 17:48:47
|
| if i got the sytex to just locate that space, i can build off of that and come up with the rest. (like testing to see if it's formatted as fname middle initial lname, or if there are prefix/suffixes, etc) |
 |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
|
|
albertkohl
Aged Yak Warrior
740 Posts |
Posted - 2007-09-27 : 18:07:06
|
| okay, but looks like i pass it the text, how would i pass it a whole table? |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-09-27 : 18:23:39
|
Here is one super simple way: DECLARE @Table TABLE (Fname VARCHAR(50), Lname VARCHAR(50), Name VARCHAR(100))INSERT @TableSELECT '', '', 'Albert Kohl'UPDATE @TableSET FName = LEFT(Name, CHARINDEX(' ', Name) - 1), LName = RIGHT(Name, LEN(Name) - CHARINDEX(' ', Name))SELECT *FROM @Table |
 |
|
|
albertkohl
Aged Yak Warrior
740 Posts |
Posted - 2007-09-27 : 18:47:19
|
Okay, so if i use this exact code, it works perfectly, so i tweaked it a little:--DECLARE @Table tempdb (Fname VARCHAR(50), Lname VARCHAR(50), Name VARCHAR(100))--INSERT @Table--SELECT '', '', 'Albert Kohl'UPDATE tempdbSET FName = LEFT(Name, CHARINDEX(' ', Name) - 1), LName = RIGHT(Name, LEN(Name) - CHARINDEX(' ', Name))FROM tempdb but i get the following error:Server: Msg 536, Level 16, State 3, Line 6Invalid length parameter passed to the substring function.The statement has been terminated.i also tried turning back on the declare line, and got this instead:Server: Msg 170, Level 15, State 1, Line 1Line 1: Incorrect syntax near 'Fname'. |
 |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-09-27 : 18:57:10
|
[code]UPDATE tempdbSET FName = LEFT(Name, CHARINDEX(' ', Name) - 1), LName = RIGHT(Name, LEN(Name) - CHARINDEX(' ', Name))FROM tempdb[/code]Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
albertkohl
Aged Yak Warrior
740 Posts |
Posted - 2007-09-27 : 18:58:42
|
| Server: Msg 536, Level 16, State 3, Line 1Invalid length parameter passed to the substring function.The statement has been terminated.:o( |
 |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-09-27 : 19:02:11
|
| Obviously there is some data that is conflicting with the Update. If you look at the data do you see any particular rows of data that stand out, that are not the regular 'fname lastname' type?Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
albertkohl
Aged Yak Warrior
740 Posts |
Posted - 2007-09-27 : 19:09:14
|
| couple examples:Berkom R VanBernalBhattacharyaBlakeBlawnBocholinBonaventure Tanas for the first,middle,last, as i said, it's cool for it to do something like: |albert w kohl| converts to: |albert|w kohl|is that's what maybe throughing it off? |
 |
|
|
albertkohl
Aged Yak Warrior
740 Posts |
Posted - 2007-09-27 : 19:20:18
|
if i just run: UPDATE tempdbSET LName = RIGHT(Name, LEN(Name) - CHARINDEX(' ', Name))it does that fine, and i checked it and it's cooli am noticing that some of the first names are just a first letter, like |A Kohl| would that mess it up too? |
 |
|
|
albertkohl
Aged Yak Warrior
740 Posts |
Posted - 2007-09-27 : 19:28:18
|
YESSS i just changed it to:UPDATE tempdbSET FName = rtrim(LEFT(Name, CHARINDEX(' ', Name))) LName = RIGHT(Name, LEN(Name) - CHARINDEX(' ', Name))and it did the job. tx alot!!!! |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-28 : 02:44:39
|
[code]UDPATE Table1SET firstName = dbo.fnParseString(-1, ' ', fullName), lastName = dbo.fnParseString(-2, ' ', fullName)UDPATE Table1SET firstName = dbo.fnParseString(-1, ' ', fullName), lastName = dbo.fnParseString(1, ' ', fullName)[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|