string manipulation - CHARINDEX

  • Hi,

    I have a query which retrieves Full_Name and makes it into a format of  ==> lastname+'.'+first letter of first name. (for example: Full_Name: Mary Washington  ==> washington.m)

    Now I want to change to a format of first six letters of lastname+'.'+first letter of first name. (for example: Full_Name: Mary Washington  ==> washin.m)

     Can someone help me to change the query?

    Thank you.

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

    SELECT  RTRIM(SUBSTRING(Full_Name, CHARINDEX(' ', Full_Name) + 1, CHARINDEX(' ', Full_Name, CHARINDEX(' ', Full_Name) + 1) - CHARINDEX(' ',

                          Full_Name) + 20)) + '.' + LOWER(SUBSTRING(Full_Name, 1, 1)) AS Expr1

    FROM    micros.Mgmt_Personnel_Table

  • DECLARE @Mgmt_Personnel_Table TABLE( Full_Name varchar(100))

     INSERT INTO @Mgmt_Personnel_Table VALUES( 'Mary Washington')

    SELECT LOWER( RTRIM( SUBSTRING( Full_Name, CHARINDEX( ' ', Full_Name) + 1, 6))) + '.' + LOWER( SUBSTRING( Full_Name, 1, 1)) AS Expr1

    FROM @Mgmt_Personnel_Table

    I wasn't born stupid - I had to study.

  • Thank you for your quick response.  It works great except there's one employee's name is "John Del Valle Kalble", the last name is Del Valle Kalble, first name is John.

    The result for this employee is "del va.m", which should be "delval.m"

    I tried to change it to

    REPLACE(LOWER(RTRIM(SUBSTRING(Full_Name, CHARINDEX(' ', Full_Name) + 1, 6))) + '.' + LOWER(SUBSTRING(Full_Name, 1, 1)), ' ', '') AS Expr1

    But this makes the lastname 5 letters only (delva.m). How to eliminate space in last name and keep it as 6 letters? 

     

  • Not sure where the "m" is coming from, but this will get you "delval.j" as well as "washin.m"

    DECLARE @Mgmt_Personnel_Table TABLE( Full_Name varchar(100))

    INSERT INTO @Mgmt_Personnel_Table VALUES( 'Mary Washington')

    INSERT INTO @Mgmt_Personnel_Table VALUES( 'John Del Valle Kalble')

    SELECT LOWER( SUBSTRING( REPLACE( SUBSTRING( Full_Name, CHARINDEX( CHAR(32), Full_Name) + 1, 10), CHAR(32) , ''), 1, 6)) +

                 '.' + LOWER( SUBSTRING( Full_Name, 1, 1)) AS Expr1

    FROM @Mgmt_Personnel_Table

    I wasn't born stupid - I had to study.

  • Thank you thank you thank you! 

Viewing 5 posts - 1 through 4 (of 4 total)

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