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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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).

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
If this is SQL-Server, I think you are missing last parameter. CONVERT has 3 parameters, and the third specifies the format.
Robin CMSenior Security and Infrastructure EngineerCommented:
The third parameter for CONVERT (date_style smallint) is optional.
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

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.
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)
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
Scott PletcherSenior DBACommented:
Local date settings take care of that.  CAST is the way to go unless you have to use CONVERT.
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?
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.
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.