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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 search string

Author  Topic 

syamkarani
Starting Member

3 Posts

Posted - 2003-07-08 : 05:00:38
i have skills column in that skills are seperated with , how can i write querry for diffrent types of search like and or fullword ?

Amethystium
Aged Yak Warrior

701 Posts

Posted - 2003-07-08 : 05:19:19
Having different skills stored in the same column with ',' separator is bad news.

I think it would be a better idea to separate your skills in store them in a separate table.

Can you do that?


Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-07-08 : 10:55:42
Wow...spent way more time than I thoght...maybe SQL Scrub should be the winner...

EDIT: I swear we did this the other day though..

Also I was wondering how to get out of using a cursor when you have n number of "columns" contained in a column

Anyway this should do it for you.



USE Northwind
GO

CREATE TABLE myTable99 (myKey char(1) NOT NULL, col1 varchar(8000))
GO

INSERT INTO myTable99 (myKey, col1)
SELECT 'A', 'Window Washer, Application DBA, SQL Scrub' UNION ALL
SELECT 'B', 'SQL MVP, SQL MVY, SQL Pimp'
GO


CREATE PROC mySPROC AS

DECLARE @myKey char(1), @col1 varchar(8000), @x int, @NewString varchar(255)

DECLARE @tmpTable Table (myNewKey char(1), skill varchar(256))

SET NOCOUNT ON

SELECT @x = -1

DECLARE csrParser CURSOR READ_ONLY FOR
SELECT myKey, Col1 FROM myTable99

OPEN csrParser

FETCH NEXT FROM csrParser
INTO @myKey, @col1

SELECT @x = charindex(',',@col1,1)

WHILE @@FETCH_STATUS = 0
BEGIN
WHILE @x <> 0
BEGIN
SELECT @NewString = SUBSTRING(@col1,1,@x-1)
INSERT INTO @tmpTable(myNewKey, skill) SELECT @myKey,LTRIM(RTRIM(@NewString))
SELECT @Col1 = SUBSTRING(@Col1,@x+1,LEN(@col1)-LEN(SUBSTRING(@col1,1,@x+1)))
SELECT @x = charindex(',',@col1,1)
END

INSERT INTO @tmpTable(myNewKey, skill) SELECT @myKey,LTRIM(RTRIM(@Col1))
FETCH NEXT FROM csrParser
INTO @myKey, @col1
SELECT @x = charindex(',',@col1,1)
END

SELECT myNewKey, skill FROM @tmpTable

CLOSE csrParser
DEALLOCATE csrParser

SET NOCOUNT OFF
Go

EXEC mySPROC
GO

DROP PROC mySPROC
GO

DROP TABLE myTable99
GO



Brett

8-)

Edited by - x002548 on 07/08/2003 10:56:47
Go to Top of Page

Amethystium
Aged Yak Warrior

701 Posts

Posted - 2003-07-08 : 11:18:20
I wonder if he will restructure his data and have the skills separate.

Sometimes I feel that people make life difficult for themselves. Maybe it's just me, but intuition told me that the skills should be in a separate table. Much easier than having to write a SP for searching with 'AND' and 'OR' etc.

Couldn't test your code though. Really strange, when I tried running your code on SQL Server 7 it failed... no surprised there....
I tried running it on the SQL Server 2000 but it wouldn't allow me to create a table on the Northwind DB

Anyway, I am sure it works..... but was it not possible to use a tally table instead of a cursor?


Shadow to Light
Go to Top of Page

Amethystium
Aged Yak Warrior

701 Posts

Posted - 2003-07-08 : 11:38:08

CREATE TABLE #TALLY (UID INT)

DECLARE @COUNTER INT
SET @COUNTER = 1
WHILE @COUNTER <=1000
BEGIN
INSERT INTO #TALLY(UID)
SELECT @COUNTER
SET @COUNTER = @COUNTER +1
END

DECLARE @SKILLS VARCHAR(2000)
SELECT @SKILLS = 'FOOT BALL,CRICKET,TENNIS,GOLF,SNOOKER,BASKET BALL'

SELECT SUBSTRING(',' + @SKILLS + ',', UID, CHARINDEX(',' , ',' + @SKILLS + ',' , UID) - UID)
FROM #TALLY
WHERE UID <= LEN(', ' + @SKILLS + ', ')
AND SUBSTRING(',' + @SKILLS + ', ', UID - 1, 1) = ','
AND CHARINDEX(', ' , ',' + @SKILLS + ', ' , UID) - UID > 0



http://www.sqlteam.com/item.asp?ItemID=5857

Shadow to Light

Edited by - Amethystium on 07/08/2003 11:44:16
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-07-08 : 11:53:40
quote:

I tried running it on the SQL Server 2000 but it wouldn't allow me to create a table on the Northwind DB



Why? With what ID do you connect, and with what permissions?

Just change myTable99 to #myTable99 and that should be all you need to see it work.



Brett

8-)
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-07-08 : 12:00:12
Oh yeah, that's right..the tally table...

I like that soooooooooo much better than mine...it's definetly slicker..


Which one do you think performs better though...

I bet your's does and NO Cursor!

WOOHOO!



Brett

8-)
Go to Top of Page

Amethystium
Aged Yak Warrior

701 Posts

Posted - 2003-07-08 : 12:00:48
Not to worry...

The local server that I connect to has SQL Server 7 which is what I use to do most of my work. Unfortunately, the SQL Server 2000 which is on a different server only gives me access to specific client DBs if you know what I mean. Even Northwind is beyond my reach. How lame is that.

Shadow to Light
Go to Top of Page

Amethystium
Aged Yak Warrior

701 Posts

Posted - 2003-07-08 : 12:02:22
quote:

Oh yeah, that's right..the tally table...

I like that soooooooooo much better than mine...it's definetly slicker..

Which one do you think performs better though...

I bet your's does and NO Cursor!

WOOHOO!



What is your problem with me Brett?

Shadow to Light
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-07-08 : 12:19:40
Now I'm wondering a performance...because to use the tally table against another table, don't we have to cartesion the tally table against every row in the table to convert?

Not that's bad...but it could get huge...


SELECT LTRIM(RTRIM(SUBSTRING(',' + col1 + ',', UID, CHARINDEX(',' , ',' + col1 + ',' , UID) - UID)))
FROM #TALLY, myTable99
WHERE UID <= LEN(', ' + col1 + ', ')
AND SUBSTRING(',' + col1 + ', ', UID - 1, 1) = ','
AND CHARINDEX(', ' , ',' + col1 + ', ' , UID) - UID > 0




Brett

8-)
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-07-08 : 12:23:50
quote:

What is your problem with me Brett?



What? No offense meant...It's just that this is a reoccuring question, and I remebered we kicked it a different way than my cursor model..

So yeah right, the tally table (he said with much unabashed joy).

It's something I need to employye more, esp when it looks like a cursor (BOOO) is the only answer.

No problems here...and no offense meant



Brett

8-)

EDIT: I guess we really do need a sarcasm detector....It would have registered 0.

I can see how it would be take otherwise...sorry..



Edited by - x002548 on 07/08/2003 12:26:08
Go to Top of Page

Amethystium
Aged Yak Warrior

701 Posts

Posted - 2003-07-08 : 13:03:57
I think my sarcasm detector broke down quite a while back.
Need to buy a copy of Sam's Get your Sarcasm detector fixed in 24 hours

quote:

So yeah right, the tally table (he said with much unabashed joy).
It's something I need to employye more, esp when it looks like a cursor (BOOO) is the only answer.



You're not the only one Brett, but you know what, the next time you come across this question you will do it with a Tally table...

There is so much to learn it is depressing sometimes. Are we ever allowed to feel proud of what we know or is there always a smart ass around the corner with a better solution?!

... there is always a smart ass around every corner

Shadow to Light
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-07-08 : 13:17:44
quote:

There is so much to learn it is depressing sometimes. Are we ever allowed to feel proud of what we know



Nope. Now expand that concept to many different platforms and O/s's..

Yikes!

The more you know, the more you know you don't know!



Brett

8-)
Go to Top of Page
   

- Advertisement -