SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Convert DD MMM YYYY to yyyymmdd in string


Convert DD MMM YYYY to yyyymmdd in string

Author
Message
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18479 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
dc.pc100
dc.pc100
SSC Journeyman
SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)

Group: General Forum Members
Points: 93 Visits: 9
Delete
dc.pc100
dc.pc100
SSC Journeyman
SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)

Group: General Forum Members
Points: 93 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
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18479 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
dc.pc100
dc.pc100
SSC Journeyman
SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)

Group: General Forum Members
Points: 93 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
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18479 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18479 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)

Group: General Forum Members
Points: 222952 Visits: 42003
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
dc.pc100
dc.pc100
SSC Journeyman
SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)

Group: General Forum Members
Points: 93 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
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)

Group: General Forum Members
Points: 222952 Visits: 42003
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search