Validating date has expired with current system date - sql

I am trying to compare the database stored date value with the current system date. The date format is (YYYY-MM-DD). and the sql date i written compare is as follows.
select id from table1 where tabel1.date > current_date
And i want to get result if the date has been expired or not.
Please find me the correct sql.
Thanks in advance

Well, when tabel1.date > current_date the date stored in your database is in the future.
Try
select id from table1 where tabel1.date < CURDATE();

Related

Oracle sql developer date/month data extract query and syntax

I Want to extract data from a table for monthwise in oracle sql.
I tried it as follow,
SELECT * From customer ct
Where ct. Application_signed _date in (Date '31-12-2020, Date '01-10-2020')
But i am unable to Fetch the data for this range, whereas data is available for this months.
The correct format for a date in Oracle -- and almost everywhere else -- is YYYY-MM-DD:
select *
From customer ct
where ct.Application_signed_date in (Date '2020-12-31, Date '2020-10-01')
Note that Oracle dates can have time components. If that is possible in your data, then one fix is:
where trunc(ct.Application_signed_date) in (Date '2020-12-31, Date '2020-10-01')

SQL Server 2008 query check date on same day

I've written a query to check whether more than one record exists on the same day. Currently the excerpt from my query that performs the restriction looks like this :
GROUP BY
entry_date
HAVING
COUNT(entry_date) > 1
As the entry date column is defined as a datetime, does it check against the full datetime or just the date?
Thanks.
if entry_date is DATETIME ,your group by wont work as expected.you need to CAST it to DATE.Cast(Datetime)to date is sargable as well.
GROUP BY cast(entry_date as DATE)
having count(cast(entry_date as DATE)) > 1
Since you don't cast or convert it to anything else, naturally it uses all data available. So it would group data together with the exact same datetime. Why would you expect anything else?
It should be full datetime.
CONVERT(date, entry_date)
should separate out the date.

Records between start and end date

I am having two textfields which represents startDate and endDate.Now the problem I am facing is that I want all the records from the database which occured between this interval.But the field which stores date is TimeStamp and is of format :
24-APR-14 09.23.44.458000 PM or we can say dd-mm-yy hh.min.ss.milli AM/PM
Now obviously user entering the date is not going to enter it in such a format.So what should be query to select records from table say t1 between this date interval.
Saving Date data as Date or Datetime makes life easy.
You have tagged Mysql in the question so here is a Mysql Solution.
This is what you can do
select * from
test
where
date_format(str_to_date(`date`,'%d-%b-%y'),'%Y-%m-%d')
between '2014-04-15' AND '2014-04-24'
DEMO
You can format the user input as you want in the query in DATE_FORMAT().
User input will have a startdate and a enddate may be in datetime OR date variable
while trying to use these two input variables in the WHERE CLAUSE use BETWEEN CAST(#StartDate AS DATE) AND (CAST(#EndDate AS DATE) + 1)
This will fetch results that lies in both dates and the data range as well

how to delete the records which is inserted 1 day ago

I dont have proper timestamp in table; is it possible to delete 1 day old logs even now?
I have a column name as SESSION_IN which is basically a VARCHAR datatype, and the value will be like
2013-10-15 02:10:27.883;1591537355
is there any way to trim the number after ; and is it possible to compare with "sysdate" identifier?
This SP should compare all the session IDs with current datetime and it should delete if it is older then 1 day.
You can igonre time part and convert date into required format somthing like this
SYSDATE - to_date('date_col','YYYY-DD-MM')
then you can perform operations.
Use the Substring function to extract the datetime portion from the record, then use convert to datetime to cast it to datetime, and then finally use datediff to check if it was inserted yesterday. Use all these caluses in a
DELETE FROM table
WHERE ___ query
For Oracle you could use something like this:
SELECT
TRUNC(to_timestamp(SUBSTR('2013-10-15 02:10:27.883;1591537355',1,
(
SELECT
instr('2013-10-15 02:10:27.883;1591537355', ';')-1
FROM
dual
)
), 'YYYY-MM-DD HH:MI:SS.FF'))
FROM
dual;
Which gives you just the date portion of your input string. Just subtract the amount of days you want to log at the end.
Hope following query helps you:
Select Convert(Datetime,Substring('2013-10-15 02:10:27.883;1591537355',1,23)), DateDiff(dd,Convert(Datetime,Substring('2013-10-15 02:10:27.883;1591537355',1,23)),Getdate())

SQL query using oracle 10g database datetime equal not working

I have SQL query like this.
select *
from TABLE_A
where LogDateTime >= startdatetime and LogDateTime <= enddatetime;
But some reason enddatetime equal is not working. I have the record with the date 11/23/09 8:50:09. When I select enddatetime as 11/23/09 8:50:09 it's not returning this record. It's returning till 8.49:59. What could be the problem? Why the timestamp is not working? Please let me know.
Thank you..
Oracle might store the datetime in higher precision, like 8:49:59.200. That's bigger than 8:49:59, but it will display the same.
Try this WHERE clause:
LogDateTime < (enddatetime + INTERVAL '1' SECOND)
This will still include anything which has the same starting second as the enddatetime.
What datatype is enddatetime? If it's a timestamp then there might be a mismatch between the type of the variable you are passing in (DateTime) and the type of the data in the table (Timestamp) this could cause this as there might not be a timestamp valeue that exactly matches the datetime value ... and the closest available value might be "off" in the direction that causes the record to be filtered out.
Is LogDateTime of TIMESTAMP datatype? It stores fractional part of seconds. Possibly your date is not exactly 11/23/09 8:50:09.
Try to output your date using TO_CHAR(LogDateTime,'MM/DD/YYYY HH24:MI:SS.FF') to see if that's the case.