substring,replace,charindex

  • Dear gurus,

    Can someone help me to construct SQL using the above functions? I have this sample set of data:

    WTP CATION EXCHANGER

    DRAUGHT- WORKING MODE

    MCWP-TESTING-MODE PART1

    I need a result as below:

    WTP

    DRAUGHT

    MCWP

    I have tried to make used of the functions above but still failed.

    Thanks in advanced!

  • For starters it helps if you provide DDL. For better, quicker answers on T-SQL questions, click on the following: http://www.sqlservercentral.com/articles/Best+Practices/61537/

    But I think this should work for you: (There are multiple ways to go about this.)

    DECLARE @Var VARCHAR(100);

    SET @Var='WTP CATION EXCHANGER';

    --SET @Var='DRAUGHT- WORKING MODE';

    --SET @Var='MCWP-TESTING-MODE PART1';

    SELECT SUBSTRING(@Var, 1, CHARINDEX(' ', REPLACE(@var, '-', ' ')) - 1);

    I probably would have used LEFT instead of SUBSTRING, but since you specified what had to be used I went with that.

    BTW this really looks like homework, since you know exactly what functions you needed to use. If it is homework you should really try to do it yourself, or at least post what you have tried and where you are running into problems.

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

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