Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

substring,replace,charindex Expand / Collapse
Author
Message
Posted Wednesday, March 23, 2011 12:43 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, June 19, 2012 8:03 AM
Points: 1, Visits: 14
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!
Post #1082486
Posted Wednesday, March 23, 2011 1:38 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, June 16, 2014 9:38 AM
Points: 2,163, Visits: 2,189
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.
Post #1082507
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse