- 0
[T-SQL] Converting between DateTime2(7), Time(7) and BigInt in SQL 2012/2014
-
Recently Browsing 0 members
- No registered users viewing this page.
-
Similar Content
-
- 1 reply
- 0 views
-
SQL Server 2022 gets data virtualization, REST APIs with PolyBase
By Hamza Jawad,
- microsoft
- sql server 2022
- (and 5 more)
- 0 replies
- 3 views
-
Get this Complete 2021 SQL Master Class Bundle for only $19.99
By News Staff,
- neowin deals
- sql
- (and 6 more)
- 0 replies
- 5 views
-
- 0 replies
- 167 views
-
Save 98% off this Complete Web Developer Coding Bundle
By News Staff,
- neowin deals
- sponsored
- (and 13 more)
- 0 replies
- 370 views
-
Question
Squirrelington
I ran into a situation where I wanted to get the difference between 2 datetime2(7)s and a time(7). The issue was that subtraction of a datetime2 is not supported.
DATEDIFF(ns, 0, cast('2015-01-01 00:00:00.0000000' as datetime2(7))) wouldn't work for converting to nanoseconds because int doesn't support that large of a value and DATEDIFF)BIG doesn't exist until SQL Server 2016 and I didn't want to extract each individual DATEPART the reconstruct the value after getting the differences from each section.
So I wrote a few scalar functions for SQL to allow conversion between the 3 formats. They're listed below, hopefully this helps someone.
Examples:
Link to comment
https://www.neowin.net/forum/topic/1296568-t-sql-converting-between-datetime27-time7-and-bigint-in-sql-20122014/Share on other sites
2 answers to this question
Recommended Posts