Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Convert DD MMM YYYY to yyyymmdd in string Expand / Collapse
Author
Message
Posted Sunday, September 29, 2013 11:17 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Monday, November 24, 2014 4:53 AM
Points: 3,422, Visits: 5,368
I find it interesting because I'm into self-flagellation and because it seems that all of the date formats you've included in your filenames CAST cleanly to a DATE data type, which can then be formatted to be stuffed back into your filename.

Here's an example using a pattern-based string splitter.

WITH SampleData (sourcedata) AS
(
SELECT '12 Jan 2013 Test.docx'
UNION ALL SELECT '01 February 2001 File.pptx'
UNION ALL SELECT 'Meeting 04 Feb 2012.xls'
UNION ALL SELECT '09 September 2011.txt'
UNION ALL SELECT '30 Jan 13.doc'
UNION ALL SELECT '3.0 Meeting Minutes 16 January 2013.pdf'
UNION ALL SELECT '0.0 ICT Meeting (12 Jan 2013).doc'
UNION ALL SELECT 'ICTAgenda 15January 2013 V1.pptx'
UNION ALL SELECT 'ICTAgenda 15 January 2013 V2.pptx'
),
SplitFNs AS
(
SELECT sourcedata, b.*
,rn=CASE WHEN LEFT(LTRIM(Item), 3) IN
('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')
THEN ItemNumber END
FROM SampleData a
CROSS APPLY dbo.PatternSplitCM(sourcedata, '[0-9]') b
),
DateRecs AS
(
SELECT a.sourcedata, b.ItemNumber, b.Item
FROM SplitFNs a
JOIN SplitFNs b
ON b.ItemNumber BETWEEN a.rn - 1 AND a.rn + 1 AND a.sourcedata = b.sourcedata
)
SELECT sourcedata
,sourcedata2=STUFF(sourcedata, CHARINDEX(FileDate, sourcedata), LEN(FileDate),CAST(FileDate AS DATE))
FROM SampleData a
CROSS APPLY
(
SELECT Item + ''
FROM DateRecs b
WHERE a.sourcedata = b.sourcedata
ORDER BY ItemNumber
FOR XML PATH('')
) b(FileDate);


PatternSplitCM can be found in my 4th signature link.

Note that I'm not necessarily recommending this approach. I just thought that odd ability to CAST to DATE might be easily exploitable, but alas I didn't find it so easy.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1499834
Posted Sunday, September 29, 2013 11:59 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, September 30, 2013 4:18 PM
Points: 7, Visits: 9
Delete
Post #1499839
Posted Monday, September 30, 2013 12:06 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, September 30, 2013 4:18 PM
Points: 7, Visits: 9
How or where would i add the below

Where [SourceData] like '%[0-9]%' + 'Jan' + '[0-9]%'


as lots of data gets messed up with dwains code, and want to limit it to date specific strings only

e.g

Destination File Name....sourcedata2
20130616.xlsx..............0616.xlsx
201307.xlsx.................07.xlsx
HSE.201306.xlsx...........HSE.06.xlsx
HSE.201307.xlsx...........HSE.07.xlsx
2013 ICT Calendar.xls....ICT Calendar.xls
Inv.10017355.txt..........Optus.Inv.7355.txt
Post #1499841
Posted Monday, September 30, 2013 12:08 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Monday, November 24, 2014 4:53 AM
Points: 3,422, Visits: 5,368
dc.pc100 (9/30/2013)
How or where would i add the below

Where [SourceData] like '%[0-9]%' + 'Jan' + '[0-9]%'


as lots of data gets messed up with dwains code

e.g

Destination File Name....sourcedata2
20130616.xlsx..............0616.xlsx
201307.xlsx.................07.xlsx
HSE.201306.xlsx...........HSE.06.xlsx
HSE.201307.xlsx...........HSE.07.xlsx
2013 ICT Calendar.xls....ICT Calendar.xls
Inv.10017355.txt..........Optus.Inv.7355.txt


Yup mine won't handle those.

Have you tried Jeff's code with this case?

I think using LIKE like you are trying to do is going to be a pain because you'd need 12 of them OR'd together.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1499842
Posted Monday, September 30, 2013 12:11 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, September 30, 2013 4:18 PM
Points: 7, Visits: 9
I havent had much experience with Tally tables so do no know how to utilize them.

The "OR" x12 in the where clause it fine for now. Ill probs just create another table and query the words(month names) in there
Not best practice but yea

Just having troubles inserting the where clause, as it doesn't like it anywhere near there Cross Apply
Post #1499843
Posted Monday, September 30, 2013 12:19 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Monday, November 24, 2014 4:53 AM
Points: 3,422, Visits: 5,368
This might work for a few more cases but no guarantee for all.

WITH SampleData (sourcedata) AS
(
SELECT '12 Jan 2013 Test.docx'
UNION ALL SELECT '01 February 2001 File.pptx'
UNION ALL SELECT 'Meeting 04 Feb 2012.xls'
UNION ALL SELECT '09 September 2011.txt'
UNION ALL SELECT '30 Jan 13.doc'
UNION ALL SELECT '3.0 Meeting Minutes 16 January 2013.pdf'
UNION ALL SELECT '0.0 ICT Meeting (12 Jan 2013).doc'
UNION ALL SELECT 'ICTAgenda 15January 2013 V1.pptx'
UNION ALL SELECT 'ICTAgenda 15 January 2013 V2.pptx'
UNION ALL SELECT '20130616.xlsx'
UNION ALL SELECT '201307.xlsx'
UNION ALL SELECT 'HSE.201306.xlsx'
UNION ALL SELECT 'HSE.201307.xlsx'
UNION ALL SELECT '2013 ICT Calendar.xls'
UNION ALL SELECT 'Inv.10017355.txt'
),
SplitFNs AS
(
SELECT sourcedata, b.*
,rn=CASE WHEN LEFT(LTRIM(Item), 3) IN
('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')
THEN ItemNumber END
FROM SampleData a
CROSS APPLY dbo.PatternSplitCM(sourcedata, '[0-9]') b
),
DateRecs AS
(
SELECT a.sourcedata, b.ItemNumber, b.Item
FROM SplitFNs a
JOIN SplitFNs b
ON b.ItemNumber BETWEEN a.rn - 1 AND a.rn + 1 AND a.sourcedata = b.sourcedata
)
SELECT sourcedata
,sourcedata2=CASE WHEN Flag = 0 THEN sourcedata
ELSE STUFF(sourcedata, CHARINDEX(FileDate, sourcedata), LEN(FileDate),CAST(FileDate AS DATE)) END
FROM SampleData a
CROSS APPLY
(
SELECT Item + ''
FROM DateRecs b
WHERE a.sourcedata = b.sourcedata
ORDER BY ItemNumber
FOR XML PATH('')
) b(FileDate)
CROSS APPLY
(
SELECT CASE WHEN LEN(FileDate) - LEN(REPLACE(FileDate, '-', '')) = 2 THEN 1 ELSE 0 END
) c(Flag);






My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1499845
Posted Monday, September 30, 2013 12:24 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Monday, November 24, 2014 4:53 AM
Points: 3,422, Visits: 5,368
dc.pc100 (9/30/2013)
I havent had much experience with Tally tables so do no know how to utilize them.

The "OR" x12 in the where clause it fine for now. Ill probs just create another table and query the words(month names) in there
Not best practice but yea

Just having troubles inserting the where clause, as it doesn't like it anywhere near there Cross Apply


The WHERE would go after the CROSS APPLYs.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1499847
Posted Monday, September 30, 2013 5:02 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:26 PM
Points: 35,617, Visits: 32,213
dc.pc100 (9/30/2013)
How or where would i add the below

Where [SourceData] like '%[0-9]%' + 'Jan' + '[0-9]%'


as lots of data gets messed up with dwains code, and want to limit it to date specific strings only

e.g

Destination File Name....sourcedata2
20130616.xlsx..............0616.xlsx
201307.xlsx.................07.xlsx
HSE.201306.xlsx...........HSE.06.xlsx
HSE.201307.xlsx...........HSE.07.xlsx
2013 ICT Calendar.xls....ICT Calendar.xls
Inv.10017355.txt..........Optus.Inv.7355.txt


Heh... Now you know why I said what I said in my first coded response. I kind of figured it wasn't as cleanly cut as the first bit of data.

At this point and considering the last line in your example above, why is it that you need to converted dates embedded in a file name to a certain format?


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1499927
Posted Monday, September 30, 2013 4:20 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, September 30, 2013 4:18 PM
Points: 7, Visits: 9

At this point and considering the last line in your example above, why is it that you need to converted dates embedded in a file name to a certain format?


Naming conventions and migrating files to a CRM
Post #1500212
Posted Monday, September 30, 2013 6:22 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:26 PM
Points: 35,617, Visits: 32,213
dc.pc100 (9/30/2013)

20130616.xlsx
201307.xlsx
HSE.201306.xlsx
HSE.201307.xlsx
2013 ICT Calendar.xls
Inv.10017355.txt


The big question would be... what do you want for the final file names for the file names above? I could guess on some of the more obvious ones but I hate guessing at things like this.

Speaking of "naming conventions"... based on what you've requested, there are two problems here...
1. The tail is wagging the dog. These files should be named following both a format and naming convention at the source instead of you having to write code to handle such things on the backend.
2. You still don't have an actual naming convention because even the modified date can appear anywhere in the file name or could be totally missing as in that last example above.

My recommendation would be to drop this task and use the file creation date, instead.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1500234
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse