SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 need this in single SELECT
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 01/29/2013 :  08:28:58  Show Profile  Reply with Quote
Solution please....
DECLARE @path varchaR(max) = '01-22-2013-12-58-18-#1.docx, 01-22-2013-12-58-18-#2.txt'


OUTPUT should be:
1.docx, 2.txt

--
Chandu

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/29/2013 :  08:39:18  Show Profile  Reply with Quote
will date part be consistent? ie all same datetime value?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 01/29/2013 :  08:42:04  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

will date part be consistent? ie all same datetime value?


consistent date part is there and also there may any number of file names ('01-22-2013-12-58-18-#1.docx, 11-29-2013-12-58-18-#2.txt', '01-22-2013-12-60-18-#fajfbasj1.docx, 04-21-2012-12-58-18-#righoiri.txt')

--
Chandu
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3724 Posts

Posted - 01/29/2013 :  08:50:04  Show Profile  Reply with Quote
Until the day we have regular expressions in SQL Server, I can't see a way to do this using T-SQL string functions. Here is one way using a string splitter function from this thread: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=153458
DECLARE @path varchaR(max) = '01-22-2013-12-58-18-#1.docx, 01-22-2013-12-58-18-#2.txt'

SELECT
	','+Item
FROM
	dbo.DelimitedSplit8K(REPLACE(@path,',','#'),'#') dsk
WHERE
	ItemNumber%2 = 0
FOR XML PATH('')


Comment -- The function in that thread is only for VARCHAR(8000), so if you need varchar(max), another function that can handle varchar(max) would be required.

Edited by - James K on 01/29/2013 08:51:23
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/29/2013 :  08:50:42  Show Profile  Reply with Quote
so you want always part after # right?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 01/29/2013 :  08:51:34  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

so you want always part after # right?


Yes visakh

--
Chandu
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 01/29/2013 :  08:54:32  Show Profile  Reply with Quote
quote:
Originally posted by James K



Hi James,
Thanks for your response
Am trying to get this in single SELECT statement...
If there is no solution at all, then i need to apply this method...

--
Chandu
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3724 Posts

Posted - 01/29/2013 :  09:01:07  Show Profile  Reply with Quote
As Visakh was leading to, if all the date parts are the same, no matter how many parts there are, you can use a single replace statement can't you?
DECLARE @path varchaR(max) = '01-22-2013-12-58-18-#1.docx, 01-22-2013-12-58-18-#2.txt'
SELECT REPLACE(@path,'01-22-2013-12-58-18-#','')
Even if that date part is not known in advance, as long as they are all the same, or even a handful of known values, this can be extended to handle that.

Edited by - James K on 01/29/2013 09:02:38
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 01/29/2013 :  09:06:33  Show Profile  Reply with Quote
James the format is MM-DD-YYYY-HH-MI-SS-#fileName.extension
Value of MM-DD-YYYY-HH-MI-SS will differ for each filename

--
Chandu
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3724 Posts

Posted - 01/29/2013 :  09:21:46  Show Profile  Reply with Quote
I thought so, but was trying to see if you will fall for that :)

I can think of some rather convoluted ways to accomplish this (which I won't post because I don't have them written, and I would be embarrassed to post them even if I had). Joining on the splitter function (or doing a cross apply to the result of the splitter function, or even making it a correlated column) seems most reliable and simple to me. That would still be one select statement, wouldn't it?

Another possibility is to use a CLR UDF. That might even be faster and more efficient, but then again, it is a function just like the splitter function.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/29/2013 :  09:42:32  Show Profile  Reply with Quote
this is my stab at it.
Single select as you asked for!


DECLARE @path varchaR(max) = '01-22-2013-12-58-18-#1.docx, 01-22-2013-12-58-18-#2.txt',
@path1 varchar(max)='01-22-2013-12-58-18-#1.docx, 11-29-2013-12-58-18-#2.txt,01-22-2013-12-60-18-#fajfbasj1.docx, 04-21-2012-12-58-18-#righoiri.txt'

SELECT REPLACE(CAST(CAST( '<Node><Row><Date>' + REPLACE(REPLACE(@path,'#','</Date><File>'),',','</File></Row><Row><Date>') + '</File></Row></Node>' AS xml).query('data(/Node/Row/File)') AS varchar(100)),' ',',') AS [path],
REPLACE(CAST(CAST( '<Node><Row><Date>' + REPLACE(REPLACE(@path1,'#','</Date><File>'),',','</File></Row><Row><Date>') + '</File></Row></Node>' AS xml).query('data(/Node/Row/File)') AS varchar(100)),' ',',') AS path1


Output
--------------------------------------------
path	        path1
----------------------------------------------
1.docx,2.txt	1.docx,2.txt,fajfbasj1.docx,righoiri.txt



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 01/29/2013 :  23:48:20  Show Profile  Reply with Quote
Thank you very much visakh......

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/29/2013 :  23:50:25  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000