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.
| 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 columnsthose 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 toif x in (1,3,5,6) do so an sostored 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.Thankssarah |
|
|
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 taskdeclare @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= '+@categorylistsarah |
 |
|
|
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 two1 23 45 6I used the "paste" function to create the third column: three <- paste(one,'-',two,sep="")so the data.frame is like this now:one two three1 2 1-23 4 3-45 6 5-6That's all I know, Be back for more!ThanksChakahttp://www.sqlhosting.net/ |
 |
|
|
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 two1 23 45 6I used the "paste" function to create the third column: three <- paste(one,'-',two,sep="")so the data.frame is like this now:one two three1 2 1-23 4 3-45 6 5-6That's all I know, Be back for more!ThanksChakahttp://www.sqlhosting.net/
What did you mean by paste function?MadhivananFailing to plan is Planning to fail |
 |
|
|
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. |
 |
|
|
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 sqlMadhivananFailing to plan is Planning to fail |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2011-05-17 : 09:55:18
|
I would use a udfIF 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]GOSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE 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 RETURNENDGO Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
|
|
|
|
|