SQL Server Epoch Converter

Convert Unix epoch timestamps to SQL Server datetime2 values and back, then copy the DATEADD() and DATEDIFF_BIG() queries for your value.

Client-side only, nothing leaves your browser

Auto-detected: seconds  ·  3.7y ago

Conversions

Epoch (seconds)
1679616000
Epoch (milliseconds)
1679616000000
Human Readable
Friday, March 24, 2023 at 04:00:00 AM GMT+4
ISO 8601
2023-03-24T00:00:00.000Z
SQL Server datetime2 (UTC)
2023-03-24 00:00:00
datetimeoffset
2023-03-24 00:00:00 +00:00

SQL Server SQL

Epoch to datetime2
SELECT DATEADD(second, 1679616000, CAST('1970-01-01' AS datetime2));
Epoch milliseconds to datetime2
SELECT DATEADD(millisecond, 0, DATEADD(second, 0, DATEADD(day, 19440, CAST('1970-01-01' AS datetime2(3)))));
Convert to a local time zone
SELECT DATEADD(second, 1679616000, CAST('1970-01-01' AS datetime2)) AT TIME ZONE 'UTC' AT TIME ZONE 'Eastern Standard Time';
Format as text
SELECT CONVERT(varchar(19), DATEADD(second, 1679616000, CAST('1970-01-01' AS datetime2)), 120);

SQL Server Epoch & Unix Timestamps

SQL Server has no FROM_UNIXTIME() or UNIX_TIMESTAMP(). To get a date, add the epoch seconds to 1970-01-01 with DATEADD(). To get an epoch, count the seconds from 1970-01-01 with DATEDIFF(), or DATEDIFF_BIG() when the number won't fit in an int. Through SQL Server 2022, the number you pass to DATEADD() must fit in an int, so a millisecond epoch has to be split into seconds and a remainder. SQL Server 2025 accepts a bigint. The result is UTC with no offset attached, and AT TIME ZONE converts it to a local zone.

Quick Reference

  • DATEADD(second, epoch, '1970-01-01'): Epoch seconds to a datetime
  • DATEDIFF(second, '1970-01-01', dt): Datetime to epoch seconds as an int
  • DATEDIFF_BIG(millisecond, '1970-01-01', dt): Datetime to epoch milliseconds as a bigint
  • AT TIME ZONE 'zone': Attach or convert a time zone, using Windows zone names
  • SYSUTCDATETIME(): Current UTC date and time as datetime2

Notable Epoch Values

EventEpoch (seconds)Date
Unix Epoch0January 1, 1970 00:00:00 UTC
1 Billion1000000000September 9, 2001 01:46:40 UTC
Y2K38 (32-bit limit)2147483647January 19, 2038 03:14:07 UTC
2 Billion2000000000May 18, 2033 03:33:20 UTC
Current (approx)~1774000000March 2026

Frequently asked questions

What is timestamp in SQL Server?
In SQL Server, timestamp is a deprecated synonym for rowversion, an 8-byte counter that changes every time a row is inserted or updated. It holds no date or time, so you can't convert it to a Unix epoch. Store real dates in datetime2, or in a bigint column if you need the raw epoch number.
Is there a FROM_UNIXTIME function in SQL Server?
SQL Server has no built-in Unix time function, so you build the conversion from DATEADD and the epoch start date: SELECT DATEADD(second, 1711238400, CAST('1970-01-01' AS datetime2)); The CAST matters, because with a plain string literal DATEADD returns the older datetime type. Through SQL Server 2022 the seconds argument is an int, which overflows for dates after 2038-01-19 03:14:07 UTC. Add whole days first in that case: DATEADD(second, @epoch % 86400, DATEADD(day, @epoch / 86400, CAST('1970-01-01' AS datetime2))).
How do I get the current Unix timestamp in SQL Server?
Count the seconds from the epoch to the current UTC time: SELECT DATEDIFF_BIG(second, '1970-01-01', SYSUTCDATETIME()); Use SYSUTCDATETIME() or GETUTCDATE() rather than GETDATE(), which returns the server's local time and gives a wrong epoch on any server not set to UTC. DATEDIFF_BIG needs SQL Server 2016 or later.
How do I convert a Unix timestamp in milliseconds to datetime in SQL Server?
A current millisecond epoch is a 13-digit number, too large for the int that DATEADD accepts through SQL Server 2022, and the query fails with an arithmetic overflow error. Split it into seconds and the millisecond remainder: SELECT DATEADD(millisecond, @ms % 1000, DATEADD(second, @ms / 1000, CAST('1970-01-01' AS datetime2(3)))); On SQL Server 2025, DATEADD takes a bigint, so DATEADD(millisecond, @ms, ...) works in one step.
How do I convert a datetime to epoch milliseconds in SQL Server?
Use DATEDIFF_BIG, which returns a bigint: SELECT DATEDIFF_BIG(millisecond, '1970-01-01', @dt); Plain DATEDIFF returns an int, so in milliseconds it overflows once two dates are more than about 24 days apart, and in seconds it fails for dates after January 2038. DATEDIFF_BIG is available from SQL Server 2016 and in Azure SQL Database.
SQL Server

Want 1bench for SQL Server?

1bench doesn't connect to SQL Server yet. Ask for it and we'll let you know as soon as early access opens.

Request Early Access