October 22, 2015 at 2:41 am
Hi All,
In my column (FilePath) contains the values as below,
FILEPATH
---------------
\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C12\29P
\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C122\207
\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C124\212
\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C127\252
If my SQL as below,
select * from TRFile where FilePath LIKE '%\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C12%' order by FileName
It will return all the values with contains 'C12' and this is wrong.
I need the result only return ONE record which is
> \\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C12
Any idea?
October 22, 2015 at 2:55 am
Initial way would be to reverse the string, find the first \ strip out the remaining characters and compare.
Something like this
create table #TRFile (filepath varchar(100))
insert into #TRFile values
('\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C12\29P'),
('\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C122\207'),
('\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C124\212'),
('\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C127\252')
select filepath from #TRFile
where LEFT(FILEPATH, (LEN(FILEPATH) - CHARINDEX('\',REVERSE(filepath),0))) = '\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C12'
drop table #TRFile
October 22, 2015 at 3:01 am
Surely you just need to add another backslash to your search pattern? I've removed the first wildcard from the front - you don't need it if all values start with "\\".
select * from TRFile
where FilePath LIKE '\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C12\%'
order by FileName
John
October 22, 2015 at 3:16 am
anthony.green (10/22/2015)
Initial way would be to reverse the string, find the first \ strip out the remaining characters and compare.Something like this
create table #TRFile (filepath varchar(100))
insert into #TRFile values
('\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C12\29P'),
('\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C122\207'),
('\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C124\212'),
('\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C127\252')
select filepath from #TRFile
where LEFT(FILEPATH, (LEN(FILEPATH) - CHARINDEX('\',REVERSE(filepath),0))) = '\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C12'
drop table #TRFile
Thanks your feedback. If sometimes my query need to filter by this scenario as below,
\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C12
OR
\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C12\29P
October 22, 2015 at 3:23 am
Us an OR to search the exact string
Just uncomment a particular string at the top you want to search
--declare @searchstring varchar(100) = '\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C12'
--declare @searchstring varchar(100) = '\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C12\29P'
--declare @searchstring varchar(100) = '\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C122'
create table #TRFile (filepath varchar(100))
insert into #TRFile values
('\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C12\29P'),
('\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C122\207'),
('\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C124\212'),
('\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C127\252')
select filepath from #TRFile
where LEFT(FILEPATH, (LEN(FILEPATH) - CHARINDEX('\',REVERSE(filepath),0))) = @searchstring or filepath = @searchstring
drop table #TRFile
October 22, 2015 at 4:51 am
knockyo (10/22/2015)
Hi All,In my column (FilePath) contains the values as below,
FILEPATH
---------------
\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C12\29P
\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C122\207
\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C124\212
\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C127\252
If my SQL as below,
select * from TRFile where FilePath LIKE '%\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C12%' order by FileName
It will return all the values with contains 'C12' and this is wrong.
I need the result only return ONE record which is
> \\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C12
Any idea?
Use = instead of LIKE.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
October 22, 2015 at 4:58 am
anthony.green (10/22/2015)
Us an OR to search the exact stringJust uncomment a particular string at the top you want to search
--declare @searchstring varchar(100) = '\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C12'
--declare @searchstring varchar(100) = '\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C12\29P'
--declare @searchstring varchar(100) = '\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C122'
create table #TRFile (filepath varchar(100))
insert into #TRFile values
('\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C12\29P'),
('\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C122\207'),
('\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C124\212'),
('\\Dpo-doctest1\Citibank Files\Citibank\07-JULY\20150703\C127\252')
select filepath from #TRFile
where LEFT(FILEPATH, (LEN(FILEPATH) - CHARINDEX('\',REVERSE(filepath),0))) = @searchstring or filepath = @searchstring
drop table #TRFile
Thanks ur help
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply