SQLserver query rows in between specific date - sql-server-2005

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'

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

query two specific dates in postgresql

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

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';

Oracle query not returning expected records

If I execute the query
select * from my_table where status=5000
I get 76 records from the database. However, I would like to narrow this down by date. Luckily there is a receiveddate column in the table and one of the records has a receiveddate of "23-APR-13". So I tried this query:
select *
from my_table
where status=5000
and receiveddate=to_date('23-APR-13', 'DD-Mon-YY')
and 0 records are returned.
If I use
select *
from my_table
where status=5000
and receiveddate > to_date('20130423', 'YYYYMMDD')
and receiveddate > to_date('20130424', 'YYYYMMDD')
I get exactly what I want. Can you guys and gals help me understand the difference/problem?
try
select *
from my_table
where status=5000
and trunc(receiveddate)=to_date('23-APR-13', 'DD-Mon-YY')
trunc will return the date part of the long date which includes time also.

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.