We help IT Professionals succeed at work.

Date time conversion / storage in SQL server

100 Views
Last Modified: 2019-02-04
Hi

I'm getting via an API date time values in the format 2019-01-20T10:16:49Z

What's my best approach to store them in SQL server ?

I have tried to define a DATETIME column and insert via CONVERT(DATETIME, '2019-01-20T10:16:49Z', 102) )  but it fails...

I'm sure it muss be pretty easy but google isn't my friend so far...
Comment
Watch Question

Mark EdwardsChief Technology Officer
CERTIFIED EXPERT

Commented:
In your string date/time, the "Z" of course stands for the 24-hour clock military "ZULU" time, which is the same as GMT except that "Z" does NOT observe daylight savings time.
You'll need to convert if you want local time.
Also, the "T" isn't going to be recognized by SQL Server, so you'll need to replace it with a space.
So replace the "T" with a space first and get rid of the "Z" and convert to date/time, then calculate local time if you want local.

Author

Commented:
Thanks

So this would not be something that SQL server could interpret natively?
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
Hey Mark
“... ZULU time which is the same as GMT, but Z doesn’t observe daylight saving ...”
The GMT Timezone doesn’t observe daylight saving either!

Author

Commented:
If not, and it is an older format, then 127 is the format that recognizes "Z":
select convert(datetime, '2019-01-20T10:16:49Z', 127)

Thanks - that did the trick !

As a "general" rule, it's best to store dates in UTC time.

Hmm do we actually have control how SQL server stores date/time values? We can format it whichever way we see fit but IMHO the storage is beyond our control...

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions