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

Date Parse Help Expand / Collapse
Author
Message
Posted Friday, August 2, 2013 10:12 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Saturday, July 26, 2014 5:26 PM
Points: 205, Visits: 744
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.
Post #1480499
Posted Friday, August 2, 2013 10:17 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: Today @ 6:22 PM
Points: 3,359, Visits: 7,270
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.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1480502
Posted Friday, August 2, 2013 10:27 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Saturday, July 26, 2014 5:26 PM
Points: 205, Visits: 744
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.
Post #1480512
Posted Friday, August 2, 2013 10:30 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:36 PM
Points: 13,111, Visits: 11,946
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1480514
Posted Friday, August 2, 2013 10:33 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Saturday, July 26, 2014 5:26 PM
Points: 205, Visits: 744
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
Post #1480517
Posted Friday, August 2, 2013 10:40 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:36 PM
Points: 13,111, Visits: 11,946
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1480518
Posted Saturday, August 3, 2013 5:59 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 6:52 PM
Points: 36,773, Visits: 31,229
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."

(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 #1480745
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse