SQL Server using wildcard within IN

前端 未结 13 662
终归单人心
终归单人心 2020-12-02 20:07

Since I believe this should be a basic question I know this question has probably been asked, but I am unable to find it. I\'m probably about to earn my Peer Pressure badge,

相关标签:
13条回答
  • 2020-12-02 20:38

    This might me the most simple solution use like any

    select *
    from jobdetails
    where job_no like any ('0711%', '0712%')
    

    In Teradata this works fine.

    0 讨论(0)
  • 2020-12-02 20:39

    Try this

    select * 
    from jobdetails 
    where job_no between '0711' and '0713'
    

    the only problem is that job '0713' is going to be returned as well so can use '07299999999999' or just add and job_no <> '0713'

    Dan zamir

    0 讨论(0)
  • 2020-12-02 20:49

    As Jeremy Smith posted it, i'll recap, since I couldn't answer to that particular question of his.

    select *
    from jobdetails
    where job_no like '071[1-2]%'
    

    If you just need 0711% and 0712% you can also place a ranges within the brackets. For the NOT keyword you could also use [^1-2]%

    0 讨论(0)
  • 2020-12-02 20:50

    How about something like this?

    declare @search table
    (
        searchString varchar(10)
    )
    
    -- add whatever criteria you want...
    insert into @search select '0711%' union select '0712%'
    
    select j.*
    from jobdetails j
        join @search s on j.job_no like s.searchString
    
    0 讨论(0)
  • 2020-12-02 20:50

    You have the answer right there in your question. You cannot directly pass wildcard when using IN. However, you can use a sub-query.

    Try this:

    select *
    from jobdetails
    where job_no in (
    select job_no
    from jobdetails
    where job_no like '0711%' or job_no like '0712%')
    )
    

    I know that this looks crazy, as you can just stick to using OR in your WHERE clause. why the subquery? How ever, the subquery approach will be useful when you have to match details from a different source.

    Raj

    0 讨论(0)
  • 2020-12-02 20:53
    1. I firstly added one off static table with ALL possibilities of my wildcard results (this company has a 4 character nvarchar code as their localities and they wildcard their locals) i.e. they may have 456? which would give them 456[1] to 456[Z] i.e 0-9 & a-z

    2. I had to write a script to pull the current user (declare them) and pull the masks for the declared user.

    3. Create some temporary tables just basic ones to rank the row numbers for this current user

    4. loop through each result (YOUR Or this Or that etc...)

    5. Insert into the test Table.

    Here is the script I used:

    Drop Table #UserMasks 
    Drop Table #TESTUserMasks 
    
    Create Table #TESTUserMasks (
        [User] [Int] NOT NULL,
        [Mask] [Nvarchar](10) NOT NULL)
    
    Create Table #UserMasks (
        [RN] [Int] NOT NULL,
        [Mask] [Nvarchar](10) NOT NULL)
    
    DECLARE @User INT
    SET @User = 74054
    
    Insert Into #UserMasks 
    select ROW_NUMBER() OVER ( PARTITION BY ProntoUserID ORDER BY Id DESC) AS RN,
           REPLACE(mask,'?','') Mask
    from dbo.Access_Masks 
    where prontouserid = @User
    
    DECLARE @TopFlag INT
    SET @TopFlag = 1
    
    WHILE (@TopFlag <=(select COUNT(*) from #UserMasks))
    BEGIN
        Insert Into #TestUserMasks 
        select (@User),Code from dbo.MaskArrayLookupTable 
        where code like (select Mask + '%' from #UserMasks Where RN = @TopFlag)
    
        SET @TopFlag = @TopFlag + 1
    END
    GO
    
    select * from #TESTUserMasks
    
    0 讨论(0)
提交回复
热议问题