Answered by:
Format the date in varchar type to mm/dd/yyyy

Question
-
Hi,
I have a column originally in date format. Later on, I changed it to nvarchar and all the existing date changed to this format "yyyy-mm-dd 00:00:00". How could I update all those dates to mm/dd/yyyy format. Should I use the expression?
Thanks.
Wednesday, February 18, 2015 3:01 PM
Answers
-
I would highly recommend you change it back to a date/time type as this is what you are storing.
- MS Sql Server has MANY ways to return dates and times formatted as you want so depending on what you want to see and how you want to see it having the data as date time is infinity better. Case in point is your current predicament.
- Sorting and filtering and comparisons on date/time types is more efficient and less error prone
- Date/time types already have built in validation, trying to enter a date of february 30 is not possible or 27 hours etc.
- You can execute date/time functions like adding a year or subtracting a month which is very useful when you start to make use of the dates/times
- Writing code against databases where dates/times are stored as strings is a horrible experience that I do not wish on anyone!
I currently have to maintain a DB that was originally created by my predecessor with date and times everywhere as varchar and it is a nightmare, Don't Do It especially if you have the chance now to revert back!!! :)
-Igor
- Proposed as answer by Qiuyun YuMicrosoft contingent staff Thursday, February 19, 2015 7:57 AM
- Marked as answer by hypeonfire Thursday, February 19, 2015 2:16 PM
Wednesday, February 18, 2015 3:45 PM
All replies
-
Try this
update <Tablename> SET start_Date=CONVERT(VARCHAR(10), datecolumn, 101)
--PRashanth
Wednesday, February 18, 2015 3:06 PMAnswerer -
Try this
update <Tablename> SET start_Date=CONVERT(VARCHAR(10), datecolumn, 101)
--PRashanth
Wednesday, February 18, 2015 3:36 PM -
- Proposed as answer by Qiuyun YuMicrosoft contingent staff Thursday, February 19, 2015 8:00 AM
Wednesday, February 18, 2015 3:40 PM -
I would highly recommend you change it back to a date/time type as this is what you are storing.
- MS Sql Server has MANY ways to return dates and times formatted as you want so depending on what you want to see and how you want to see it having the data as date time is infinity better. Case in point is your current predicament.
- Sorting and filtering and comparisons on date/time types is more efficient and less error prone
- Date/time types already have built in validation, trying to enter a date of february 30 is not possible or 27 hours etc.
- You can execute date/time functions like adding a year or subtracting a month which is very useful when you start to make use of the dates/times
- Writing code against databases where dates/times are stored as strings is a horrible experience that I do not wish on anyone!
I currently have to maintain a DB that was originally created by my predecessor with date and times everywhere as varchar and it is a nightmare, Don't Do It especially if you have the chance now to revert back!!! :)
-Igor
- Proposed as answer by Qiuyun YuMicrosoft contingent staff Thursday, February 19, 2015 7:57 AM
- Marked as answer by hypeonfire Thursday, February 19, 2015 2:16 PM
Wednesday, February 18, 2015 3:45 PM -