Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 If...else if in cursor

Author  Topic 

Grifter
Constraint Violating Yak Guru

274 Posts

Posted - 2011-09-06 : 04:55:19
Hi

When i try and do an if else if in a cursor it keeps underlining the else part saying incorrect syntax near 'else', why does it keep doing this, I am confused where I need to use begin...end as well in cursor and at if statements:


declare @ExtractStr VARCHAR(100), @RawData VARCHAR(100)
declare c1 cursor
for
select column1
from dbo.Sheet1$
open c1
fetch next from c1 into @RawData
while @@FETCH_STATUS = 0
BEGIN
if CHARINDEX(',', @RawData, 0) <> 0 AND CHARINDEX('XYZ', @RawData, 0) <> 0
set @ExtractStr = substring(@RawData, CHARINDEX(' ', @RawData, 0) + 1, len(@RawData))
print 'Remaining string is: ' + substring(@RawData, CHARINDEX(' ', @RawData, 0) + 1, len(@RawData))
else if CHARINDEX(',', @RawData, 0) = 0
print 'Value in c1 is: ' + @RawData
end
fetch next from c1 into @RawData
close c1
deallocate c1


I have rows in column one where data is as follows:

column1
=======
232wewe
456ffff
454tyuu
721tnji, 583sdfs

And I am trying to filter and display any rows like the bold one above basically showing the string after the comma. Eventually I would like to copy this value into a new row and copy the other data in that row to create a duplicate, like this:

column1
=======
232wewe
456ffff
454tyuu
721tnji
583sdfs


Any ideas, is there a better way than a cursor?

G

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-06 : 05:11:07
yep see one method here

http://visakhm.blogspot.com/2010/01/multipurpose-apply-operator.html

and parsevalue can be found below link
http://visakhm.blogspot.com/2010/02/parsing-delimited-string.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -