format different sql dates into mm/dd/yyyy format

I have an sql server 2012 order table that has dates in various formats stored in a varchar field  

here is a sample

order id
    1                     Null
    2                   12-01-2017
    3                    01/01/93
    4                    01/03/16
    5                     12/01/1999
     6                    2-2-07

trying to write a query that would "force" dates into mm/dd/yyyy format

(no year is before1990)

if the date is null would like it formatted as 01/01/1900

given the sample I would like to see

ordered  date
1               01/01/1900
2               12/01/2017
3                01/01/1993
4                01/03/2016
5                12/01/1999
6                02/02/2007
johnnyg123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

johnnyg123Author Commented:
ms sql server 2012
0
Bill PrewCommented:
Give this a try.

SELECT [Line], CONVERT(date, IsNull([order id], '1-1-1900')) AS [order id] FROM
(SELECT 1 AS [Line], NULL AS [order id]
UNION
SELECT 2 AS [Line], '12-01-2017' AS [order id]
UNION
SELECT 3 AS [Line], '01/01/93' AS [order id]
UNION
SELECT 4 AS [Line], '01/03/16' AS [order id]
UNION
SELECT 5 AS [Line], '12/01/1999' AS [order id]
UNION
SELECT 6 AS [Line], '2-2-07' AS [order id]) As t1

Open in new window


»bp
0
Garfield SamuelsProject ManagerCommented:
Try:

SELECT convert(datetime, [ordered  date], 101)
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Mark WillsTopic AdvisorCommented:
You can use FORMAT, but need to check if it is a date to use format

e.g. select iif(isdate(yourdate) = 1,format(cast(yourdate as date),'d','en-US'),'01/01/1900')

That leaves it as a string, or convert to a date data type...

select iif(isdate(yourdate) = 1,cast(yourdate as date),'1900-01-01')

edit : select iif(isdate(yourdate) = 1,format(cast(yourdate as date),'MM\/dd\/yyyy'),'01/01/1900')
0
Scott PletcherSenior DBACommented:
If all the dates are good, you can do something like this:

SELECT CONVERT(varchar(10), CAST(NULLIF(column_name, 'NULL') AS date), 101)

Btw, that format seems like a bad idea, since it doesn't compare accurately nor sort accurately.  You should consider using yyyy-mm-dd.
0
Dean ChafeeIT/InfoSec ManagerCommented:
udpate [tblname] set [order date]  = replace( CAST([order date] AS varchar),'-','/')  where isdate([order date]) =1;
go
udpate [tblname] set [order date]  = '01/01/1900'  where [order date] = NULL;
go
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NerdsOfTechTechnology ScientistCommented:
TRY_CONVERT is a safe approach to the date conversion as on error (from say a malformed or invalid date) it returns null; this null and a natural null can then default to 01/01/1900.
 SELECT 
 `order_id`,
 IsNull(TRY_CONVERT(DATE, `order_date`, 101), CONVERT(DATE,'01/01/1900')) AS `ordered_date`
 FROM orders

Open in new window

0
johnnyg123Author Commented:
Thanks too all that responded
needed is date check as part of it
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.