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
 is there function similar to : if x in (2,40,50,1)

Author  Topic 

sarahmfr
Posting Yak Master

214 Posts

Posted - 2011-05-16 : 22:54:25
I have a table jobs with columns jobcategory, careerlevel, jobtype, educationallevel and other columns

those are integer columns (foreign keys to other tables ) eg jobcategory table (categoryid, categoryname)

I am having a search page the user wants to pull all jobs with jobcategory is any of the following(1,2,3,5,8,9)

and jobtype is any of the following (10,12,14,etc)

is there an easy way to do that. Is their a function similar to

if x in (1,3,5,6) do so an so

stored procedure recieves the list of possible values as a string i.e.
categoryoptions ="1,2,3,5,8,9"
jobtypeoptions="10,12,14"

I do not want to loop through those values. Performance wise will not be good.
I can not use contains because I have a full text index already on a column named keywords.

Thanks

sarah

sarahmfr
Posting Yak Master

214 Posts

Posted - 2011-05-17 : 00:08:30
I found a way to do it
but I need a faster way to do the same task

declare @categorylist varchar (100)
declare @jobtypelist varchar
set @categorylist='1,2,3,4,7'
set @jobtypelist='6,9,11'

set @categorylist=REPLACE(@categorylist,',', ' or jobcategory=')
set @categorylist='jobcategory= '+@categorylist



sarah
Go to Top of Page

chakadoll
Starting Member

1 Post

Posted - 2011-05-17 : 00:49:24
I have a data.frame which has two columns as the following:
one two
1 2
3 4
5 6

I used the "paste" function to create the third column: three <- paste(one,'-',two,sep="")
so the data.frame is like this now:
one two three
1 2 1-2
3 4 3-4
5 6 5-6

That's all I know, Be back for more!


Thanks
Chaka
http://www.sqlhosting.net/
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-05-17 : 05:31:05
quote:
Originally posted by chakadoll

I have a data.frame which has two columns as the following:
one two
1 2
3 4
5 6

I used the "paste" function to create the third column: three <- paste(one,'-',two,sep="")
so the data.frame is like this now:
one two three
1 2 1-2
3 4 3-4
5 6 5-6

That's all I know, Be back for more!


Thanks
Chaka
http://www.sqlhosting.net/


What did you mean by paste function?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-05-17 : 07:11:56
I can think of couple of different ways to try to do this. First is to use the like operator:

where
','+ @categorylist +',' like '%',+cast(jobcategory as varchar(32)) + ',%'
This may not be any faster than the method you tried. Another alternative is to split the comma-separated list into a table and then join with that table. To split the comma-separated list, you can install the function listed in Fig. 21 of this page: http://www.sqlservercentral.com/articles/Tally+Table/72993/ and use it.

BUT, if you really really want great performance, you have to make sure that you have the appropriate indexes on the table. Post your table DDL, including indexes, and there are people on this forum who can take one look at it and say something like "add two indexes, one on the categoryid column and another on the jobtype column(*)", and like magic, the query will run a lot faster.

Brett's blog here has info on how to get the table DDL: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

(*) The indexes I have listed above are purely fictional. Any resemblance to real indexes, clustered, or non-clustered, is purely coincidental.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-05-17 : 08:59:18
quote:
Originally posted by sunitabeck

I can think of couple of different ways to try to do this. First is to use the like operator:

where
','+ @categorylist +',' like '%',+cast(jobcategory as varchar(32)) + ',%'
This may not be any faster than the method you tried. Another alternative is to split the comma-separated list into a table and then join with that table. To split the comma-separated list, you can install the function listed in Fig. 21 of this page: http://www.sqlservercentral.com/articles/Tally+Table/72993/ and use it.

BUT, if you really really want great performance, you have to make sure that you have the appropriate indexes on the table. Post your table DDL, including indexes, and there are people on this forum who can take one look at it and say something like "add two indexes, one on the categoryid column and another on the jobtype column(*)", and like magic, the query will run a lot faster.

Brett's blog here has info on how to get the table DDL: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

(*) The indexes I have listed above are purely fictional. Any resemblance to real indexes, clustered, or non-clustered, is purely coincidental.


Another method would be to make use of dynamic sql

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-05-17 : 09:55:18
I would use a udf




IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[udf_Table]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[udf_Table]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [dbo].[udf_Table](@ParmList varchar(8000), @Delim varchar(20))
RETURNS @table TABLE
(Parameter varchar(255))

AS

/*
SELECT * FROM dbo.udf_Table( 'a,b,c', ',')
*/

BEGIN
DECLARE @x int, @Parameter varchar(255)

WHILE CHARINDEX(@Delim, @ParmList)-1 > 0
BEGIN
INSERT INTO @table(Parameter) SELECT SUBSTRING(@ParmList,1,CHARINDEX(@Delim, @ParmList)-1)
SELECT @ParmList = SUBSTRING(@ParmList,CHARINDEX(@Delim, @ParmList)+1, LEN(@ParmList)-CHARINDEX(@Delim,@ParmList))
END
INSERT INTO @table(Parameter) SELECT @ParmList
RETURN
END

GO



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -