August 2, 2013 at 10:12 am
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.
August 2, 2013 at 10:17 am
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)
August 2, 2013 at 10:27 am
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.
August 2, 2013 at 10:30 am
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/
August 2, 2013 at 10:33 am
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 🙂
August 2, 2013 at 10:40 am
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/
August 3, 2013 at 5:59 pm
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
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply