Patindex

  • hi,

    i m new to sql.

    can any one brief the function of the below query and how it works

    select * from tab

    where patindex('%@friends1.com',email)>0

  • The query you provide should return all rows in tab where '@friends1.com' are the final characters in the email column. If you have any non-blank characters after that string those rows will be eliminated. For example try this:

    Declare @tab table (id int identity(1,1), char_email char(50), varchar_email varchar(50), nvarchar_email nvarchar(50))

    Insert Into @tab

    (

    char_email,

    varchar_email,

    nvarchar_email

    )

    Select

    'jack@friends1.com ',

    'jack@friends1.com ',

    'jack@friends1.com '

    Union All

    Select

    'jack@friends1.com',

    'jack@friends1.com',

    'jack@friends1.com '

    Union All

    Select

    'jack@friends1.com1',

    'jack@friends1.com a',

    'jack@friends1.com c'

    Select

    char_email,

    patindex('%@friends1.com',char_email),

    varchar_email,

    patindex('%@friends1.com',varchar_email),

    nvarchar_email,

    patindex('%@friends1.com',nvarchar_email)

    From

    @tab

  • Jack beat me to it, but you can also learn more about the patindex function in BOL (Books On-Line).

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

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