Number of records in a period of time PostgreSQL - sql

I have a PostgreSql query, which should make a count of the results that exist between that time frame (between the field "date" and the current time "now"), however the query does nothing but count all the records without applying the filter, does anyone know what I am missing in the query?
This is the query:
SELECT count(*) from table where date between
TO_TIMESTAMP('2022-8-1 12:00:00','YYYY-M-D HH:MI:SS') and now();
Result: 15,480 (all results, does not apply filter "between")
Greetings and thanks

select TO_TIMESTAMP('2022-8-1 12:00:00','YYYY-M-D HH:MI:SS') ;
to_timestamp
------------------------
2022-01-01 00:00:00-08
Per template patterns here Format functionsTable 9.26. Template Patterns for Date/Time Formatting it needs to be:
select TO_TIMESTAMP('2022-08-01 12:00:00','YYYY-MM-DD HH24:MI:SS') ;
to_timestamp
------------------------
2022-08-01 12:00:00-07
Though it would be easier to do something like:
select '2022-8-1 12:00:00'::timestamptz;
timestamptz
------------------------
2022-08-01 12:00:00-07
Ending up with:
SELECT count(*) from table where date between
'2022-8-1 12:00:00'::timestamptz and now();

Related

ORACE How to get begin of the day from CURRENT_TIMESTAMP

I have a problem with solving this problem. I have to extract the IDs of data from a table that has been changed only today. My current solution is
SELECT DISTINCT id
FROM changes_table
WHERE valid_to > (SELECT TO_DATE(CURRENT_DATE, 'DD-MON-RR') FROM DUAL)
This works in BDeaver but not in my application, there I get "ORA-01843:"
So is it possible to use CURRENT_TIMESTAMP, but it take timestamp from now. Is it possible to change it to format DD-MON-RR with 00:00:00 time?
Or is there any other solution?
I can send only one SQL command.
You may simply use SYSDATE here truncated to midnight:
SELECT DISTINCT id
FROM changes_table
WHERE valid_to > TRUNC(SYSDATE); -- RHS is today at midnight
Note that the above should work whether valid_to be a date or a timestamp.

SQL: How to data of current date from timestamp field in oracle?

How to data of current date from timestamp field in oracle?
select
jobstatus0_.JOBNAME as JOBNAME1_21_,
jobstatus0_.STARTDATE as STARTDATE2_21_,
jobstatus0_.ENDDATE as ENDDATE3_21_,
jobstatus0_.REMARKS as REMARKS4_21_,
jobstatus0_.STATUS as STATUS5_21_
from
PD_OWNER.CIM_SNOW_JOB_STATUS_TAB jobstatus0_
where
jobstatus0_.JOBNAME='AGREEMENTS'
AND jobstatus0_.STARTDATE=sysdate <-- Not working with this
We need not to consider Time but date only
I tried following but it is also not working.
and cast(jobstatus0_.STARTDATE as date)=sysdate
Also used which worked
AND to_date(to_char(jobstatus0_.STARTDATE,'DD-MON-YYYY'))=to_date(to_char(sysdate,'DD-MON-YYYY'));
but want more elegant way.
You may use the TRUNC(date) function
Purpose
The TRUNC (date) function returns date with the time portion of the day truncated to the unit specified by the format model fmt. The value returned is always of datatype DATE, even if you specify a different datetime datatype for date. If you omit fmt, then date is truncated to the nearest day. Please refer to "ROUND and TRUNC Date Functions" for the permitted format models to use in fmt.
Examples
The following example truncates a date:
SELECT TRUNC(TO_DATE('27-OCT-92','DD-MON-YY'), 'YEAR')
"New Year" FROM DUAL;
New Year
---------
01-JAN-92
If column STARTDATE is DATE (or TIMESTAMP), you could truncate it (as well as SYSDATE) and get
and trunc(jobstatus0_startdate) = trunc(sysdate)
as trunc will remove time component:
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select trunc(sysdate) trd,
2 trunc(systimestamp) trt
3 from dual;
TRD TRT
------------------- -------------------
06.02.2020 00:00:00 06.02.2020 00:00:00
SQL>
Though, if there was an index on that column, truncating it will make the index unusable, but that's another story and there is a workaround (function based index or making the column value "between" sysdate at this and next midnight).
In postgresql/ mysql use as below
date(jobstatus0_.STARTDATE)=date(now())
In sql-server
cast(jobstatus0_.STARTDATE as date)=cast(CURRENT_TIMESTAMP as date)

two results when i use oracle date

i am running below sql in sql developer:
SELECT count(*)
from FTTH_AMS_DEVICE_METRICS DAT
where TRUNC(DAT.COLLECTION_TMS)>(select start_dt from ETL_JOB_CONTROL where job_name='s_m_ftth_prfrm_fact_tbl')
and TRUNC(DAT.COLLECTION_TMS)<=(select end_dt from ETL_JOB_CONTROL where job_name='s_m_ftth_prfrm_fact_tbl')
i get 38 million rows.
when i use hardcoded values in filter as below and run the query:
SELECT count(*)
from FTTH_AMS_DEVICE_METRICS DAT
where TRUNC(DAT.COLLECTION_TMS)> TO_DATE('10-14-2016','MM-DD-YYYY')
and TRUNC(DAT.COLLECTION_TMS)<= TO_DATE('10-15-2016','MM-DD-YYYY')
i am getting 12 million rows.
because i pass the date values through parameter files in informatica.
I am very much confused how to handle this and get 38 million rows when i run in Informatica.
data type for COLLECTION_TMS is timestamp and end_dt is datetime.
if you need more information on this, i will share immediately.
Thanks for your help.
If the dates are exactly as you show them, then in the second query you should have > to_date ('10-15-2016', 'mm-dd-yyyy') and <= to_date('10-16-2016', 'mm-dd-yyyy'). You are comparing to the wrong dates.
The following is speculation, informed speculation. The date data type in Oracle supports a time component. However, when you print out the date, often the time component is removed.
I am guessing that the values in start_dt and end_dt in ETL_JOB_CONTROL have time components.
If so, the following query should return at least 38 million:
select count(*)
from FTTH_AMS_DEVICE_METRICS DAT
where TRUNC(DAT.COLLECTION_TMS) > TO_DATE('10-14-2016', 'MM-DD-YYYY') and
TRUNC(DAT.COLLECTION_TMS) <= TO_DATE('10-16-2016', 'MM-DD-YYYY');
If this is the case, then just determine the full value for the two columns and use that:
select to_char(start_dt, 'YYYY-MM-DD HH24:MI:SS'),
to_char(end_dt, 'YYYY-MM-DD HH24:MI:SS')
from ETL_JOB_CONTROL;

Update query with compare of date and time

I have a query to update some field on some condition.
Conditions
The time difference is not more than 1 hour and the date can be same.
select *
from Table
where user_cd = 'HARSHIT'
and to_char(sysdate, 'dd/mm/yyyy') = to_char(brth_dt, 'dd/mm/yyyy');
But one condition is also there like at night the user tries to update at 23:30 and after that the he tries next day at 00:15 so the difference is 45 min but the update must execute
select brth_dt from Table where user_cd = 'HARSHIT';
select sysdate from dual;
select brth_dt from Table
where user_cd = 'HARSHIT'
and sysdate-(1/24) < BRTH_DT;
Result of above query
BRTH_DT
25/02/2016 12:30:00
1 row selected.
SYSDATE
24/02/2016 16:7:58
1 row selected.
BRTH_DT
25/02/2016 12:30:00
1 row selected.
I see no reason to convert a date to a string ... if you need to check 2 dates are within an hour of each other, just do the math on the date, and compare :
select * from sir_people
where user_cd = 'HARSHIT'
and BRTH_DT BETWEEN sysdate-(1/24)
AND sysdate;
to_char on a date, for purposes of comparisons, is fundamentally flawed logic and should be avoided.
[edit] based on example provided: it appears you want to exclude future dates, and only include those dates between now, and an hour earlier.
query updated to accomodate that additional requirement.
to_char(col_name, 'yyyy-mm-dd hh24:mi:ss')
just use 24-hour format, I think that should do the work.
Simply translate required condition into sql:
"The time difference is not more than 1 hour and the date can be same."
select *
from Table
where user_cd = 'HARSHIT'
and abs(sysdate-brth_dt) <= 1/24

Select from table by knowing only date without time (ORACLE)

I'm trying to retrieve records from table by knowing the date in column contains date and time.
Suppose I have table called t1 which contains only two column name and date respectively.
The data stored in column date like this 8/3/2010 12:34:20 PM.
I want to retrieve this record by this query for example (note I don't put the time):
Select * From t1 Where date="8/3/2010"
This query give me nothing !
How can I retrieve date by knowing only date without the time?
DATE is a reserved keyword in Oracle, so I'm using column-name your_date instead.
If you have an index on your_date, I would use
WHERE your_date >= TO_DATE('2010-08-03', 'YYYY-MM-DD')
AND your_date < TO_DATE('2010-08-04', 'YYYY-MM-DD')
or BETWEEN:
WHERE your_date BETWEEN TO_DATE('2010-08-03', 'YYYY-MM-DD')
AND TO_DATE('2010-08-03 23:59:59', 'YYYY-MM-DD HH24:MI:SS')
If there is no index or if there are not too many records
WHERE TRUNC(your_date) = TO_DATE('2010-08-03', 'YYYY-MM-DD')
should be sufficient. TRUNC without parameter removes hours, minutes and seconds from a DATE.
If performance really matters, consider putting a Function Based Index on that column:
CREATE INDEX trunc_date_idx ON t1(TRUNC(your_date));
Personally, I usually go with:
select *
from t1
where date between trunc( :somedate ) -- 00:00:00
and trunc( :somedate ) + .99999 -- 23:59:59
Convert your date column to the correct format and compare:
SELECT * From my_table WHERE to_char(my_table.my_date_col,'MM/dd/yyyy') = '8/3/2010'
This part
to_char(my_table.my_date_col,'MM/dd/yyyy')
Will result in string '8/3/2010'
You could use the between function to get all records between 2010-08-03 00:00:00:000 AND 2010-08-03 23:59:59:000
trunc(my_date,'DD') will give you just the date and not the time in Oracle.
Simply use this one:
select * from t1 where to_date(date_column)='8/3/2010'
Try the following way.
Select * from t1 where date(col_name)="8/3/2010"