SQL Server - Multiple Filter added Programatically

Asked By Bikash Samal
19-Dec-11 06:23 AM
In my website I search for a word from database and show result in gridview using textbox text.
My textbox filled value from database like: PDF/PPT/EXE.
I have to split the total word separated by '/' and according to that search I've to fill gridview.
But problem is that I do'nt have any idea how many fields are there to search...
Means I found three words from "PDF/PPT/EXE" and fill the gridview by writing query by distinguish 3 like operator and later someone added more search fields to database like "PDF/PPT/EXE/xlsx", then how could I able to know another filter.

Is there any way to add automatically another filter when search criteria increases ?????
I don't any idea about this problem...
Anybody please suggest or give some solution please......


Thanks in adv......
  Vickey F replied to Bikash Samal
19-Dec-11 06:28 AM
For that you can use LINQ like this-

class DynamicPredicates : StudentClass
{
    static void Main(string[] args)
    {
      string filter = "PDF/PPT/EXE/xlsx";
string[] ids = filter .Split('/');
Console.WriteLine("Press any key to exit."); Console.ReadKey(); } static void QueryByID(string[] ids) { var queryNames = from student in students let i = student.ID.ToString() where ids.Contains(i) select new { student.LastName, student.ID }; foreach (var name in queryNames) { Console.WriteLine("{0}: {1}", name.LastName, name.ID); } } }


Try this and let me know.
  Riley K replied to Bikash Samal
19-Dec-11 06:28 AM


The most efficient way to implement this type of search is with a stored procedure. The statement shown here creates a procedure that accepts the required parameters. When a parameter value is not supplied it is set to NULL.

CREATE PROCEDURE ps_Customers_SELECT_NameCityCountry
@Cus_Name varchar(30) = NULL,
@Cus_City varchar(30) = NULL,
@Cus_Country varchar(30) =NULL
AS
SELECT Cus_Name,
     Cus_City,
     Cus_Country
FROM Customers
WHERE Cus_Name = COALESCE(@Cus_Name,Cus_Name) AND
    Cus_City = COALESCE(@Cus_City,Cus_City) AND
    Cus_Country = COALESCE(@Cus_Country,Cus_Country)


Regards
  Bikash Samal replied to Bikash Samal
19-Dec-11 06:32 AM
But I'm using dotnet 2005.
So, no LINQ is there and I'm searching data using storeprocedure....
So, I have to do all in sql server....

Is there any way to do this in sql server.???
  Web Star replied to Bikash Samal
19-Dec-11 06:35 AM
you need to use split paramter with '\' to getting all value and than add all with like keyword in you where search clause with proper column name
you can make search conditon within while loop by using split,
First you split you paramter with '\' and store it into temp table and then get rowcount into @MaxId and set @1 = 1 and start loop to make search condition with all splited value one by one using like search and finaly you can use that serach with where clause of sql query

WHILE @i <=@MaxID
   BEGIN
      SET @Search = @Search + ', colname like (%' + CONVERT(VARCHAR(10), @splitValue) + '%)'
      SET @i = @i + 1
   END
  Bikash Samal replied to Riley K
19-Dec-11 06:41 AM
CREATE PROCEDURE [dbo].[sp_UserExceptions]
@SDate varchar(20),
    @EDate varchar(20),
    @ExcpType varchar(20),
    @Search varchar(20)
AS
BEGIN
if(@ExcpType = 'Usb')
begin
select cp.vchcpaneluname as vchcpaneluname,cp.intnoteid as intnoteid,tu.vchip as vchip,convert(varchar,tu.newdate,103) as date,
replace(convert(varchar(max),tu.vchFile),'%20',' ') as vchfile,count(convert(varchar(max),tu.vchFile)) as usbcount
from dbo.ControlPanelUser cp join tblusbdetails tu on cp.intnoteid = tu.intnoteid where tu.vchFile is not null 
and (tu.vchFile like '%.ppt%' or tu.vchFile 
like '%.pdf%' or tu.vchFile like '%micro%' or tu.vchFile like '%.exe%' or tu.vchFile like '%.aspx%')
and 
(convert(varchar,tu.newdate,112)>=@SDate and 
convert(varchar,tu.newdate,112)<=@EDate) 
group by cp.vchcpaneluname,cp.intnoteid,tu.vchip,convert(varchar(max),tu.vchFile),convert(varchar,tu.newdate,103)
end
========================================
Previously My store procedure was this....
Now I'm storing filter criteria in a table like: "ppt/pdf/exe" and I've to filter criteria from this table..
So, if somebody change the filter area to "ppt/pdf/exe/xlsx" then how could I able to add another like operator in my store procedure programatically....???
How could I split that data and added in like parameter and add more like parameter too???
 
  R B replied to Bikash Samal
19-Dec-11 06:42 AM

 

Hello

 

Made one function as follows

 

    CREATE FUNCTION dbo.Split(@String varchar(8000), @Delimiter char(1))    

    returns @temptable TABLE (items varchar(8000))    

    as    

    begin    

      declare @idx int    

      declare @slice varchar(8000)    

     

      select @idx = 1    

        if len(@String)<1 or @String is null  return    

     

      while @idx!= 0    

      begin    

        set @idx = charindex(@Delimiter,@String)    

        if @idx!=0    

          set @slice = left(@String,@idx - 1)    

        else    

          set @slice = @String    

       

        if(len(@slice)>0) 

          insert into @temptable(Items) values(@slice)    

   

        set @String = right(@String,len(@String) - @idx)    

        if len(@String) = 0 break    

      end  

    return    

    end 

 

After then call query as follows

 

 

declare @Product as varchar(max)

set @Product ='Cha/Coffe' // Added Multple product here

declare @Delimater as char(1)

set @Delimater='/' // Set your split delimater

SELECT * from Product

where ProductName in(select * from dbo.split(@Product,@Delimater))

 

Hope this helpful

 

  R B replied to Bikash Samal
19-Dec-11 06:48 AM

Call above split function as follows in your SP

 

tu.vchFile is not null

and (tu.vchFile in (select * from dbo.split('ppt/pdf/exe/xlsx','/'))

 

  Riley K replied to Bikash Samal
19-Dec-11 06:55 AM


Ok, here is the sample, split the string and pass to like stmnt

DECLARE @TAB TABLE (COL VARCHAR(MAX))
INSERT INTO @TAB VALUES('WIS82763')
INSERT INTO @TAB VALUES('SUN37483')
 
DECLARE @PARAM VARCHAR(MAX)
SET @PARAM = 'WIS82,SUN3'
 
SELECT  COL
FROM    @TAB CROSS JOIN DBO.SPLIT(@PARAM, ',')
WHERE   COL LIKE '%' + VAL + '%'

Regards
  Bikash Samal replied to R B
20-Dec-11 12:38 AM

Thanx for reply....
=======================
tu.vchFile is not null

and (tu.vchFile in (select * from dbo.split('ppt/pdf/exe/xlsx','/'))
======================================================================
Your code is very much correct. But I've to find using like method. I think this code will not applicable to my requirements. Because my code is like: select * from abc where vchdoc like '%ppt%' or vchdoc like '%pdf%' or vchdoc like '%exe%'.... So, in this case how I'll add multiple like to my store procedure.... Is there any way to solve my problem???  

  R B replied to Bikash Samal
20-Dec-11 12:48 AM
hello

can you send me your sp??

So I will check here and give you proper solution
  Bikash Samal replied to R B
20-Dec-11 04:31 AM
Sorry for late reply.....
Here is my store procedure....
=========================================
CREATE PROCEDURE [dbo].[sp_UserExceptions]
@SDate varchar(20),
    @EDate varchar(20),
    @ExcpType varchar(20),
    
AS
BEGIN
if(@ExcpType = 'Usb')
begin
select cp.vchcpaneluname as vchcpaneluname,cp.intnoteid as intnoteid,tu.vchip as vchip,convert(varchar,tu.newdate,103) as date,
replace(convert(varchar(max),tu.vchFile),'%20',' ') as vchfile,count(convert(varchar(max),tu.vchFile)) as usbcount
from dbo.ControlPanelUser cp join tblusbdetails tu on cp.intnoteid = tu.intnoteid where tu.vchFile is not null 
and (tu.vchFile like '%.ppt%' or tu.vchFile 
like '%.pdf%' or tu.vchFile like '%micro%' or tu.vchFile like '%.exe%' or tu.vchFile like '%.aspx%')
and 
(convert(varchar,tu.newdate,112)>=@SDate and 
convert(varchar,tu.newdate,112)<=@EDate) 
group by cp.vchcpaneluname,cp.intnoteid,tu.vchip,convert(varchar(max),tu.vchFile),convert(varchar,tu.newdate,103)
end 
=========================================
  R B replied to Bikash Samal
20-Dec-11 04:48 AM

Hello,

Added yellow background line in your sp

CREATE PROCEDURE [dbo].[sp_UserExceptions]

@SDate varchar(20),

    @EDate varchar(20),

    @ExcpType varchar(20),

    @vchFile varchar(max) = '.pdf/.micro/.exe/.aspx/.ppt',

    @delimeter char(1)='/',

   

AS

BEGIN

if(@ExcpType = 'Usb')

begin

select cp.vchcpaneluname as vchcpaneluname,cp.intnoteid as intnoteid,tu.vchip as vchip,convert(varchar,tu.newdate,103) as date,

replace(convert(varchar(max),tu.vchFile),'%20',' ') as vchfile,count(convert(varchar(max),tu.vchFile)) as usbcount

from dbo.ControlPanelUser cp join tblusbdetails tu on cp.intnoteid = tu.intnoteid where tu.vchFile is not null

and (tu.vchFile in (select * from dbo.split(@vchFile,@delimeter))

and

(convert(varchar,tu.newdate,112)>=@SDate and

convert(varchar,tu.newdate,112)<=@EDate)

group by cp.vchcpaneluname,cp.intnoteid,tu.vchip,convert(varchar(max),tu.vchFile),convert(varchar,tu.newdate,103)

end

 

Remove following line

 

and

 

(tu.vchFile like '%.ppt%' or tu.vchFile like '%.pdf%' or tu.vchFile like '%micro%' or tu.vchFile like '%.exe%' or tu.vchFile like '%.aspx%')

 

 

 

Hope this is helpful !

Thanks

 

 

 

 

 

  Bikash Samal replied to R B
20-Dec-11 05:09 AM
Thanks for reply....

But, I don't think it'll work...
Because 'like' and 'in' are two different way of search....
'in' finds for the particular word....
let me explain..Suppose I have a column that contains "newtextdoc.txt"...
and if i'm trying to find "txt" in that column then i've to use 'like' rather than 'in'....

So, what to do in this case...??
that's my problem.....
I've to dynamically create 'like' which I can't do yet......
If you have any solution then please share......

  R B replied to Bikash Samal
20-Dec-11 07:59 AM

Hello,

First you need to change your Split function as follows

 

USE [Practice]

GO

/****** Object:  UserDefinedFunction [dbo].[Split]    Script Date: 12/20/2011 16:38:28 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

    ALTER FUNCTION [dbo].[Split](@String varchar(8000), @Delimiter char(1))    

    returns @temptable TABLE (pid int,items varchar(8000))   

 

    as   

 

    begin   

 

    declare @idx int   

 

    declare @slice varchar(8000)   

    declare @ids int

  

 

    select @idx = 1   

 

      if len(@String)<1 or @String is null  return   

 

  

        SET @ids =0

    while @idx!= 0   

 

    begin   

 

      set @idx = charindex(@Delimiter,@String)   

 

      if @idx!=0   

BEGIN

      set @slice = left(@String,@idx - 1)   

      SET @ids = @ids + 1

END

      else   

BEGIN

      set @slice = @String   

      SET @ids = @ids + 1

END

    

 

      if(len(@slice)>0)

 

      insert into @temptable(pid,Items) values(@ids,@slice)   

 

  

 

      set @String = right(@String,len(@String) - @idx)   

 

      if len(@String) = 0 break   

 

    end 

 

    return   

 

    end 

 

After then retive data as per your requiremnt you need to store data into temp table as follows

 

    DECLARE @Product AS varchar(max)

    SET @Product ='%a%/%offe%/%il%'

    DECLARE @Delimater AS char(1)

    SET @Delimater='/'

 

    DECLARE @imax INT, @i INT = 1

    CREATE TABLE #TempProduct

    (

        ProductID int,

        ProductName char(30)

    )

     

    SELECT @imax= COUNT(*) from dbo.split(@Product,@Delimater)

    WHILE (@i <= @imax)

    BEGIN

        INSERT INTO #TempProduct (ProductID, ProductName)

          SELECT    ProductID, ProductName

          FROM Product

          WHERE     ProductName LIKE

          (

              select  items from dbo.split(@Product,@Delimater) WHERE  pid = @i

          )

          SET @i = @i + 1

    END

    SELECT * FROM #TempProduct

    Drop Table #TempProduct

   

  

Hope this is helpful !

Thanks

 

 

 

 

 

  Bikash Samal replied to R B
21-Dec-11 12:23 AM
Thanx for reply and for the great solution.....

It's working perfect.....

Hats off u......
  R B replied to Bikash Samal
21-Dec-11 12:33 AM
Thank you very much for reply to this post

you most welcome :)
Create New Account
help
server SQL Server Do I have to set up a sql server database on a sql server? Or can I do this on any server SQL Server Setup Discussions SQL Server (1) CREATE DATABASE (1) Databases (1) Database (1) Create
Attach SQL Server 2008 database to SQL Server 2000 Server SQL Server Hi, I have created a database in SQL Server 2008, with compatibility level of SQL Server 2000. Now when I detach the DB
Linked server SQL Server HI its possible create a local sql server as a linked server In sql server 2000 and Sql server 2005 I thought sql server 2005 its not possible thanks
MSDE on Windows 2003 R2 box, new DL385G6 - Install Fails during SQL Services SQL Server I have been finding that I am having trouble with the Crystal Reports Server XI installation failing when it is dealing with SQL. So, as a thought and in case there was something wrong with my SQL portion of the isntall. I thought ok, I will try installing the actual MSDE application direct from Microsoft. So, I downloaded the MSDE for SQL 2000 (which is msde2000a.exe), set my switches and off to the races. It quit seconds left to the installation and bombed with the same errors as the Crystal Reports Server install. The error is the same whether I try to install MSDE by itself or
Backup on Sql server A and restore to Sql server B SQL Server Hi! I have two SQL Server 2008 Std (Sql Server A and Sql Server B) on Windows Server 2008 R2. SQL