Select a matching rows which exist in particular string

  • Hi,

    Suppose I have string like

    DECLARE @strname varchar(50) = 'i_ncm_ai04';

    DECLARE @SAMPLE_DATA TABLE

    (

    SD_ID INT NOT NULL

    ,SD_TEXT VARCHAR(10) NOT NULL

    );

    INSERT INTO @SAMPLE_DATA(SD_ID,SD_TEXT)

    VALUES

    (1,'i_n0')

    ,(2,'i_nccc')

    ,(3,'i_ncm')

    ,(4,'i_ncm_pp' )

    ,(5,'i_ncm_ar' )

    ,(6,'i_ncm_an' )

    ,(7,'i_ncm_ai05' )

    ,(8,'i_ncm_a');

    how can I search a ID's which are exist in my string.

    result should be,

    3 i_ncm

    8 i_ncm_a

    Thanks,

    Fanindra

  • select * from @SAMPLE_DATA sd

    where @strname like '%' + sd.SD_TEXT + '%'

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Actually, this version should be faster:

    select * from @SAMPLE_DATA sd

    where @strname like sd.SD_TEXT + '%'

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

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

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