I'm trying to compare dates and it looks like I'm doing it like examples online, but I only see the rows of dates returned when I do >= for the comparison. When I just use = it doesn't return anything.
This is my query with >= for the date comparison:
select *
from valhist_tbl --dmh
where
CRTUPDDT >= to_date('28-10-21','DD-MM-YY')
and metricvalue <> 'Device Found'
order by CRTUPDDT asc
I see a lot of this sort of thing returned:
CRTUPDDT METRICVALUE
28-OCT-21 12.00.00.069000000 AM NOT FOUND
28-OCT-21 12.00.00.071000000 AM NOT FOUND
...
I want it to do this: CRTUPDDT = to_date('28-10-21','DD-MM-YY')
but that doesn't return any rows.
I think the comparison as equals and not greater than has to do with the what is returned, but I'm not sure how to fix it. Also, I don't want to see duplicate lines returned, but there's so many that distinct makes it take forever.
This is a link I was looking at: date
Compare on a range over the entire day:
SELECT *
FROM valhist_tbl --dmh
WHERE CRTUPDDT >= DATE '2021-10-28'
AND CRTUPDDT < DATE '2021-10-28' + INTERVAL '1' DAY
AND metricvalue <> 'Device Found'
ORDER BY CRTUPDDT asc
You could also compare using TRUNC:
SELECT *
FROM valhist_tbl --dmh
WHERE TRUNC(CRTUPDDT) = DATE '2021-10-28'
AND metricvalue <> 'Device Found'
ORDER BY CRTUPDDT asc
However, if you have an index on CRTUPDDT then it would not be used in this latter query; you would need to have a function-based index on TRUNC(CRTUPDDT) instead.
Related
I'm trying to set up some monitoring in my SQL database.
select
gn.Goal_Name_
,gn.EventTimestamp as Timestamp
--,Max(EventTimestamp) as Timestamp
from(
select CASE when substr(GoalName,1,3)='MSD' then 'MSD' when substr(GoalName,1,5)='https' then 'https' else goalname END as Goal_Name_
,EventTimestamp
from CG.Goal as goal
)gn
group by 1,2
Produces a table with a structure like:
Goal_Name_
Timestamp
MSD
05.03.2021 11:05:20.162
Logout
18.01.2022 20:07:29.799
Login
23.01.2022 09:12:16.597
etc
etc
The problem i'm having is finding a way to count each distinct Goal Name for each day. Find the daily occurence really.
You are almost there. The only thing missing is converting your timestamp to date and count the number of rows.
select
gn.Goal_Name_
,CAST(gn.EventTimestamp AS DATE FORMAT 'YYYY/MM/DD') as eventDay
,Count(*) as GoalsCount
from(
select CASE when substr(GoalName,1,3)='MSD' then 'MSD' when
substr(GoalName,1,5)='https' then 'https' else goalname END as Goal_Name_
,EventTimestamp
from CG.Goal as goal
)gn
group by 1,2
This is my scenario. I have a table with FirstMaintenanceEventDate and some data repeating after certain days from FirstMaintenanceEventDate. What I need to find out through a SQL Server query is to get the nearest date of each row among them.
Ex: there is a data row FirstMaintenanceEventDate is last month and it will repeat after 40 days which is next month. Likewise there are a lot of events here. Some of them have FirstMaintenanceEventDate in the future. Out of all these items I need to get the nearest date for each row.
I could get the nearest date without considering repeating process.
This is my query
SELECT TOP 1 *
FROM FIS_MaintenanceEventInstance
WHERE VehicleName = '600-GUR'
AND FirstMaintenanceEventDate >= GETDATE()
ORDER BY FirstMaintenanceEventDate ASC
Need to update it to consider repeat events as I describe above. Probably something like this but this isn't correct.
SELECT TOP 1 *
FROM FIS_MaintenanceEventInstance
WHERE
VehicleName = '600-GUR'
AND FirstMaintenanceEventDate >= GETDATE()
AND CASE
WHEN FirstMaintenanceEventDate < GETDATE()
THEN (Getdate() + RecurringDays)
END
ORDER BY FirstMaintenanceEventDate ASC
Any suggestion would be appreciated.
NOTE: If you need more information please let me now.
EDITED
I have tried follow query as Jatin Patel suggested in his answer below.
SELECT TOP 1 *,
CASE WHEN FirstMaintenanceEventDate < GETDATE() THEN DateAdd(day,RecurringDays,FirstMaintenanceEventDate)
ELSE FirstMaintenanceEventDate END AS MaintenanceEventDate
FROM FIS_MaintenanceEventInstance
WHERE VehicleName ='600-GUR'
ORDER BY MaintenanceEventDate ASC
This is not working as expected. After calculate the repeat date (here it's MaintenanceEventDate) also should consider when get the nearest date. According to this query it is calculate repeated date (MaintenanceEventDate) if it is in past and return it without check with other dates in the table.
Try this,
SELECT TOP 1 *,
CASE WHEN FirstMaintenanceEventDate < GETDATE() THEN (Getdate()+RecurringDays) ELSE FirstMaintenanceEventDate END AS MaintenanceEventDate
FROM FIS_MaintenanceEventInstance
WHERE VehicleName ='600-GUR'
ORDER BY MaintenanceEventDate ASC
I have below information in table and want to retrive the count if difference between two dates is >= 1.
Id testdate exdate
1 20120502 20120501 --> This should included, because diff is 1
2 20120601 20120601 --> This should not included, because diff is 0
3 20120704 20120703 --> This should included, because diff is 1
4 20120803 20120802 --> This should included, because diff is 1
Based on the above data, my select count should return 3.
I am trying the following, but it's not giving any results:
select count(to_char(testdate,'YYYYMMDD')-to_char(exdate,'YYYYMMDD')) from test ;
select count(*)
from my_table
where testdate <> exdate
You really should convert those to a date data-type though... it saves a lot of problems in the long run.
Your query will give you results. It will return 4. It gives you results because as long as the result of testdate - exdate is not null it will return a value for that row.
However, as you're not using dates Oracle will most probably convert those to numbers, which won't help for date comparisons should you do that in the future.
20120901 - 20120831 = 70 -- not 1
Okay, from your comment:
Working with ,if i use down voteaccept select count(*) from test where
to_char((testdate,'YYYYMMDD') - to_char(exdate,'YYYYMMDD')) >= 1; .But
count is one of the column.how to retrive above select statement as
one of the column
you're trying something completely different.
Your dates are actually dates; it's helpful to post this. You're looking for an analytic function, specifically count().
select a.*, count(*) over ( partition by 1 ) as ct
from my_table a
where trunc(exdate) <> trunc(testdate)
Note the trunc function, which, without additional parameters will remove the time portion of the date this enabling a direct comparison without resorting to converting the date to a character.
select count(*)
from test
where to_date(testdate,'YYYYMMDD') - to_date(exdate,'YYYYMMDD') >= 1;
or
select count(*)
from test
where to_date(testdate,'YYYYMMDD') <> to_date(exdate,'YYYYMMDD');
Looking at testdate and exdate it looks more like the columns are VARCHAR type so you would require apropriate date conversion.
In Oracle if the type is date you can calculate with them. 1 equal 1 day. 1/24 equals 1 hour.
Your case is rather easy because you could even compare the strings.
SELECT count(*)
FROM test
WHERE testdate <> exdate
But it sounds like you want to be able to be variable, so you rather convert them to a date and then you can do
SELECT count(*)
FROM test
WHERE to_date(testdate,'YYYYMMDD')-to_date(exdate,'YYYYMMDD') >= 1
I am not sure what you want if testdate minus exdate is -1 or more because the exdate is after testdate. Then you can work with ABS
SELECT count(*)
FROM test
WHERE ABS(to_date(testdate,'YYYYMMDD')-to_date(exdate,'YYYYMMDD')) >= 1
I have a row from an SQL table, and one field is a DATE. (The value may be undefined, as DATE has no default value). How can I tell if this date, if it exists, is still in the future?
I searched for answers and found lots of ways to extract all records that pass this test, but I don't want to do that. I just want to check this one previously extracted record. Sorry for the newbie question!
$userQuery = "SELECT * FROM `passwords` WHERE `name` = '$name' LIMIT 1";
$userResult = mysql_query($userQuery);
$userRow = mysql_fetch_assoc($userResult);
$bestBeforeDate = $userRow['bestBeforeDate']; // field is in DATE format
// what now? How to find if the 'best Before Date' has passed?
You can do it in SQL as additional column
select case when (current_timestamp < date_column)
then 1
else 0
end as is_in_future
from your_table
EDIT
You can do this in one query while reading other values from your table.
Example (not knowing the column names of your table)
select id,
date_column,
other_column,
case when (current_timestamp < date_column)
then 1
else 0
end as is_in_future
from your_table
where some_conditions
Example output:
id date_column other_column is_in_future
1 2012-01-01 abc 0
2 2012-08-01 def 1
...
I have a bunch of tasks in a MySQL database, and one of the fields is "deadline date". Not every task has to have to a deadline date.
I'd like to use SQL to sort the tasks by deadline date, but put the ones without a deadline date in the back of the result set. As it is now, the null dates show up first, then the rest are sorted by deadline date earliest to latest.
Any ideas on how to do this with SQL alone? (I can do it with PHP if needed, but an SQL-only solution would be great.)
Thanks!
Here's a solution using only standard SQL, not ISNULL(). That function is not standard SQL, and may not work on other brands of RDBMS.
SELECT * FROM myTable
WHERE ...
ORDER BY CASE WHEN myDate IS NULL THEN 1 ELSE 0 END, myDate;
SELECT * FROM myTable
WHERE ...
ORDER BY ISNULL(myDate), myDate
SELECT foo, bar, due_date FROM tablename
ORDER BY CASE ISNULL(due_date, 0)
WHEN 0 THEN 1 ELSE 0 END, due_date
So you have 2 order by clauses. The first puts all non-nulls in front, then sorts by due date after that
The easiest way is using the minus operator with DESC.
SELECT * FROM request ORDER BY -date DESC
In MySQL, NULL values are considered lower in order than any non-NULL value, so sorting in ascending (ASC) order NULLs are listed first, and if descending (DESC) they are listed last.
When a - (minus) sign is added before the column name, NULL become -NULL.
Since -NULL == NULL, adding DESC make all the rows sort by date in ascending order followed by NULLs at last.