473,406 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

SQL Split and insert

16
Hello all, I am trying to take a column of names currently seperated by a comma and split it into two seperate columns being firstname and last name.

This is what I have so far and it is failing with the error:
Server: Msg 207, Level 16, State 3, Line 10
Invalid column name 'GuarName'.

This is my script:
Expand|Select|Wrap|Line Numbers
  1. DECLARE @iPosition char(1)
  2. DECLARE @FName varchar(500)
  3. DECLARE @LName varchar(500)
  4. select distinct GuarName from patientacc
  5. where FinCode1 is null 
  6. and FinCode2 is null 
  7. and GuarName is not null 
  8. and convert(smalldatetime,[Last Seen], 101) > '1/1/2000'
  9.  
  10. set @FName = GuarName
  11. set @iPosition = ','
  12. if (@FName is not null)
  13.  begin
  14.      WHILE (LTrim(RTrim(@FName)) <>'')
  15.             BEGIN
  16.                 Set @iPosition = CHARINDEX(',',LTrim(RTrim(@FName)))
  17.                 if (@iPosition > 1)
  18.                   begin
  19.                         set @LName = Left(LTrim(RTrim(@FName)), @iPosition-1)
  20.                         set @FName = RIGHT(LTrim(RTrim(@FName)), LEN(Ltrim(RTrim(@FName)))-@iPosition)    
  21.                   end
  22.                 else
  23.                   begin
  24.                         set @LName = @FName
  25.                         set @FName = ''
  26.                   end
  27.                 if (Len(@LName) <> 0 )
  28.                   begin
  29. --                     insert into PatientAcc(GuarLName) Values(@LName)
  30. select @LName
  31. end end end
Hoping maybe some one could help me out on this.
Thanks a million,
Tim
Jan 18 '08 #1
5 2600
Delerna
1,134 Expert 1GB
CHARINDEX(',',fieldname) returns the position of the first comma in a string

so left(fieldname,CHARINDEX(',',fieldname)-1) will return all the characters on the left of the comma

I'll let you figure out how to get the right hand side
:)

Regards
Jan 18 '08 #2
Delerna
1,134 Expert 1GB
OOPS
Just noticed you are already using charindex

line 4 select distinct GuarName from patientacc
line 29 Insert into PatientAcc(GuarLName) Values(@LName)

are the meant to be the same field, I guess not but.....


and should this set @FName = GuarName
be something like set @FName=(Select GuarName from .....)
Jan 18 '08 #3
tmeers
16
Well I guess that kinda helped but I did figure it out so kudos. Here is my code now, and it works well except for the fact that the machine it's running on cannot run it on the 16000+ row table oh well time to try it on another machine.

Expand|Select|Wrap|Line Numbers
  1. declare @FName varchar(50), @LName varchar(50), @iPosition char(1), @pt_id int
  2. declare @get_name cursor
  3. set @get_name = cursor for 
  4. select pt_id, GuarName from patientacc
  5. where GuarName is not null
  6.  
  7. open @get_name
  8.  
  9. fetch next from @get_name INTO @pt_id, @FName
  10.  
  11. while @@fetch_status = 0
  12. fetch next from @get_name 
  13.  
  14.  
  15. --SET @FName = GuarName
  16.  
  17. set @iPosition = ','
  18. if (@FName is not null)
  19.  begin
  20.      WHILE (LTrim(RTrim(@FName)) <>'')
  21.             BEGIN
  22.                 Set @iPosition = CHARINDEX(',',LTrim(RTrim(@FName)))
  23.                 if (@iPosition > 1)
  24.                   begin
  25.                         set @LName = Left(LTrim(RTrim(@FName)), @iPosition-1)
  26.                         set @FName = RIGHT(LTrim(RTrim(@FName)), LEN(Ltrim(RTrim(@FName)))-@iPosition)    
  27.                   end
  28.                 else
  29.                   begin
  30.                         set @LName = @FName
  31.                         set @FName = ''
  32.                   end
  33.                 if (Len(@LName) <> 0 )
  34.                   begin
  35.             Update patientacc
  36.             set guarlname = @LName
  37.             where pt_id = @pt_id
  38.                      --insert into PatientAcc(GuarLName) Values(@LName)
  39. --select @LName
  40. end end end
  41.  
  42. close @get_name
  43. deallocate @get_name
Jan 18 '08 #4
Delerna
1,134 Expert 1GB
You could try something like this, it will run a lot faster for you.
Set based updates (well set based anything really) will always be faster than cursors.
set based queries are easier to read and understand than cursors and therefore easier to modify at a later date when you have forgotten how it works.
never use a cursor unless you have exhaused set based possibilities
cursors should be your last resort.

Expand|Select|Wrap|Line Numbers
  1.  
  2. UPDATE patientacc SET
  3. guarLname=ltrim(   left(  guarname,charindex(',',guarname)-1  )    )
  4. ,guarname=rtrim(  right( guarname,len(guarname)-charindex(',',guarname)+1 )  )
  5. where charindex(',',guarname) > 1
  6.  
  7.  
you might need to play around with the +1 and -1 to get the correct result, I can never remember straight off the top of my head.
Try it with a select first untill you get it right

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT Top 10
  3.    ltrim(     left(  guarname,charindex(',',guarname)-1  )      )
  4.    ,rtrim(  right(  guarname,len(guarname)-charindex(',',guarname)+1  )  )
  5. FROM patientacc 
  6. WHERE charindex(',',guarname) > 1
  7.  
  8.  
Jan 18 '08 #5
tmeers
16
PERFECT! well for the most part. Thank you VERY much for the help and best of all I have learned a bit about it. It's alway nice to see people help and teach at the same time. Oh and I have to do two -1's to get it here is the final and working code.

UPDATE patientacc SET
guarLname=ltrim( left( guarname,charindex(',',guarname)-1 ) )
,guarFname=rtrim( right( guarname,len(guarname)-charindex(',',guarname)-1 ) )
where charindex(',',guarname) > 1

Thanks a million. Now I'm off to convert some more data.
Tim
Jan 21 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: middletree | last post by:
Not really sure what the problem is, perhaps I am using split wrong. Page should get form fields form previous page and some of them will go into a tabel called Personal. Other items, to resolve...
8
by: middletree | last post by:
Sorry for starting a new thread, but the original was 24 hours ago, and I am afraid it won't get seen. In the original, Ray advised me to change if len(strPeopleID)>0 then arrPeople =...
0
by: j | last post by:
Hi, Anyone out there with binary search tree experience. Working on a project due tomorrow and really stuck. We need a function that splits a binary tree into a bigger one and smaller one(for a...
3
by: twdo | last post by:
Let me see if I can explain my situation clearly. I have a table with the columns: answer_id, question_id, member_id, answer - answer_id is the primary key for the table. - question_id...
5
by: Willem | last post by:
Hello I am quite hopeless and of course a newbe. The situation: Sql2k / query I would like it ot break down the following string: 2004 Inventory:Ex.Plant Farm1:1st Cut:Premium:0094
1
by: Mark Goldin | last post by:
I am trying to split cells in a table. I am selecting a cell and I want to break into two. There is no "split" command available anywhere. What would you suggest?
0
by: Eduardo Luiz | last post by:
Hi, im brazilian.. Sorry my english.. I need a component to manager sounds.. Split, Joiner... i have one sound.. and i need insert a other sound (ads) in a second or frame position.. Ex: ...
9
by: MrHelpMe | last post by:
Hello again experts, I have successfully pulled data from an LDAP server and now what I want to do is drop the data into a database table. The following is my code that will insert the data but...
1
by: clayalphonso | last post by:
Here is the code: <% dim testArray, testArray2 dim Conn, rs dim sSQL, sConnString 'response.write request.form("sel1") 'testArray = split(request.form("sel1"),",") 'for each gidstuff In...
1
by: Mike1961 | last post by:
Hi all. I have problem with this asp code: strSplitDati = Split(request.Form("dati"), ",") for i = LBound(strSplitDati) to (INT(UBound(strSplitDati)/4)-1)*4 step 4 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.