Truncate a date in Postgres (latest version) - database

I have different timestamps within my posgres db. With timestamp I mean unix time since 1970 saved as bigint.
Now I need to get the timestamp since 1970 striped down to days. For instance: 1469059200000 (Today: 2:00 am should become 1469052000000 (Today: 00:00 am)
I've tried this in pgadmin
Select date_trunc('day', rp.timestamp) from rollup_days as rp
but just got this error. ERROR: function date_trunc(unknown, bigint) does not exist

A bigint is not "a timestamp", so you must convert the number to a date before you can apply date_trunc() on it:
Select date_trunc('day', to_timestamp(rp.timestamp))
from rollup_days as rp;
To convert the timestamp back to a bigint, use extract()
Select extract(epoch from date_trunc('day', to_timestamp(rp.timestamp)))
from rollup_days as rp;

Related

Converting Varchar to Timestamp in Snowflake

I have a date defined as varchar. The value of the date field is 19810801. When I am trying to convert into Timestamp using To_TimeSTAMP the value returned is 1970-08-20 06:18:21.000 +0000. I am not sure why snowflake is setting the year as 1970 and month as August.
Do you know what could be the issue here?
Here is my code -
Select to_timestamp_tz(EFFECTIVE_DATE) from Table1
The output returned is 1970-08-20 06:18:21.000 +0000 for the EffectiveDate 19810801
TO_TIMESTAMP is evaluating the value as epoch time:
https://www.epochconverter.com/
If you want to convert it as 1981-08-01, you can use this:
Select to_timestamp_tz('19810801','YYYYMMDD');

SQL Server UTC timestamp

I have stored procedure which inserts data into some table. There is a column say pick up time (datatype datetime). Now the time is in datetime format I want the stored procedure to store datetime to UTC zone timing.
How to do it in the stored procedure?
There are several ways to get the UTC time. Which you use depends on what you need:
SELECT GETUTCDATE(), --datetime
SYSUTCDATETIME(), --datetime2(7)
SYSDATETIMEOFFSET() AT TIME ZONE 'UTC'; --datetimeoffset(7)
This is simple to achieve using GETUTCDATE() as shown:
SELECT GETUTCDATE()
Output:
2021-06-02 12:55:01.213
As per the description in the docs:
Returns the current database system timestamp as a datetime value. The
database time zone offset is not included. This value represents the
current UTC time (Coordinated Universal Time). This value is derived
from the operating system of the computer on which the instance of SQL
Server is running.
SELECT GETDATE() AS PickUpTime, GETUTCDATE() AS UTCTime
Use GETUTCDATE() function in the Insert statement inside your stored procedure:
Insert into MyTable (PickUp_Col)
values (GETUTCDATE() )

Convert Wed Oct 14 08:00:00 CDT 2020 format to DateOnly and Time Only

We receive a csv file that has a column in this date format -- Wed Oct 14 08:00:00 CDT 2020, along with a column that has a count for each date/time
I am using an SSIS package to grab the file and import this data into a sql table, then I can format it the way I need to and then actually export the data in the format needed.
If there is a way to do this all within one SSIS package I am all ears but currently I am working on just getting the data into SQL and converted to the right format so that I can export it.
I need to get that file and convert that date format and split it up into two separate columns
One column will be just the date in this format 2020-10-14 00:00:00.000
One column will be just the time in this format 08:00:00.0000000
Updated to change the dates to match so it's not as confusing and also the error I am receiving when running the suggested code below.
Image of Error I'm recieving
Image of table with the data I am trying to convert
Image of table attributes
Screenshot of my screen when running a select * from the table I am pulling the data that I need converted
Screenshot of the error I receive when running the query by Aaron.
If this is the format it will always be in, and timezone is irrelevant, you can first try to convert it to a datetime, then you can extract the parts from that.
SET LANGUAGE us_english; -- important because not all languages understand "Oct"
;WITH src AS
(
SELECT dt = TRY_CONVERT(datetime, RIGHT(OpenedDateTime ,4)
+ SUBSTRING(OpenedDatetime, 4, 16))
--, other columns...
FROM [dbo].[VIRTUALROSTERIMPORT_Res_Import]
)
SELECT OpenedDateTime = CONVERT(datetime, CONVERT(date, dt)),
OnHour = CONVERT(time, dt)
--, other columns...
FROM src;
Results:
OpenedDateTime OnHour
-------------- ----------------
2020-10-14 08:00:00.0000000
If you need to shift from one timezone to another timezone, that's a different problem.
I was just showing the date formats, don't look so into the actual date examples I used. The time zone is irrelevant I just need the formats changed.
When I used The code Aaron suggested I got a conversion error: I'm assuming its because the columns are varchar in the table, but I cant get the dates to load as date formats bc SSIS keeps giving me truncated errors-- so I have to load it as varchar.
Below is the code I was running, I tweaked it to use the column and table names I am using.
SET LANGUAGE us_english; -- important because not all languages understand "Oct"
DECLARE #foo varchar(36) = 'Wed Oct 14 08:00:00 CDT 2020';
;WITH src(d) AS
(
SELECT TRY_CONVERT(datetime, RIGHT(#foo,4) + SUBSTRING(#foo, 4, 16))
)
SELECT OpenedDateTime = CONVERT(datetime, CONVERT(date, OpenedDateTime)),
onhour = CONVERT(time, OpenedDateTime)
FROM [dbo].[VIRTUALROSTERIMPORT_Res_Import];

there were some errors in the query: RequestError: Conversion failed when converting date and/or time from character string

i am using MS SQL Server 2008 when i run this query
select top 1 * from table_name where ModifiedDate='Wed Mar 16 2016 15:52:20 GMT+0530 (IST)'
i get this error
there were some errors in the query: RequestError: Conversion failed when converting date and/or time from character string.
i also tried CAST and CONVERT but having same error
i am using node mssql client
To answer the question directly, use this instead:
select top 1 * from table_name where ModifiedDate=cast('2016-03-16 15:52:20 +05:30' as datetimeoffset)
Now the explanation. To use timezones, you need to use datetimeoffset - regular datetime fields don't include timezone info. So, this will work:
select cast('2016-7-16 15:52:20 +05:30' as datetimeoffset)
Using AdventureWorks2012 (this syntax will also work in sql 2008, but this is demoing how you'd write this query against the AW2012 database):
select top 1 * from Person.Address where ModifiedDate=cast('2002-01-04 00:00:00 +00:00' as datetimeoffset)
Note in this case, AW2012 uses a datetime field, but not a datetimeoffset field in this table - nevertheless, it compiles and runs.
The next example demonstrates how the timezone offset works:
select top 1 * from Person.Address where ModifiedDate=cast('2002-01-04 04:00:00 +04:00' as datetimeoffset)
This returns a value, whereas changing the offset by 1 hour returns no result. (There's one record with a ModifiedDate of 2002-01-04 00:00:00 in the table):
select top 1 * from Person.Address where ModifiedDate=cast('2002-01-04 04:00:00 +03:00' as datetimeoffset)

Convert datetime of a timezone to GMT in sql server

I have a requirement of converting a record of time w.r.to timezone and compare it with current time.
For this, I want to convert datetime of a timezone to GMT timezone in SQL server.
i.e. (Jan 12 2015 11:30 A.M +5:30--->GMT standard time
Also is it possible to find out client's time zone from sql server?
If your datetime is stored as a datetimeoffset (or convertible to it) you can simply convert to a datetime2 (assuming you want to keep the precision of the datetimeoffset):
declare #dt datetimeoffset
select #dt = 'Jan 12 2015 11:30 AM +05:30'
select convert(datetime2, #dt, 1)
Which returns the time in UTC:
2015-01-12 06:00:00.0000000
This has nothing to do with the users' timezone, as it is simply doing the timezone to UTC calculation based on your provided offset.
SqlFiddle

Resources