(SQL Server) Get Current Date/Time as Timestamp (YYYY-MM-DDThh:mm:ssTZD)
Demonstrates how to get the current system date/time in YYYY-MM-DDThh:mm:ssTZD format.
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @dt int
-- Use "Chilkat_9_5_0.CkDateTime" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @dt OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @success int
EXEC sp_OAMethod @dt, 'SetFromCurrentSystemTime', @success OUT
-- Get a UTC time.
DECLARE @bLocal int
SELECT @bLocal = 0
DECLARE @timestamp nvarchar(4000)
EXEC sp_OAMethod @dt, 'GetAsTimestamp', @timestamp OUT, @bLocal
PRINT 'Current UTC Time: ' + @timestamp
-- Get a local time.
SELECT @bLocal = 1
EXEC sp_OAMethod @dt, 'GetAsTimestamp', @timestamp OUT, @bLocal
PRINT 'Current Local Time: ' + @timestamp
-- Sample output:
--
-- Current UTC Time: 2022-03-01T00:48:58Z
-- Current Local Time: 2022-02-28T18:48:58-06:00
EXEC @hr = sp_OADestroy @dt
END
GO
|