I have the following function in SQL I used to take a varchar "query" string from a search page on my website. It splits the String parameters into temp table with a list of all words in that query. However I would like to incorporate the ability for users to search for phrases by enclosing words in quotes. So that a phase would basically be considered one word in my returned temp table. So basically the way it works now if you search "Gold TV" 4K it would return something like
- "Gold
- TV"
- 4K
And I would like it to return
- Gold TV
- 4K
I haven't been able to get the logic right, here is my current function. Or if there is a much better way to do this let me know.
CREATE FUNCTION [dbo].[querySplit](@String varchar(8000), @Delimiter char(1))
returns @temptable TABLE (items varchar(8000))
as
begin
declare @idx int
declare @slice varchar(8000)
select @idx = 1
if len(@String)<1 or @String is null return
while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String
if(len(@slice)>0)
insert into @temptable(Items) values(@slice)
set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end
With the use of a helper function and a CROSS APPLY
Example
Results
Since you are on 2014, here is my split/parse function