Date Parse Help

  • Hi Guys,

    Here is my Date Column, sample data below

    20130801173009

    20130801173009

    20130801173009

    20130801173009

    20130801173009

    20130801173009

    Is someone can help me to parse in Year,Month, Day and rest are time?

    Thanks in advance.

  • You should avoid to use strings or integers to store dates. This might help you with your problem.

    SELECT CAST( STUFF(STUFF(STUFF(strDate, 13, 0, ':'), 11, 0, ':'), 9, 0, ' ') AS datetime)

    FROM (VALUES('20130801173009'))x(strDate)

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Luiz thank you for your help, however that sql is just for one e.g. How I can use your sql on one column?

    Year could be 2013 or 2012 or any year and same as date and month.

    Thank You.

  • rocky_498 (8/2/2013)


    Luiz thank you for your help, however that sql is just for one e.g. How I can use your sql on one column?

    Year could be 2013 or 2012 or any year and same as date and month.

    Thank You.

    The code Luis posted will work on one column. You didn't provide a table to work with so he just made a table with a single row with your sample data.

    Try it out on your table. And if at all possible you should consider changing the datatype in your original table to datetime instead of the varchar/int that it is currently.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Yes I got it. Sorry or confusion, Thanks Guys.

    Yes you guys are right good idea to change D.Type.

    Sometime not a good idea to argue with Manager about wrong thing. I already told but they don't wanna listen 🙂

  • rocky_498 (8/2/2013)


    Yes I got it. Sorry or confusion, Thanks Guys.

    Yes you guys are right good idea to change D.Type.

    Sometime not a good idea to argue with Manager about wrong thing. I already told but they don't wanna listen 🙂

    That is not a fun thing when they won't listen to a rational discussion. The worst part is when they later get mad because they end up with some garbage values in there that you can't parse to a datetime. Then you have to remind them that is one of the reason you suggested changing it in the past. 😛

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • rocky_498 (8/2/2013)


    Hi Guys,

    Here is my Date Column, sample data below

    20130801173009

    20130801173009

    20130801173009

    20130801173009

    20130801173009

    20130801173009

    Is someone can help me to parse in Year,Month, Day and rest are time?

    Thanks in advance.

    Before we run off with the good solution given so far, let's make sure that cuff's match collar for datatypes. What is the datatype of your datecolumn? If it's not CHAR or VARCHAR, it might be wise for us to try something just a little different,

    --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 7 posts - 1 through 6 (of 6 total)

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