Table not modified since date - sql

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

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

Selecting 2 date sets from the same dataset?

Say I have a query that selects all the sales from the past 90 days. I want to be able to isolate certain rows on a case/when basis, and can't quite figure out how to do this. The case statement is depending on dates, so: If the date falls between 3/1 and 5/31, then I want to select the sales from any month ends (3/31, 4/30, 5/31 and TODAY) otherwise, if the date is not between 3/1 and 5/31, then I just want to select the past 3 month-ends.
What I tried so far is inserting a Case/When statement in the WHERE clause, but that doesn't seem kosher. Is there another way to go about this?
For reference, the #monthends table contains the following single column:
monthends
2019-03-31
2019-02-28
2019-01-31
and the #insideRule table contains similarly:
insiderRule
2019-03-31
2019-04-22
The query:
SELECT *
FROM mytable
WHERE asofdate IN
CASE WHEN asofdate BETWEEN '3-1-2019' AND '5-31-2019' THEN
(SELECT * FROM #insideRule)
ELSE
(SELECT * FROM #monthends)
END
When I execute the above, I get syntax errors around "IN"
You want exists not case expression :
IF EXISTS (SELECT 1 FROM mytable WHERE aasofdate BETWEEN '2019-03-01' AND '2019-05-31')
SELECT *
FROM #insideRule
ELSE
SELECT *
FROM #monthends
I am thinking you want something like this:
SELECT ir.*
FROM #insideRule ir
WHERE getdate() >= '2019-03-01' AND
getdate() < '2019-06-01'
UNION ALL
SELECT me.*
FROM #monthends me
WHERE getdate() < '2019-03-01' OR
getdate() >= '2019-06-01';
This assumes that the two tables have the same columns in the same order with compatible types.

Athena greater than condition in date column

I have the following query that I am trying to run on Athena.
SELECT observation_date, COUNT(*) AS count
FROM db.table_name
WHERE observation_date > '2017-12-31'
GROUP BY observation_date
However it is producing this error:
SYNTAX_ERROR: line 3:24: '>' cannot be applied to date, varchar(10)
This seems odd to me. Is there an error in my query or is Athena not able to handle greater than operators on date columns?
Thanks!
You need to use a cast to format the date correctly before making this comparison. Try the following:
SELECT observation_date, COUNT(*) AS count
FROM db.table_name
WHERE observation_date > CAST('2017-12-31' AS DATE)
GROUP BY observation_date
Check it out in Fiddler: SQL Fidle
UPDATE 17/07/2019
In order to reflect comments
SELECT observation_date, COUNT(*) AS count
FROM db.table_name
WHERE observation_date > DATE('2017-12-31')
GROUP BY observation_date
You can also use the date function which is a convenient alias for CAST(x AS date):
SELECT *
FROM date_data
WHERE trading_date >= DATE('2018-07-06');
select * from my_schema.my_table_name where date_column = cast('2017-03-29' as DATE) limit 5
I just want to add my little words here, if you have date column with ISO-8601 format, for example: 2022-08-02T01:46:46.963120Z then you can use parse_datetime function.
In my case, the query looks like this:
SELECT * FROM internal_alb_logs
WHERE elb_status_code >= 500 AND parse_datetime(time,'yyyy-MM-dd''T''HH:mm:ss.SSSSSS''Z') > parse_datetime('2022-08-01-23:00:00','yyyy-MM-dd-HH:mm:ss')
ORDER BY time DESC
See more other examples here: https://docs.aws.amazon.com/athena/latest/ug/application-load-balancer-logs.html#query-alb-logs-examples

Select records after specific date in SQL

I have a column in my database called "begin_date".
I am trying to select records where the begin_date are greater than a specific date. I put
select * from Table_Name
where begin_date >= '1/1/2014'
However, it returns error message "String to date conversion error".
I am not sure how to modify the query to make it work?
Thanks!
Try using ISO (8601) standard date formats:
select *
from Table_Name
where begin_date >= '2014-01-01';
select *
from table
where data >= '01/01/2014'

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'