June 8, 2017 at 3:35 pm
I have a column that is varchar(8) which holds a date as 20170609. I need to convert it to datetime in the format dd/mm/yyyy 09/06/2017
June 8, 2017 at 4:12 pm
Try:
SELECT Convert(varchar(10),Cast('20170609' AS date), 103) Char8AsUKdate
June 8, 2017 at 4:22 pm
June 8, 2017 at 4:47 pm
Thanks that is spot on.
How do I mark as answered?
June 8, 2017 at 5:40 pm
Like it.
June 8, 2017 at 5:53 pm
The like only appears on the last post. How do I like a previous post?
June 9, 2017 at 8:29 am
robert.morgan 63781 - Thursday, June 8, 2017 5:53 PMThe like only appears on the last post. How do I like a previous post?
Thumbs up below each post.
June 9, 2017 at 8:49 am
It would be remiss if somebody didn't mention that the root of your issue is that you are storing dates as varchar. You should choose the appropriate datatype for the data being stored. If this was a date datatype instead this would not even be a question you would have needed to ask. You would simply retrieve the data and put your formatting in the front end where it belongs.
_______________________________________________________________
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/
June 9, 2017 at 9:32 am
Sean Lange - Friday, June 9, 2017 8:49 AMIt would be remiss if somebody didn't mention that the root of your issue is that you are storing dates as varchar. You should choose the appropriate datatype for the data being stored. If this was a date datatype instead this would not even be a question you would have needed to ask. You would simply retrieve the data and put your formatting in the front end where it belongs.
Liked it. 🙂
June 11, 2017 at 3:04 pm
Not an option as this is an application database, as such we have no control over how they design their database.
We extract data for reporting purposes only.
June 11, 2017 at 6:27 pm
robert.morgan 63781 - Sunday, June 11, 2017 3:04 PMNot an option as this is an application database, as such we have no control over how they design their database.
We extract data for reporting purposes only.
Are you querying the data in place or populating other tables with the data for reporting? If you have your own tables for reporting, then you can design it well. Your report users will thank you for it.
June 12, 2017 at 2:25 pm
Considering that you don't have the option to make this an actual DATE data type, I would suggest that you leave the data in it's current format. The YYYYMMDD is a universal format that can be correctly translated into a date no matter what collation settings are being used.
If this is just for display purposes in a SELECT statement, you can use something like the following...
DECLARE @StringDate CHAR(8) = '20170609';
SELECT
UK_Format = CONCAT(sv.DY, '/', sv.MO, '/', sv.YR)
FROM
( VALUES (
SUBSTRING(@StringDate, 1, 4),
SUBSTRING(@StringDate, 5, 2),
SUBSTRING(@StringDate, 7, 2)
) ) sv (YR, MO, DY);
-- OR --
SELECT
UK_Format = CONCAT(SUBSTRING(@StringDate, 7, 2), '/', SUBSTRING(@StringDate, 5, 2), '/', SUBSTRING(@StringDate, 1, 4));
Viewing 12 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply