how to select only by date from timestamp column in postgres? - sql

I have a table.
as you can see created_date column is timestamp field. now on selection i want to consider only date value. for e.g if i want to make selection of rows from today i want to do something like:-
select * from audit_logs where created_date = '2018-11-28';
the above query returns null. is it possible to make selection this way ?

You can use date() function
select * from audit_logs where date(created_date) = '2018-11-28'

An alternative function is date_trunc(text, timestamp), if you want more flexibility (truncate to hour, for example). Check out the postgres documentation (which is great) for all the date/time functions: https://www.postgresql.org/docs/current/functions-datetime.html
You do get a timestamp back instead of date, mind.

Related

SELECT matching dates from a timestamp with time zone column

I have a timestamp with time zone column within which I'd like to run a query returning all matching dates. eg. I want all rows which have a timestamp with date 2019-09-30. I'm trying something like this but haven't been able to figure it out:
SELECT * FROM table WHERE
x='1277' AND
date='2019-09-30 21:40:01.316240 +00:00'::DATE;
There are two options:
range search:
WHERE timestampcol >= TIMESTAMPTZ '2019-09-30'
AND timestampcol < (TIMESTAMPTZ '2019-09-30' + INTERVAL '1 day')
The proper index to make this fast is
CREATE INDEX ON atable (timestampcol);
conversion to date:
WHERE CAST(timestampcol AS date) = '2019-09-30'
The proper index to make this fast is
CREATE INDEX ON atable ((CAST(timestampcol AS date)));
Both methods work equally well. The second method has a shorter WHERE clause, but a specialized index that maybe no other query can benefit from.
You can use such a collation among your date converted column value and fixed date value :
with tab( x, date ) as
(
select 1277, '2019-09-30 21:40:01.316240 +00:00'::timestamp with time zone union all
select 1278, '2019-09-29 21:40:01.316240 +00:00'::timestamp with time zone
)
select *
from tab
where date::date = date'2019-09-30';
Demo
I am not familiar with postgresql, but the following page https://www.postgresql.org/docs/9.1/datatype-datetime.html indicates that there is an optional precision value p that is used to specify the number of fractional digits retained in the seconds field. How was the data type configured for your date field? Is the timestamp column being stored as an eight byte integer or a floating-point number? If the later, the effective limit of precision might be less than 6. All of this info is on the above linked page.
You have several options, as they have been mentioned, and may vary depending on the data type of your field named "date".
For example,
Turn your field to date, returning format 'yyyy-mm-dd':
SELECT * FROM table WHERE x='1277' AND date::DATE='2019-09-30';
"date" just after field name.
Convert it to char and retrieve 10 characters:
SELECT * FROM table WHERE x='1277' AND LEFT(date::varchar,10)='2019-09-30';
Like the previous:
SELECT * FROM table WHERE x='1277' AND to_char(date,'yyyymmdd')='20190930';
And there are many others. For more specific info, you have to check PostgreSQL documentation to check which one is best for you or post more information in order we can guess more about your problem.
https://www.postgresql.org/docs/9.4/functions-datetime.html

SQL query doesnt bring out results when queried using date

I see that a table has the data value as 18-May-2012. But when I query looking for the same date using the below query, no results are available.
Select Submit_Dt From Siebel.S_Order_Dtl
where submit_dt = '18-May-2012'
Could you help me sort this issue?
You need to convert string date into date with TO_DATE() function.
Also you need to take into account that your date might contain hours/minutes/seconds. In order to handle this you need to truncate submit_dt column.
In your case it would look like this:
Select Submit_Dt From Siebel.S_Order_Dtl
where TRUNC(submit_dt) = TO_DATE('18-May-2012','dd-MON-yyyy')
Try to convert the date to date format using to_date as below
Select Submit_Dt From Siebel.S_Order_Dtl
where submit_dt = to_date('18-May-2012','DD-MON-YYYY')

Sort Date stored in SQLite database

I'm using a SQLite database (not mine) and I have to sort some data, order by date.
I have done many searches on the web but no solutions found!
The type of the field with the dates is "Date", with the format: DD/MM/YYYY HH:mm:SS
I tried many functions like strftime, datetime, date, ... But I can't sort the date correctly.
For example, if I do something like this:
SELECT * FROM events ORDER BY start_date
The date 04/10/2013 00:00:00 is after 03/12/2013... You know what I mean ?
I really don't know how I can sort in this format.
Thanks for your help
UPDATE 1:
I tried something like this:
SELECT start_date FROM events ORDER BY strftime('%d/%m/%Y %H:%M:%S', start_date)
Is this the right way to do ?
Results: No error, but nothing has changed. The dates are not sorted.
UPDATE 2:
Solution by laalto:
select * from tab order by substr(col,7,4)||'-'||substr(col,4,2)||'-'||substr(col,1,2)||'T'||substr(col,12);
The best way is to make the data sortable at insertion phase e.g. by inserting ISO-8601 stamps or unixtime integers, but since it's not your database, let's assume you can't influence that.
You can use substr() to extract parts from the datetime string and construct e.g. a ISO-8601 datetime string that you can sort lexicographically, resulting in chronological order.
Example:
sqlite> create table tab(col);
sqlite> insert into tab values('04/10/2013 00:00:00'),('03/12/2013 00:00:00');
sqlite> select substr(col,7,4)||'-'||substr(col,4,2)||'-'||substr(col,1,2)||'T'||substr(col,12) as date from tab order by date;
2013-10-04T00:00:00
2013-12-03T00:00:00
sqlite> select * from tab order by substr(col,7,4)||'-'||substr(col,4,2)||'-'||substr(col,1,2)||'T'||substr(col,12);
04/10/2013 00:00:00
03/12/2013 00:00:00

Select where date without time

I am using SQLite, now how can I Select a query if I have a timestamp column that has this value for example 07:00 06/03/13 but I want only to select where timestamp 06/03/13.
This is my example of my query..
select
timestamp, pname
from
attendance
where
timestamp between `06/01/13` and `06/10/13`
Use date
select timestamp, pname
from attendance
where date(timestamp) = '2013-03-06'
When filtering on a timestamp column, you want this logic.
where YourField >= TheStartOfYourRange
and YourField < TheDayAfterTheEndOfYourRange
This is equivalent to the two answers that use the date() function, but will generally perform faster. Using functions in the where clause usually slows down production, especially on indexed fields.
SQLite support function date, which can be used to get the date part of a timestamp. For example:
SELECT * FROM mytable WHERE date(timestamp_col) = '2013-05-20'
See SQLite date and time functions documentation for more info.

How to get update record based on given record in oracle?

I want to get record from DB based on given date value. I used Timestamp as coulmn type. I used query as
SELECT *
FROM table1
WHERE LAST_UPDT_S = to_date('23-AUG-12')
Getting empty table. where as records were present in the table.
Please, sort out me from this issue.
Thanks in advance.
May be your table contains dates with time part so try to use:
SELECT *
FROM table1
WHERE Trunc(LAST_UPDT_S) = to_date('23-AUG-12')
Try this,
SELECT *
FROM table1
WHERE LAST_UPDT_S = to_date('23-AUG-12', 'DD-MON-YY')
SEE: TO_DATE Manual
Generally it's better to use a time range - this opens the possibility of using an index if one exists:
SELECT *
FROM table1
WHERE LAST_UPDT_S >= DATE '2012-08-23'
AND LAST_UPDT_S < DATE '2012-08-24'
(Also, it's better to not use TO_DATE without specifying the date format, otherwise it will use whatever the current session's NLS setting happens to be.)