query two specific dates in postgresql - sql

I have a table as below. I want to query specific dates from the table. The dates are random and therefore i do not want to use the BETWEEN keyword. Also, the number of dates that i want to query could vary from one to many(for simplicity lets say 2 distinct dates.
create table temptable(id serial primary key not null,myTimestamp timestamp);
insert into temptable(myTimestamp) values ('2020-09-25 02:02:51.99');
insert into temptable(myTimestamp) values ('2020-08-24 12:20:51.111');
insert into temptable(myTimestamp) values ('2020-09-23 13:20:51.286');
The following query is executed hoping to get two distinct dates.
select *
from temptable
where myTimestamp::date = date '2020-09-23'
and myTimestamp::date = date '2020-08-24';
The above query executes on pgadmin but nothing is listed on the table. If i use OR operator, i can see one date returned but that i not what i want. I want both the dates
Please advice
Thanks

One method uses in:
select *
from temptable
where myTimestamp::date in (date '2020-09-23', date '2020-08-24');
However, this might be more efficient with inequalities for the date comparisons:
select *
from temptable
where (myTimestamp >= '2020-09-23'::timestamp and
myTimestamp < '2020-09-24'::timestamp
) or
(myTimestamp >= '2020-08-24'::timestamp and
myTimestamp < '2020-08-25'::timestamp
)
This is more index friendly.
Or if you prefer:
select *
from temptable tt join
(values ('2020-09-23'::timestamp), ('2020-08-24'::timestamp)
) v(ts)
on myTimestamp >= v.ts and
myTimestamp < v.ts + interval '1 day';

So, as it has been already commented, all you need is to replace and with or
select *
from temptable
where myTimestamp::date = date '2020-09-23'
or myTimestamp::date = date '2020-08-24';
Here is a demo:
DEMO

Related

Extracting data from only the year

I have data in a table in SQL with dates, but how do I select only those that happen in 2021. (The dates look like 31-oct-2020) in the table. The dates are the actual date variable, not just text.
You should avoid storing your dates as text, but rather should use a proper date column. That being said, you may check the right 4 characters of the date string:
SELECT *
FROM yourTable
WHERE RIGHT(date_col, 4) = '2021';
If the column be an actual date type then use:
SELECT *
FROM yourTable
WHERE date_col >= '2021-01-01' AND date_col < '2022-01-01';
I suspect that your DB is Oracle after checking out your previous post. Then you can use
SELECT *
FROM yourTable
WHERE EXTRACT(year FROM dt) = 2021
or
SELECT *
FROM yourTable
WHERE TRUNC(dt,'YYYY') = date'2021-01-01'
or
SELECT *
FROM yourTable
WHERE dt BETWEEN date'2021-01-01' AND date'2021-12-31'
You can benefit the index if there's one on the date column(namely dt) by using the last SELECT statement
If using MSSQL, you can leverage the YEAR(...) to extract the year from a date.
Replace and , with the table name and date column name respectively.
select * from <tablename> where year(<datecolumn>) = 2021

Table not modified since date

I have a table with following fields.
Part ID, Quantity, Last modified date
I am using the following simple query to retrieve data, how can I add more criteria to show that quantity in hand 0 and part did not modified since 01/01/2015 till now.
SELECT *
FROM table001
where quantity_on_hand=0
Just add another filter in your WHERE clause like below:
SELECT *
FROM table001
WHERE quantity_on_hand=0
AND ModificationDate <= '20150101' -- Date format = 'YYYYMMDD'
I think this will do what you're looking for
SELECT * FROM table001 WHERE quantity_on_hand = 0 AND last_modified_date <= '2015-01-01'
If you are looking to combine multiple conditions you could use AND,OR operators after WHERE clause.
Here I think you might be looking for:
SELECT
*
FROM
table001
WHERE
qunatity_on_hand=0 AND last_modified_date <= '2015-01-01';

Query for dates which are not present in a table

Consider a table ABC which has a column of date type.
How can we get all the dates of a range (between start date and end date) which are not present in the table.
This can be done in PLSQL.I am searching a SQL query for it.
You need to generate the arbitrary list of dates that you want to check for:
http://hashfactor.wordpress.com/2009/04/08/sql-generating-series-of-numbers-in-oracle/
e.g.:
-- generate 1..20
SELECT ROWNUM N FROM dual
CONNECT BY LEVEL <= 20
Then left join with your table, or use a where not exists subquery (which will likely be faster) to fetch the dates amongst those you've generated that contains no matching record.
Assuming that your table's dates do not include a time element (ie. they are effectively recorded as at midnight), try:
select check_date
from (select :start_date + level - 1 check_date
from dual
connect by level <= 1 + :end_date - :start_date) d
where not exists
(select null from mytable where mydate = check_date)
Given a date column in order to do this you need to generate a list of all possible dates between the start and end date and then remove those dates that already exist. As Mark has already suggested the obvious way to generate the list of all dates is to use a hierarchical query. You can also do this without knowing the dates in advance though.
with the_dates as (
select date_col
from my_table
)
, date_range as (
select max(date_col) as maxdate, min(date_col) as mindate
from the_dates
)
select mindate + level
from date_range
connect by level <= maxdate - mindate
minus
select date_col
from the_dates
;
Here's a SQL Fiddle
The point of the second layer of the CTE is to have a "table" that has all the information you need but is only one row so that the hierarchical query will work correctly.

List rows after specific date

I have a column in my database called "dob" of type datetime. How do I select all the rows after a specific DoB in SQL Server 2005?
Simply put:
SELECT *
FROM TABLE_NAME
WHERE
dob > '1/21/2012'
Where 1/21/2012 is the date and you want all data, including that date.
SELECT *
FROM TABLE_NAME
WHERE
dob BETWEEN '1/21/2012' AND '2/22/2012'
Use a between if you're selecting time between two dates
Let's say you want to get all records from a table called Table_One with a datetime column called date_value that have happened in the past six months...
CREATE TABLE (
date_value DATETIME
)
SELCECT *
FROM Table_One
WHERE date_value > DATEADD(month, -6, getdate());
This gives a bit more dynamic of a solution.

SQLserver query rows in between specific date

how could i get row by filtering in between specific date...
in my table, i have thousand of rows about Ordered Food.
I want to check ordered food between June-01-2011 1:00:00PM to June-05-2011 6:00:00pm.
how could i suppose to write t-sql code...
select *
from MyTable
where MyDate >= `2011-06-01T13:00:00` and MyDate < '2011-06-05T18:00:00'
if you want to include both datetimes:
select *
from MyTable
where MyDate between `2011-06-01T13:00:00` and '2011-06-05T18:00:00'
Making some wild assumption about your table structure basically do this...
Select * from ordered_food
where ordered_date
between '2011-06-01 13:00:00' and '2011-06-05 18:00:00'