Register  Login  
  February 6, 2012  
Search_Blog Minimize
Print  

Blog_Archive Minimize
Print  

Blog    
View_Blog Minimize
Jan 23

Written by: host
Saturday, January 23, 2010 10:36 AM 

One of the most useful T-SQL features is the keyword "IN".  THis keyword allows the user to filter the selected data against a list of comma separated data. For example, using the Northwind sample database:

    select * from Customers where Country IN ('Canada', 'Mexico')
   
However, this is rather limited syntax as the parameter list is static.  T-SQL does not support variable substitution with the IN keyword so the following will NOT work

    declare @CList varchar(100)
  set @Clist = 'Canada, Mexico'
  select * from Customers where Country IN (@Clist)  -- will fail!
   
Is there another way?  Yes.  The IN keyword will accept a sub-select. So we could use a temporary table and a sub-select to implement something like this:

    declare @Ctable table ( Cname varchar(100) )
    Insert @Ctable Values ( 'Canada')
    Insert @Ctable Values ( 'Mexico')
    select * from Customers where Country IN
        ( Select Cname from @Ctable )

This works, but still is a bit cumbersome to implement. However, this example does not have the same flexiblity of a comma-delimited list.

So how can this functionality be implemented?  Use a User Defined Function (UDF) to return a table of paramters!

This is not a new concept. I recently came across this article (http://www.4guysfromrolla.com/webtech/031004-1.shtml) posted in 2004.  In the article, the author (Corey Aldebol) defines a function capable of taking a comma-delimited parameter list and returning a table of paramters.  Here is the Function

    CREATE FUNCTION dbo.Split
    (
      @List nvarchar(2000),
      @SplitOn nvarchar(5)
    ) 
    RETURNS @RtnValue table
    (
      Id int identity(1,1),
      Value nvarchar(100)
    )
    AS 
    BEGIN
      While (Charindex(@SplitOn,@List)>0)
        Begin
        Insert Into @RtnValue (value)
        Select
          Value = ltrim(rtrim(Substring(@List,1,Charindex(@SplitOn,@List)-1)))
          Set @List = Substring(@List,Charindex(@SplitOn,@List)+len(@SplitOn),len(@List))
        End
        Insert Into @RtnValue (Value)
        Select Value = ltrim(rtrim(@List))

        Return
    END

Using this function, we can then change the failed example into the follow:

    declare @CList varchar(100)
    set @Clist = 'Canada, Mexico'
    select * from Customers where Country in
        (Select Value from dbo.Split(@Clist,','))

or even simpler

    select * from Customers where Country in
      (Select Value from dbo.Split('Canada, Mexico',','))

So how can this be used by SGSV (SQLGridSelectedView)?  IF you implement the User Defined Function, then the function can be used by SGSV using a Text Search parameter filter. 


  
which results in


   

You can test the function at www.tressleworks.ca/samples

Hope this helps
Paul.
   

Update: MarkXA has provided an alternative solution for the Split function. I called it Split2.  This version requires SQL2005 or better.

CREATE FUNCTION dbo.Split2 (
  @List nvarchar(max), @SplitOn nvarchar
)
RETURNS TABLE
AS
RETURN (
   WITH Split AS (
      SELECT CONVERT(nvarchar(max), NULL) AS Value, @List AS List
      UNION ALL
      SELECT

                CASE WHEN CHARINDEX(',', List) = 0 THEN List 
             ELSE LEFT(List, CHARINDEX(',', List) - 1) END,
        CASE WHEN CHARINDEX(',', List) = 0 THEN ''
             ELSE RIGHT(List, LEN(List) - CHARINDEX(',', List)) END
      FROM Split
      WHERE List <> ''
   )
   SELECT LTRIM(RTRIM(Value)) as [Value]
   FROM Split
   WHERE Value IS NOT NULL
)

 

Thanks Mark.

Tags:

5 comment(s) so far...

Re: What's IN your database - Using a UDF with SGSV

All of the projects look great! You make it so simple to this. Thanks

By abercrombie and fitch deutschland on   Sunday, January 29, 2012 1:40 AM

Re: What's IN your database - Using a UDF with SGSV

What a fun pattern! It's great to hear from you and see what you've sent up to. All of the projects look great! You make it so simple to this. Thanks

By abercrombie on   Sunday, January 29, 2012 1:41 AM

cheap oakley sunglasses

cheap oakley sunglasses
cheap oakleys
oakleys
RGNPJLWMDFFYY I like it very much!

By cheap oakley sunglasses on   Tuesday, January 31, 2012 2:50 AM

Re: Chinese Footware - and the removal of...

GameCoach Factory Outlet Onlineare usuallyCoach Factoryin By Coach Outlet Online on   Thursday, February 02, 2012 1:21 AM

beats by dre uk


The author of the paper joint, Cornell university application and engineering physics institute scientists morty-Fred mann according to design a special method, "this method through the change of laser beam frequency and wavelength, Cheap Beats By Dre so the spread in different rate, thus produce a (time) gap." Then, time holes on the other side of the second pulse laser beam, this bunch of pulse laser the function of the one is in the opposite direction to change the attributes of the laser beam, so that the laser beam back to the original attributes. In the experiment, occurs in time of vulnerability of events, can escape the probe detection.
University of rochester institute of optics scientists ShiZhiMin (Zhimin Shi) said, "this is the first real time gap between the produce experiment, is also the first realize objects in space and in time and invisible experiment." ShiZhiMin was not involved in the study.
Although the study is still in the early stages, but this time to the operation will has a broad prospect of application. ShiZhiMin said, "I believe, may the people do not have to this kind of technology exactly how much use thinking too much. auriculares beats Maybe people thinks of above all is to let something invisible don't be detected, or intentionally let some event in 'the cloak of'."
However, time lens technology and may be quietly to the continuous flow of data to insert specific information, this behaviour can be perfect, and it is hard to find. Fred mann explains, "I think you can bring some data is coming into the hidden in optical fiber in data flow. Using the time lens, beats by dr dre australia you can follow one's inclinations to manipulate data, they may be reduction."
Experts think, the technology will can be applied to the research and development due to chip, speed up the Internet and computer internal data and data flow processing speed. ShiZhiMin says, beats by dre headphones any application will depend on whether to expand the time holes. In addition, in the present experiment, simply by a single laser experiments, and the actual application to consider three dimensional light from different point of view to illuminate. ShiZhiMin think, Cornell university experiment has taken the first step in the key.

By beats by dre uk on   Friday, February 03, 2012 1:27 AM

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment   Cancel 

Blog_List Maximize
Print  

  Home | Blog | Forum | Subscriptions | Free Modules | Videos   Page generated in 0.0626024 seconds.
  Copyright 2010 by TressleWorks   Terms Of Use | Privacy Statement