convert to datetime

How can I convert the string '31.5.2016 00:00:00' to date time.
If I do
SELECT CONVERT(datetime,'31.5.2016 00:00:00)
I get the error:
The conversion of a varchar data type to a datetime data type resulted in an out -of-range value.
johnson1Asked:
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.

Robin CMSenior Security and Infrastructure EngineerCommented:
Specify the date & time in ISO 8601 format:
SELECT CONVERT(datetime,'2016-05-31T00:00:00')
ISO 8601 is always a good idea as it's easy to remember (largest unit to smallest), easy to read, and avoids any confusion over non-US/US date formatting (i.e. day before month vs month before day).
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
gplanaCommented:
If this is SQL-Server, I think you are missing last parameter. CONVERT has 3 parameters, and the third specifies the format.
https://msdn.microsoft.com/en-us/library/ms187928%28v=sql.120%29.aspx
0
Robin CMSenior Security and Infrastructure EngineerCommented:
The third parameter for CONVERT (date_style smallint) is optional.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

gplanaCommented:
Yes, it is optional but I would recommend to use it in order to "explain" to the computer what kind of format are you using on the string.
0
Scott PletcherSenior DBACommented:
Just CAST it.  CAST is more flexible in that it will accept any valid format without you having to provide a specific code for it:

SELECT CAST('31.5.2016 00:00:00' AS datetime)
0
gplanaCommented:
Yes, but the problem is that some dates as 3/2/2015 could be read as febrary the 3th of 2015 or as march the 2nd of 2015. So I think it is a good idea to provide which format are you using to avoid ambiguosities
0
Scott PletcherSenior DBACommented:
Local date settings take care of that.  CAST is the way to go unless you have to use CONVERT.
0
gplanaCommented:
Yes, but if you relay of local date settings, what happens when you run the program on another computer? What if you migrate the server?
0
Scott PletcherSenior DBACommented:
??  Presumably if you're storing dates as strings in a local table, they will be stored in the local format.

If you're entering dates yourself as literals, you should always use format:
'YYYYMMDD hh:mm:ss' --hh=24hr
which works with any settings so you don't need a format code.
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
Microsoft SQL Server 2008

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.