January 15, 2014 at 12:03 pm
declare @ch varchar(200)
declare @result varchar(200)
set @ch = 'US , CA- I have been there'
BEGIN
set @result = LTRIM(RTRIM(CAST(@ch as varchar(max))))
set @result = substring (@result, charindex('-',@result),len(@result))
END
select @result
Current Result
=================
- I have been there
Expected Result
=================
US , CA
kinldy help me to alter query
January 15, 2014 at 1:20 pm
Shanmuga Raj (1/15/2014)
declare @ch varchar(200)declare @result varchar(200)
set @ch = 'US , CA- I have been there'
BEGIN
set @result = LTRIM(RTRIM(CAST(@ch as varchar(max))))
set @result = substring (@result, charindex('-',@result),len(@result))
END
select @result
Current Result
=================
- I have been there
Expected Result
=================
US , CA
kinldy help me to alter query
Wow talk about overcomplicating something. Why do you cast @ch as varchar(max) and put it back into a variable that is the same datatype as the original?
You don't need a variable here to hold your results and the BEGIN END are not serving any purpose at all.
Here is all you need to do this.
declare @ch varchar(200)
set @ch = 'US , CA- I have been there'
select LEFT(@ch, charindex('-', @ch) - 1)
Keep it simple. 😀
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply