Archive

Archive for December, 2012

T-SQL : An Interesting Question – 3

The requirement is as follows,

Filter out records that has three or more words in it

(or)

Filter out records that has two or more spaces in it

There are several ways to perform this and i would like to show some of them,

Declare @table Table(words varchar(50))
insert into @table
select 'Get the query' union
select 'Two words' union
select 'Three' union
select 'Explain' union
select 'See the output' union
select 'Execute the Query' union
select 'Find the Names' union
select 'single' union
select 'see the output below'

select words from @table
where words like '% % %'

select words from @table
where words  LIKE '%| %| %' ESCAPE '|'

select words from @table
where words LIKE '_% _% _%'

select words from @table
where DATALENGTH(words) - DATALENGTH(REPLACE(words,' ',''))>=2

select words from @table
where CHARINDEX(' ',words, CHARINDEX(' ',words, 1) + 1) >  0

select words from @table
where PATINDEX('% % %',words)> 0

Happy Querying 😉

Advertisements
%d bloggers like this: