LIKE unicode character in SQL Select

  • I have a table: Table_1 as below:

    IDDescription

    1College 1 kaká

    2University 1

    3College 2

    4University 2

    I use sql select: Select * from Table_1 where Description like N'%College%'

    => Result:

    IDDescription

    1College 1 kaká

    3College 2

    But 'College' always change, so I want use a variable on it, sample:

    Select * from Table_1 where Description like N'%' + @test-2 + '%'

    but It work wrong

    Please help to resolve them,

    Thank you very much

  • How did you declare the variable?

    Using this, you might get a hint:

    PRINT N'%' + @test-2 + '%'

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Naturally I will declare a variable, sample:

    Declare @test-2 varchar(50)

    select @test-2 = 'College'

    (1) Select * from Table_1 where Description like N'%' + @test-2 + '%'

    But test result is different with

    (2) Select * from Table_1 where Description like N'%College%'

    I want result of (1) is the same (2)

  • vantuan02t1 (11/19/2015)


    I have a table: Table_1 as below:

    IDDescription

    1College 1 kaká

    2University 1

    3College 2

    4University 2

    I use sql select: Select * from Table_1 where Description like N'%College%'

    => Result:

    IDDescription

    1College 1 kaká

    3College 2

    But 'College' always change, so I want use a variable on it, sample:

    Select * from Table_1 where Description like N'%' + @test-2 + '%'

    but It work wrong

    Please help to resolve them,

    Thank you very much

    Would this work?

    Select * from Table_1 where CHARINDEX(@test, Description) > 0



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • vantuan02t1 (11/19/2015)


    Naturally I will declare a variable, sample:

    Declare @test-2 varchar(50)

    select @test-2 = 'College'

    (1) Select * from Table_1 where Description like N'%' + @test-2 + '%'

    But test result is different with

    (2) Select * from Table_1 where Description like N'%College%'

    I want result of (1) is the same (2)

    You're certainly doing something different as both queries return the same result.

    CREATE TABLE Table_1(

    ID int,

    Description nvarchar(50)

    );

    INSERT INTO Table_1

    VALUES(1,'College 1 kaká')

    ,(2,'University 1')

    ,(3,'College 2')

    ,(4,'University 2')

    Declare @test-2 varchar(50)

    select @test-2 = 'College'

    --(1)

    Select * from Table_1 where Description like N'%' + @test-2 + '%'

    --But test result is different with

    --(2)

    Select * from Table_1 where Description like N'%College%'

    GO

    DROP TABLE Table_1;

    What are you actually doing?

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Actually, have you tried this?

    Declare @test-2 nvarchar(50)

    select @test-2 = N'College'

    I'm assuming that you have a unicode character in the value which won't translate correctly into ASCII strings, instead of the simple word College.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • I want to use a variable (ex: @test-2) to replace value 'College' in LIKE

    Sample:

    Select * from Table_1 where Description like N'%@test%'

  • vantuan02t1 (11/19/2015)


    I want to use a variable (ex: @test-2) to replace value 'College' in LIKE

    Sample:

    Select * from Table_1 where Description like N'%@test%'

    We already know this. I already post a test which shows that the code is not a problem for that sample data. We need to have more accurate details on what's happening.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Thank you all, I think I had resolved it

Viewing 10 posts - 1 through 9 (of 9 total)

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