I have developed an application in C# / WPF which saved nvarchar
columns in the table in two formats, one is with hyphens and on with forward dashes, like so: '01-Sep-2017' to '01/09/2017'
The simplest solution would be to convert the date which is in format '01-Sep-2017' to '01/09/2017' while in query and get the data as it should be?
How can I do that?
Or should I use some sort of update statement to convert and save the date into appropriate format?
Note: but if would not be easy. I have different data for the two formats. It should be collapsed into the same date.
You can view the formats here: https://docs.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql
You would need to be sure that your SQL Server is installed in English. Once you are sure about that the convert function should do the job
And if you need to turn that into that string you shared:
Style Reference here
As noted by practically everyone on the comments, and anyone that knows what they are doing when it comes to databases, storing dates as strings is a terrible idea, that can only lead to problems.
Here is a (very) short list of reasons:
'13/09/2017'
in one row and'Banana'
in another one.'13/14/1000'
or'31-Feb-2020'
or even'01-Dev-1999'
'01-Dez-2017'
is December first 2017, in German, for instance.DateTime
struct maps directly to SQL ServerDate
,DateTime
,DateTime2
data types. Storing dates as strings means you have to do extra work passing dates between the application layer and the database.Having said all that, If you still absolutely can't refactor your database to store dates properly, you can use
convert
to change the string representation of dates to proper dates. Since it's 2008 version,Try_convert
is off the table, so you will need to use a couple of common table expressions to handle the different string representations of dates: