SQL server dates?
Author
Discussion

aclivity

Original Poster:

4,072 posts

220 months

Wednesday 7th February 2018
quotequote all
This must be possible ... how?

I have a date field set up as varchar (database comes from elsewhere so can't change it) that has dd-m-yyyy hh:mm on it.

I want to use SQL date functions so I need to convert it to yyyy-mm-dd hh:mm - but I can't work out what to do? I have tried using convert but it has failed.

Anyone got a clue what to do?

plasticpig

12,932 posts

257 months

Wednesday 7th February 2018
quotequote all
CONVERT(datetime,'12-03-2018 10:09:33',105) works for me.





aclivity

Original Poster:

4,072 posts

220 months

Wednesday 7th February 2018
quotequote all
D'oh

Tried that for date ... Didn't try it with time.

Thank you - silly me!

plasticpig

12,932 posts

257 months

Wednesday 7th February 2018
quotequote all
No problem. Your probably better off using TRY_CONVERT rather than CONVERT though.

aclivity

Original Poster:

4,072 posts

220 months

Wednesday 7th February 2018
quotequote all
Similar question on time - convert from an 5 digit number to a date / time, is it simply using dateadd?

plasticpig

12,932 posts

257 months

Wednesday 7th February 2018
quotequote all
If this is from Excel data? If so CONVERT(smalldatetime,43138 -2). You need to take away 2 as for some reason MS chose different starting dates for Office and SQL server.