


There are few absolutes in SQL server but you should always always always store datetime data in a datetime column. but this is better done on the client (if at all).

When you have a valid date as a string literal, you can use: SELECT CONVERT (CHAR (10), CONVERT (datetime, '20120101'), 120). Like I said, the best approach is to use datetime. Now you don't have to worry about the formatting - you can always format as YYYYMMDD or YYYY-MM-DD on the client, or using CONVERT in SQL. It’s value is predefined by the SQL Server. style It specifies the format of the date. datetime It can be the expression that evaluates date or datetime value that you want to convert into string. Select BadDate, convert(varchar, cast(CAST(BadDate as CHAR(8)) as datetime), 101) from #BadDates Syntax: CONVERT (VARCHAR, datetime ,style) VARCHAR It represent the string type. This is likely to perform horribly if you have very much data because it has to be fiddled with so much. UGH!!! Display formatting really should happen in the front end.of course that isn't always possible. The real ugliness is that you have to first convert your decimal to a varchar and to a datetime so that you can convert it to a display value. To work with it as is you need to make sure you that your dateformat is the same as the table is stored and that you don't have any values that are not valid datetimes values. I have been working on this for a while.all my efforts to get the right result are futile (ĮEK! Any chance you can make it a datetime? Why decimal?
#Sql convert string to date yyyymmdd code#
That's why the above code is not giving the right result. You are right mycolumn is defined as decimal in its DDL statement.
