I am trying to run a simple query that excludes all records from the table which are greater than the maximum value of a date column. e.g.
SELECT * FROM TABLE1
WHERE LD_TMSTMP > (SELECT MAX(LD_TMSTMP) FROM TABLE1)
===========================================
0 records returned
This query should return zero records and it does that. However when I try to run the inner query I get this:
SELECT MAX(LD_TMSTMP) FROM TABLE1
===========================================
2015-04-22 06:42:32
And when I put this value in the same query I get 131 records
SELECT * FROM TABLE1
WHERE LD_TMSTMP > TO_DATE('2015-04-22 06:42:32','YYYY-MM-DD HH24:MI:SS')
===========================================
131 records returned
Does anyone know why this happens? Do I need to use a better precision value when returning the date in string format?
The problem here is that you are confusing DATE with TIMESTAMP. The tipoff is in your title, which references a DATE, but your data shows you are clearly dealing with a TIMESTAMP.
From your SQL we can see you are using TO_DATE rather than TO_TIMESTAMP to convert your character representation, which unsurprisingly gives us a DATE.
TESTDB.ADMIN(ADMIN)=> select TO_DATE('2015-04-22 06:42:32','YYYY-MM-DD HH24:MI:SS');
TO_DATE
------------
2015-04-22
(1 row)
TESTDB.ADMIN(ADMIN)=> select to_timestamp('2015-04-22 06:42:32','YYYY-MM-DD HH24:MI:SS');
TO_TIMESTAMP
---------------------
2015-04-22 06:42:32
(1 row)
Related
I am facing an issue extracting the month and year from the data (in Character varying type) >> InvoiceDate in SQL Postgres. I have seen the solution is relatively easy with MySQL function: DATEFROMPARTS as per the below Code which is not available in SQLpostgres. How can I get the same result DATA_PART function in Postgres SQL, but simultaneously I need to change the data type of the column "InvoiceDate" to the date
Select
CustomerID,
min(InvoiceDate) first_purchase_date,
DATEFROMPARTS(year(min(InvoiceDate)), month(min(InvoiceDate)), 1) Cohort_Date
into #cohort
from #online_retail_main
group by CustomerID
The output:
Customer ID| first_purchase_date |Cohort_Date|
-----------+-------------------------+-----------+
12345 | 2010-12-20 15:47:00:00 | 2010-12-01|
I am trying to make a date consits of Year and Month , while the day to be set as 1 for all
Assuming a valid Postgres timestamp:
select date_trunc('month', '2010-12-20 15:47:00.00'::timestamp)::date;
date_trunc
------------
12/01/2010
--or ISO format
set datestyle = 'ISO,MDY';
select date_trunc('month', '2010-12-20 15:47:00.00'::timestamp)::date;
date_trunc
------------
2010-12-01
Uses date_trunc to truncate the timestamp to a month which means the first of the month. Then cast(::date) to a date type. The DateStyle just deals with how the value is presented to the user. The value is not stored formatted.
To do something similar to what you did in MySQL:
select make_date(extract(year from '2010-12-20 15:47:00.00'::timestamp)::integer, extract(month from '2010-12-20 15:47:00.00'::timestamp)::integer, 1);
This uses make_date from here Date/time functions and extract to build a date.
I have a postgres database carrying date/time information in a text format. There is no way of changing it, but I have to retrieve those values as milisecons since epoch.
I managed to make a query, converting those date-time records to timestamps so that I get a correct "max" function behaviour like so:
SELECT max(TO_TIMESTAMP(column_name, 'YYYY/MM/DD HH24:MI:SS'))
FROM table_name;
But converting other results into miliseconds does not seem to work. And all the examples int the documentation and forums showcase only the usage for some literal value, not a value selected from a database. So lines like these don't work:
SELECT EXTRACT(EPOCH FROM TIMESTAMP
(select max(TO_TIMESTAMP(column_name, 'YYYY/MM/DD HH24:MI:SS'))
FROM table_name));
SELECT EXTRACT(EPOCH FROM TIMESTAMP
(select TO_TIMESTAMP(column_name,'YYYY/MM/DD HH24:MI:SS')
FROM table_name));
SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE(
SELECT TO_TIMESTAMP(column_name, 'YYYY/MM/DD HH24:MI:SS')
FROM table_name));
SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE TO_TIMESTAMP
(column_name, 'YYYY/MM/DD HH24:MI:SS'))
FROM table_name;
Is there an actual way to accomplish what I want by using a query, or I have to do something more complicated?
P.S.
Of course I can just retrieve all the infomation as text and use Qt (QDateTime) to convert it to miliseconds, but It would be more expensive and I was wondering if there is a way to ask the database to do it for me.
The timestamp keyword is only needed for literals (constants), not if you have a proper timestamp value available:
SELECT extract(epoch from max(TO_TIMESTAMP(column_name, 'YYYY/MM/DD HH24:MI:SS')))
FROM table_name;
Note that epoch represents seconds, not milliseconds.
I am currently searching my transaction table using the following query. The second TO_TIMESTAMP is specified as 2019-08-21..
SELECT t.* FROM Transaction t WHERE t.datetime >= TO_TIMESTAMP('2019-01-01T07:54:34','YYYY-MM-ddTHH:MI:SS')
AND t.datetime < TO_TIMESTAMP('2019-08-21T14:38:34','YYYY-MM-ddTHH:MI:SS') AND (t.location_1 = 2001 OR t.location_2 = 2001);
It returns me the following result:
When i change the second TO_TIMESTAMP to 2019-08-22.. It returns me the current day's result. I am not sure why I need to add one more day in order to retrieve the current day's result..
The current timezone in the PostgreSQL 9.6 is:
This is your problem:
SELECT TO_TIMESTAMP('2019-08-21T14:38:34','YYYY-MM-ddTHH:MI:SS');
to_timestamp
------------------------
2019-08-21 00:38:34+00
(1 row)
The unescaped T confuses the parser (the actual result may even be buggy).
Use the correct format string:
SELECT TO_TIMESTAMP('2019-08-21T14:38:34','YYYY-MM-DD"T"HH24:MI:SS');
to_timestamp
------------------------
2019-08-21 14:38:34+00
(1 row)
I m querying data between two date range in PostgreSQL SQL but it does not give me expected result.
select
pi_serial,
amount_in_local_currency,
status,
proforma_invoice_date
from proforma_invoice
where created_by = 25
and proforma_invoice_date BETWEEN '03/01/2018' and '09/03/2018'
order by proforma_invoice_date
Now look at the query and column proforma_invoice_date. In this query i am searching data between 03/01/2018 and 09/03/2018. the date format is (DD/MM/YYYY) and it's character varying. The result i have got in this picture. it just give me the result according by the only day but not the whole date format. i have tried so many things date conversion, character varying to date. but i didn't get any expected result
Your Query is absolutely fine. just change it as follows
select pi_serial, amount_in_local_currency, status, proforma_invoice_date
from proforma_invoice
where created_by = 25
and to_date(proforma_invoice_date, 'DD/MM/YYYY') BETWEEN to_date('03/01/2018', 'DD/MM/YYYY') and to_date('09/03/2018', 'DD/MM/YYYY')
order by proforma_invoice_date
I have a set of rows, each with a date value, and I need to select rows that fall within a specific date range. How can I do this?
select * from table where convert(int,date_created) between //what should go here?
I want to select between '20-10-2010' and '22-10-2010'.
It keeps complaining about string to date conversion.
You need to use yyyymmdd which is the safest format for SQL Server
select * from table
where date_created BETWEEN '20101020' and '20101022'
Not sure why you had CONVERT to int there...
Note: if date_created has a time component that this fails because it assume midnight.
Edit:
To filter for the day 20 Oct 2010 to 22 Oct 2010 inclusive, if the date_created column has times, without applying a function to date_created:
where date_created >= '20101020' and date_created < '20101023'
Either don't convert the date_created to an int or use integers for your data values
I would leave the date_created as a date.
select * from table
where date_created between '20101020' and '20101022'