PLSQL Date Filter Error - oracle

I have a table with data since 2012. I need to get data in this table from 31 days back from given date. so i wrote below query to get data.
select ('[' || work_date || '] ' || field_name || ' - ' ||work_desc) d
from DAILY_WORK
where TO_CHAR(work_date,'DD/MM/YYYY') >= TO_CHAR(to_date('30-Jan-13','dd-MON-yyyy') - (31),'DD/MM/YYYY')
order by work_date desc
When i execute this query, it returns data in below dates only.
31-AUG-12
31-OCT-12
30-DEC-12
31-DEC-12
But actually i need to get data from 2012-12-30 to 2013-01-30.
How could i do this ?

use this:
select ('[' || to_char(work_date, 'dd-MON-yyyy') || '] ' || field_name || ' - ' ||work_desc) d
from DAILY_WORK
where work_date >= to_date('30-Jan-2013','dd-MON-yyyy') - 31
order by work_date desc

You can use the below query:
select * from DAILY_WORK where work_date >= TRUNC(to_date('01-30-2013','MM/DD/YYYY')) - 31
Check date format which tool you are using As per tool i am using date format is 'MM/DD/YYYY'
This query give correct result.You can check.

Related

In Oracle execution time for finding a data inside comma seperated column taking too long

I am trying to find out if the list of data is in comma separated column. I have a code select * from my_table where (regexp_substr ( listcolumn, '[^,]+', 1, level ) in ('a','d')) connect by level <= regexp_count(listcolumn, ',') + 1; and table with a lot data. The problem is it works for small data table but it is taking too much time to execute for a lot a table with a lot of data. I am not expert in database. so can you please help to how to resolve this problem. Thank you in advance.
Don't split the string, look for a sub-string match (with the surrounding delimiters so that you match entire terms):
SELECT *
FROM my_table
WHERE ',' || listcolumn || ',' LIKE '%,' || 'a' || ',%'
OR ',' || listcolumn || ',' LIKE '%,' || 'd' || ',%';

Display all the rides for which the travel duration is more than 2 hours

Sample output:
Rise_ID FROM_LOCATION TO_LOCATION SEATS_LEFT
SEATS_TOTAL RIDE_PROVIDER START_ON ENDS _ON IS_STARTED IS_FINISHED 12014 Nandi hills banglore 0 3 11002 13-Nov-20
04.00.13.36 PM. 13-Dec-20 08.02.13.36PM yes yes
one way you can get the difference between the 2 date columns as follows.
If the following SQL SYSDATE is getting the current time, the other column is a hard coded date time column.
SELECT ROUND(minutes_ / 60, 2) || ' Hours '
FROM ( SELECT (sysdate - to_date('14/09/2022 19:35:00', 'DD/MM/YYYY HH24:MI:SS')) * 24 * 60 AS minutes_
FROM dual
);
SELECT FROM_LOCATION || ' - ' || TO_LOCATION
FROM TABLE_OUTPUT T
WHERE (TO_DATE(START_ON, 'DD-MON-YY HH.MI.SS') - TO_DATE(ENDS_ON, 'DD-MON-YY HH.MI.SS')) > 2;
I assuming you are looking this data from a table, also you need to check the format, the current you're using not gonna work, i suggest the format 'DD-MON-YY HH.MI.SS'

Query to search an entire DB for a string

I've been tasked to do a bit of data discovery work. I'm working with our in house application, and I need to determine the first few tables that get hit when we do specific actions in the application. I have no intimate knowledge with the DB other than the fact that there are 1000 different tables I need to account for.
During my search, I came across this link https://lalitkumarb.wordpress.com/2015/01/06/sql-to-search-for-a-value-in-all-columns-of-all-atbles-in-an-entire-schema/
Which is exactly what I need, but when I run it it's not returning any data. I've confirmed that it's not working as intended by going and grabbing some data that I KNOW is in the DB and searching for that.
Here's the query I'm running
SELECT DISTINCT SUBSTR (:val, 1, 11) "Searchword",
SUBSTR (table_name, 1, 14) "Table",
SUBSTR (column_name, 1, 14) "Column"
FROM cols,
TABLE (xmlsequence (dbms_xmlgen.getxmltype ('select '
|| column_name
|| ' from '
|| table_name
|| ' where upper('
|| column_name
|| ') like upper(''%'
|| :val
|| '%'')' ).extract ('ROWSET/ROW/*') ) )
ORDER BY "Table"
/
When I execute this query, TOAD pops up with a Variables screen in which I specify the type and value of :val. I hit OK, the query executes and returns nothing.
Am I missing something?

Oracle date time to datetime

I've looked at the answers on here but none of them seem to work.
I have the following date and time columns with example times how they are stored as below:
DATE_V TIME_V
26-NOV-15 10:58
How do I add these together into one column and convert it to a datetime as below? The trailing zeros are not necessary.
DateTime_V
2015-11-26 10:58:00.000
I’ve used the following which saves it as string but I can't get it to datetime.
TO_CHAR(DATE_V, 'YYYY-MM-DD') || ' ' || TO_CHAR(TO_timestamp(time_V, 'HH24:MI'),'HH24:MI')
= 2015-11-26 10:58
Assuming (bad word) that these are both stored as VARCHAR2 fields the following should work:
SELECT DATE_V, TIME_V, TO_DATE(DATE_V || ' ' || TIME_V, 'DD-MON-RR HH24:MI') AS DATETIME_V
FROM YOURTABLE
SQLFiddle here
Best of luck.

sysdate difference ( how to make query between sysdate and my table date)

I am new to oracle.
I have some problems to make query.
Im trying to make query that solves the difference between sysdate() and the date from my own table
select to_char(to_char(sysdate,'YYYYMMDDHH24MISS')
- to_char(S_DATE||S_HOUR||':00' , 'YYYYMMDDHH24MISS'))
from dual;
I'm doing like that.
In my table, I have two columns 'S_DATE' and 'S_HOUR' that means time.
So, I would like to know the time difference and how to make this query.
Thank you in advance.
You can get difference between two dates by simple - (minus) operator. However first you need to convert date string to date using TO_DATE.
Sample in SQL Fidlle is here
The sample query:
select
sysdate , S_DATE || ' ' || S_HOUR "Date",
round((sysdate - to_date(s_date || s_hour ,' YYYY/MM/DDHH24:MI') ) * 24 * 60, 2) "Dif In Min",
round((sysdate - to_date(s_date || s_hour ,' YYYY/MM/DDHH24:MI') ) * 24 * 60 * 60, 2) "Dif In Sec"
from myDate

Resources