Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


substring,replace,charindex


substring,replace,charindex

Author
Message
sha_azna
sha_azna
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 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!
UMG Developer
UMG Developer
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2512 Visits: 2204
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search