I am inserting into the table with field type "timestamp with time zone" string "1858-11-17 01:09:05+0000" and getting back strage formated value "05:11:29+04:02:24".
Here is session
test=> create table ddtbl (val timestamp with time zone);
CREATE TABLE
test=> insert into ddtbl (val) values ('1858-11-17 01:09:05+0000');
INSERT 0 1
test=> select * from ddtbl;
val
------------------------------
1858-11-17 05:11:29+04:02:24
Why is this happening and what is "+04:02:24" here ?
UPD: PostgreSQL version
% psql --version
psql (PostgreSQL) 9.2.4
UPD2: Local timezone
% date +%Z
YEKT
% date +%z
+0600
This is an effect of the time zone. Before early 20th century many countries (like Germany or Russia) had completely different regimes like "mean solar time" which would not translate cleanly to UTC.
Therefore a time in time zone 0 (GMT at the time, as there was no UTC yet) would have an odd time offset when represented as local time for Yekaterinburg (Russia).
+04:02:24 is the actual offset as compared to UTC.
It is interpreting your input value is UTC.
psql=# select cast('1858-11-17 01:09:05 UTC' as timestamp with time zone);
timestamptz
------------------------
1858-11-17 01:09:05+00
(1 row)
psql=# select cast('1858-11-17 01:09:05 BRT' as timestamp with time zone);
timestamptz
------------------------
1858-11-17 04:09:05+00
(1 row)
The two values are the just different representations of the same timestamp.
psql=# select cast('1858-11-17 05:11:29+04:02:24' as timestamp with time zone) = cast('1858-11-17 01:09:05+0000' as timestamp with time zone);
?column?
----------
t
(1 row)
Related
While running the below code i get an error saying invalid input syntax for type timestamp from admission_datetime.
UPDATE ccsm.stg_demographics_baseline
SET xx_los_days =
(CASE WHEN admission_datetime IS NULL OR
date_trunc('day',admission_datetime) = ''
THEN NULL
WHEN discharge_datetime IS NULL OR
date_trunc('day',discharge_datetime) = ''
THEN date_diff('day', admission_datetime, CURRENT_DATE)
ELSE
date_diff('day', admission_datetime, discharge_datetime)
END);
enter code here
See date_trunc documentation:
The return value is of type timestamp or interval with all fields that are less significant than the selected one set to zero (or one, for day and month).
So you can not compare it with an empty string:
date_trunc('day', admission_datetime) = ''
The invalid input syntax for type timestamp error message concerns the empty string (''), not the admission_datetime column.
Furthermore, there is no date_diff function in PostgreSQL. Just subtract one timestamp from another and you will get an interval result:
SELECT timestamp '2001-09-29 03:00' - timestamp '2001-09-27 12:00'
You'll get
interval '1 day 15:00:00'
If you need the difference in days, try this:
SELECT DATE_PART('day', timestamp '2001-09-29 03:00' - timestamp '2001-09-27 12:00')
The result is 1.
See here for examples of DATEDIFF-like expressions in PostgreSQL.
My objective is to count Lead records based on simple date range query in local time
Integer TotalLeads = [ Select Count() From Lead where createddate >= fromdate CreatedDate <= todate];
Fairly basic. However, the issue I'm running into is I only want to count the leads for the "local" time not UTC; createddate is in UTC for lead records.
Sample dates:
From: 03/23/2017
To: 03/29/2017
For these sample dates and my local time is UTC - 7 (Los Angeles), so my query would be
Integer TotalLeads = [ Select Count() From Lead where createddate >= 2017-03-23T07:00:00z AND CreatedDate <= 2017-03-30T06:59:59z];
If these are my dates, how do I append the local time so from date is 2017-03-23T07:00:00z and to date is 2017-03-30T06:59:59z?
Using from date first, I was able to do the following but can't figure how to keep it in local time
// Date
date ds = date.valueof('2017-03-23');
string dm = string.valueOf( ds.month());
string dd = string.valueOf(ds.day());
string dy = string.valueOf(ds.year());
// DateTime Midnight (UTC)
String SDate = string.valueof(dm +'/' + dd + '/' + dy + ' 12:00 AM');
system.debug(SDate); // -> 3/23/2017 12:00 AM
// DateTime (Local Time)
datetime ds2 = datetime.parse(SDate );
system.debug(ds2); // -> 2017-03-23 07:00:00
system.debug(ds2.format('yyyy-MM-dd\'T\'hh:mm:ss'')); // -> 2017-03-23T12:00:00
As you can see, using ds2.format put its in the format I need but back to UTC (midnight), I need it to be 2017-03-23T07:00:00
Any suggestions?
Thanks!
Figured what I was doing wrong. The date calculation was fine, it had to do with how this was being passed to a batch job.
I want to pull today's date plus the last four weeks. Does anyone know a function for this in Netezza? What I have below is a guess that doesn't work. Also, I don't want to extract the date.
Select c.BUSINESS_UNIT_NBR, c.BUSINESS_UNIT_NAME, b.STORE_NBR, b.INV_CUST_ACNT_NBR,c.INV_CUST_NAME, a.NDC_NBR, a.GENERIC_NAME, a.INV_NBR, a.CONTRACT_ID, a.CONTRACT_NAME, a.ORD_DT, b.INV_DT, b.SHIP_DT, a.ORD_QTY, a.SHIPPED_QTY, a.INV_PRICE_AMT, a.INV_COST_AMT, a.MARKUP_MARKDOWN_PCT, a.INV_LINE_AMT
from fct_dly_invoice_detail a, fct_dly_invoice_header b, dim_invoice_customer c
where a.INV_HDR_SK = b.INV_HDR_SK
and b.DIM_INV_CUST_SK = c.DIM_INV_CUST_SK
and a.SRC_SYS_CD = 'ABC'
and a.NDC_NBR is not null
**and b.inv_dt(current_date)-16**
and b.store_nbr in (813, 1197, 2771, 3048, 3177, 3387, 3477, 3602, 3766, 3912, 4020, 4138, 4228, 4434, 4435, 4507, 4742, 4791, 5353, 5392, 5775, 5776, 5890, 6177, 6692, 6736, 6806, 7933, 9175, 9472)
Assuming inv_dt is the column you want to filter on, your where predicate should include:
WHERE
...
inv_dt between CURRENT_DATE - 16 and CURRENT_DATE
...
16 days does not equal four weeks, but adjust that number accordingly to your needs.
I have One Table DailyPerformanceReport(with Two Time Columns) with data like this in SQL Server 2008R2
i want to get difference of InPunch and OutPunch as WorkHRs(in Time format).Myexpected output
Try this:
SAMPLE DATA
CREATE TABLE DailyPerformanceReport(
InPunch TIME,
OutPunch TIME
)
INSERT INTO DailyPerformanceReport VALUES
('09:14:00', '19:22:00'),
('09:54:00', '19:37:00'),
('09:14:00', '18:39:00');
QUERY
SELECT
*,
WorkHRS = CAST(DATEADD(MILLISECOND, DATEDIFF(MILLISECOND, InPunch, OutPunch), 0) AS TIME)
FROM DailyPerformanceReport
RESULT
InPunch OutPunch WorkHRS
---------------- ---------------- ----------------
09:14:00.0000000 19:22:00.0000000 10:08:00.0000000
09:54:00.0000000 19:37:00.0000000 09:43:00.0000000
09:14:00.0000000 18:39:00.0000000 09:25:00.0000000
You can try with the datediff functionality.
select DATEDIFF(MINUTE ,'value1',DateAdd(day, 1, 'value2'))
Currently I'm using Code On Time to develop my system.
I got a default current date and time, after the user selects a completed date from lookup, I want to calculate how many days and hours have been taken by the user.
Is there any way to do this?
Is this what you're after?
declare #dateTimeNow datetime = getutcdate()
, #dateTimeThen datetime = '2012-11-28 12:00:00'
select case when DATEPART(hour,(#dateTimeNow - #dateTimeThen)) >0 then day(#dateTimeNow - #dateTimeThen)-1 else day(#dateTimeNow - #dateTimeThen) end days
, DATEPART(hour,(#dateTimeNow - #dateTimeThen)) hours
or
select DATEDIFF(day,#datetimethen, #datetimenow) - case when (DATEDIFF(Hour,#datetimethen, #datetimenow)) % 24 = 0 then 0 else 1 end days
, DATEDIFF(hour,#datetimethen, #datetimenow) % 24 hours