TSQL Like caluse does not work

  • I need to find the model from the given strings :
    STRINGS GIVEN:
    Iphone 6 Screen Rota
    Apple iPhone 6S 64gb GOLD - T-Mobile Smartphone w/Warranty 
    iPhone 6 Plus MGAF2LL/A Gold 4G LTE 128GB Unlocked Cell Phone

    1) Iphone 6 Screen Rota----model: iphone 6
    2)Apple iPhone 6S 64gb GOLD - T-Mobile Smartphone w/Warranty ----------model : iphone 6s
    3) iPhone 6 Plus MGAF2LL/A Gold 4G LTE 128GB Unlocked Cell Phone -----------model : iphone 6 plus

    I was able to get using "like :'%iphone 6s%' and '%iphone 6 plus%'

    but if i use "Like for ''%IPHONE 6%'' it gets all the models of iphone 6.

  • So what is your question?  This is one of those posts where having the DDL (CREATE TABLE statement) for the table(s), sample data (INSERT INTO statements) for the table(s), expected results based on the sample data would greatly assist in providing a decent answer.

  • komal145 - Tuesday, April 11, 2017 3:08 PM

    I need to find the model from the given strings :
    STRINGS GIVEN:
    Iphone 6 Screen Rota
    Apple iPhone 6S 64gb GOLD - T-Mobile Smartphone w/Warranty 
    iPhone 6 Plus MGAF2LL/A Gold 4G LTE 128GB Unlocked Cell Phone

    1) Iphone 6 Screen Rota----model: iphone 6
    2)Apple iPhone 6S 64gb GOLD - T-Mobile Smartphone w/Warranty ----------model : iphone 6s
    3) iPhone 6 Plus MGAF2LL/A Gold 4G LTE 128GB Unlocked Cell Phone -----------model : iphone 6 plus

    I was able to get using "like :'%iphone 6s%' and '%iphone 6 plus%'

    but if i use "Like for ''%IPHONE 6%'' it gets all the models of iphone 6.

    If I understand correctly, you want something like this:
    LIKE '%IPHONE 6 %' AND NOT LIKE '%IPHONE 6 PLUS%'

    if I am correct, the DDL would be something like:
    CREATE TABLE [dbo].[models] (model varchar(255))
    INSERT INTO [dbo].[models] VAULES ('Iphone 6 Screen Rota'),
    ('Apple iPhone 6S 64gb GOLD - T-Mobile Smartphone w/Warranty'),
    ('iPhone 6 Plus MGAF2LL/A Gold 4G LTE 128GB Unlocked Cell Phone')

    and the results you want are from something like this:
    SELECT model, CASE
    WHEN [model] LIKE '%IPHONE 6%' AND [model] NOT LIKE '%IPHONE 6 PLUS%' THEN 'iPhone 6'
    WHEN [model] LIKE '%IPHONE 6S%' THEN 'iPhone 6S'
    WHEN [model] LIKE '%IPHONE 6 PLUS%' THEN 'iPhone 6 Plus'
    ELSE 'Not an iPhone'
    END as [Iphone Type]
    FROM [dbo].[models]

    You could re-write that so the first case statement was the last one and you could remove the NOT LIKE portion of that as you would have already tested to see if it was an iPhone 6 Plus.
    Does the above work/make sense?

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply