Home > T-SQL > T-SQL : An Interesting Question – 3

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
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: