SQLTeam.com | Weblogs | Forums

Trimming a word from a sentence in sql


#1

I have a data in a columnA of a table as given below, and I want to extract only wheel drive in separate solumn

and i want only wheel drive section from a sentence.like given below

Can anyone please suggest how to do this.
Thanks


#2

Use String Splitter with ";" delimiter and constrain to only values that are LIKE '% wheel drive'

SELECT	SplitterValue
FROM	YourTable AS T
	CROSS APPLY dbo.YourSplitterFunction(T.columnA, ';') AS X
WHERE	SplitterValue LIKE '% Wheel Drive'

#3
declare @t as table
(
    val    varchar(100)
)

insert into @t
Select 'Four Wheel Drive'
union 
Select 'Four Wheel Drive;Sedan'
union 
Select 'Four Wheel Drive;HatchBack'
union 
Select 'Four Wheel Drive;Transcode A727'
union 
Select 'XLS Sport;Rear Wheel Drive'
union 
Select 'Classic SLT; Rear Wheel Drive;Extended Cab Pickup;Front Torsion Bar'
union 
Select 'SLT; Rear Wheel Drive;Standrard Cab Pickup'

select * , substring(val,CHARINDEX(' Wheel Drive', val)-4,16)
from @t

#4

Thanks Viggneshwar.