Example Data from table Vehicle
ID BODY TYPE Litre
1 AAA 1.5
2 BBB; CCC 1.9
3 DDD 1.9
4 EEE; FFF; GGG 1.8
5 GGG 1.8
I need a Select Distinct statement that will bring the following result into a query so we are selecting all unique values but also splitting by ; as well.
BODY TYPE
AAA
BBB
CCC
DDD
EEE
FFF
GGG
I have looked at similar questions that contain various functions but I would like the result viewable as a query. I have tried adapting the suggestion below to my scenario
You should really store your data in a normalised form. That said, try this
;with c as (
select bodytype, 0 as start, CHARINDEX(';', bodytype) as sep
from Vehicle
where litre=1.9
union all
select bodytype, sep, CHARINDEX(';', bodytype, sep+1) from c
where sep>0
)
select distinct LTRIM(RTRIM(SUBSTRING(bodytype,start+1,chars))) as [BodyType]
from
(
select *, Case sep when 0 then LEN(bodytype) else sep-start-1 end as chars
from c
) v
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With