Convert DD MMM YYYY to yyyymmdd in string

  • 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![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • 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![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • 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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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

  • 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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 5 posts - 16 through 19 (of 19 total)

You must be logged in to reply to this topic. Login to reply