special query

  • i have a select statement and i want trim down some characters in the result of my data

    can you please give me the correct select statement for this

    Sample:

    select cust

    from customer

    and the result

    cust

    ------

    john pratts

    leny pratts

    jen pratts

    jeffrey pratts

    (4 rows affected)

    i want to trim the "PRATTS"

    in all of my results

    sample:

    cust

    ------

    john

    leny

    jen

    jeffrey

    (4 rows affected)

    hope you cn give me some idea ...

    thanks

    doc

    doc


    doc

  • select substring(name,1,charindex(' ',name)) as Name from t1

    Try this it will give the first word in the column before the blankspace where name is the columnname and ' ' is the separator

    Arvind


    Arvind

  • select substring(name,1,charindex(' ',name)) as Name from t1

    Try this it will give the first word in the column before the blankspace where name is the columnname and ' ' is the separator

    Arvind


    Arvind

  • select substring(name,1,charindex(' ',name)) as Name from t1

    Try this it will give the first word in the column before the blankspace where name is the columnname and ' ' is the separator

    You can also use the string function Left() if you know the number of characters you want to trim from the word and it works only if the characters are the same. Better try the first one!!

    Arvind


    Arvind

  • If however you know that it is PRATTS you need to remove from every instance and does not change then this will also work, but Arvind's is most likely what you are looking for.

    SELECT REPLACE(cust, 'PRATTS', '')

    Note thou if they have a middle initial you want to keep or just looking to remove last name, you could use len with left and reverse like this.

    SELECT LEFT(cust, LEN(cust) - CHARINDEX(' ', REVERSE(cust)))

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

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