Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to match similar string in field?

Author  Topic 

adr3nal1n
Starting Member

2 Posts

Posted - 2010-09-06 : 10:04:53
Hi,

I am new to SQL and have read multiple online tutorials but am struggling to form a query to match similar strings in a field and then display the results.

I have a database containing filenames, these files either end in .txt or .xls I would like to be able to query the database to display only filenames that have both .txt and .xls extensions.

I have tried the following but it produces no output and I know I have some files that end in both extensions (e.g. Fred.txt and Fred.xls).

select Filename from files where Filename like '%.txt' and Filename like '%.xls';

Apologies if this is a trivial query, I am still very much a novice and would appreciate some direction as to where I am going wrong.

Thanks in advance for any help.

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-09-06 : 10:13:00


select parsename(Filename,2) from files
where Filename like '%.txt' or Filename like '%.xls'
group by parsename(Filename,2)
having count(distinct parsename(Filename,1) )=2


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

adr3nal1n
Starting Member

2 Posts

Posted - 2010-09-06 : 10:25:36
Thanks very much for your help and the prompt reply, am now reading through the query so I can workout the logic of it as this will help me with developing further queries.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-09-07 : 04:36:09
quote:
Originally posted by adr3nal1n

Thanks very much for your help and the prompt reply, am now reading through the query so I can workout the logic of it as this will help me with developing further queries.


You are welcome

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -