Left and Right character comparison

  • Can anyone please help I'm a relative novice when it comes to MS SQL.

    I need to compare the first 3 characters and last 2 characters and compare that with identical pattern in another field.

    Example

    Field 1 = XYZ 123

    Field 2 = XYZAP 99123

    With a resultant match of XYZ123

    Thanks

  • why don't you use right(fieldname,3) and left(fieldname,3)

    ----------------------------------------------------------------------------------------------------------------------------------------------------
    Roshan Joe

    Jeff Moden -Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • Not sure if this is what you are looking for.

    CREATE TABLE #t (varr varchar(50))

    INSERT INTO #t values ('XYZAP99123')

    INSERT INTO #t values ('XYZAP98123')

    INSERT INTO #t values ('XYZAP97123')

    INSERT INTO #t values ('XYZAP9997123')

    INSERT INTO #t values ('DEFAP97123')

    Declare @field1 as varchar(50)

    SET @field1 = 'XYZ123'

    Select * from #t

    where varr like LEFT(@field1, 3) + '%' + Right(@field1, 2)

    ---------------------------------------------------------------------------------

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

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