Good Day
Trying to understand if it is possible to have a script with a condition of " todays date"
#original Values
start_date = '2021-01-01'
end_date = '2023-01-20'
I've tried these , none work
end_date = 'datetime.now'
end_date = 'datetime.date'
Error Received :
ValueError("time data 'datetime.date' does not match format '%Y-%m-%d'")
Related
I am running the following query and can't seem to figure out where the error is-
select case when month>=7 then substr(year,3,2)|| '/'||TO_NUMBER( substr(year,3,2))+1
else to_number(substr(year,3,2))-1 || '/'|| substr(year,3,2) end as fiscal_year
FROM ( SELECT DISTINCT to_Char(extract( year from date)) as year,
extract( month from date)as MONTH FROM TABLE )
I want to convert year to fiscal year like 19/20, 20/21 etc
The operator precedence rules means the string concatenation is happening before the addition; this expression:
substr(year,3,2)|| '/'||TO_NUMBER( substr(year,3,2))+1
is evaluated as
substr(year,3,2)|| '/'||TO_NUMBER( substr(year,3,2))
and then it tries to add 1 to that string result. Hence the error you get.
You can add parentheses to make it add 1 to the year number before then concatenating that:
substr(year,3,2)|| '/'|| (TO_NUMBER( substr(year,3,2))+1)
You could also do this without so much string manipulation:
select case when extract (month from your_date) >= 7 then
to_char(your_date, 'YY') || '/' || to_char(add_months(your_date, 12), 'YY')
else
to_char(add_months(your_date, -12), 'YY') || '/' || to_char(your_date, 'YY')
end as fiscal_year
FROM (
SELECT DISTINCT trunc(your_date, 'MM') as your_date
FROM your_table
)
db<>fiddle
and there are other options, of course.
I have two date fields start_date and end_date (in a format like 2021-02-25 10:24:44), I want to use this format only and want that end_date must be equal or greater than start_date, after_or_equal is not working in my case.
I am using
'start_date'=> 'nullable|date_format:Y-m-d H:i:s',
'end_date'=> 'nullable|date_format:Y-m-d H:i:s|after_or_equal:start_date',
But, even if I am giving a end_date bigger than start_date I am getting this message
The end date must be a date after or equal to start time.
The inputs were
"start_date" : "2021-01-01 10:30:50",
"end_date" : "2021-01-01 11:31:55",
Please help me, what I am doing wrong.
I have this query:
SELECT CREAD, DATE, NOTIFICATION,
NVL (ROUND ((((:endate - :stdate) * 24) - CASE
WHEN MAX(CREAD) = MIN(CREAD) THEN MAX(CREAD)
ELSE MAX(CREAD) - MIN(CREAD)
END) / COUNT(NOTIFICATION),2),0) "MTTR"
FROM DUAL;
When user select dates between start_date and end_date then execute this table:
CREAD DATE NOTIFICATION MTTR
123 1/1/2017 6 56
1000 30/1/2017 3 80
When user select no dates between start_date and end_date then execute this table:
CREAD DATE NOTIFICATION MTTR
123 1/1/2017 6 0
1000 30/1/2017 3 0
IN "MTTR" column value is 0. but i want same value in the above table.
How to write query in oracle sql?
When user select no dates between start_date and end_date then ... "MTTR" column value is 0.
When the user selects no date the first part of the expression (:endate - :stdate) evaluates to (null - null), hence the rest of the expression evaluates to null and therefore your query returns the NVL() default of 0.
but i want same value in the above table.
To get this outcome you need to handle null values for start_date and end_date. Maybe this will work for you:
nvl(:endate -:stdate, 1)
Or you may want some other default. But as the value of MTTR depends on the range of start_date and end_date it's hard to see how you can get "the same value" when the user enters no bounds.
I have problems with comparing the day of week
Here is my query in oracle database.
SELECT DAY, WEEKDAY
FROM (SELECT v.TNI, v.FRMP, v.LR, v.DAY, v.HH, v.VOLUME,
CASE WHEN hd.HOLIDAY_DATE is not null then 'HOLIDAY'
ELSE to_char(v.DAY, 'Day') END AS WEEKDAY
FROM v_nem_rm16 v
LEFT JOIN DBP_ADMIN.DBP_HOLIDAY hd
ON v.DAY = hd.HOLIDAY_DATE
WHERE v.STATEMENT_TYPE != 'FORECAST')
WHERE WEEKDAY = 'Monday';
Basically, the "WEEKDAY" column has the day of week based on the "DAY". Day is just date like 13/Mar/17
If you look at the "CASE" statement, you will notice that the "WEEKDAY" column is filled by "to_char(v.DAY, 'Day')".
So the column has values like "Sunday, Saturday and so on, the day of week".
The problem is the outer query's where clause, "WHERE WEEKDAY = 'Monday'"
When I execute this query, It does not give me any rows even if I have rows having Monday as the value in "WEEKDAY" column
But when I change the WHERE clause to "WHERE WEEKDAY = to_char(sysdate, 'Day')", It works fine. The sql statement gives me the rows having "Saturday" in "WEEKDAY" column since the "to_char(sysdate, 'Day')" gives me "Saturday".
So what is problems with my first query??
I just want to filter rows by the name of the day of week like if i pass "Monday", i want to have all the rows having " Monday" in "WEEKDAY" column.
How can I do??
THANKS GUYS
It appears TO_CHAR(, 'Day') returns a fixed length string with right side padding of blanks. So, you are not getting 'Monday', you are really getting 'Monday '. The longest day is 'Wednesday', so it is a 9 character string. So, either trim the value or compare against 'Monday ' exactly.
select '|'||to_char(sysdate+level,'Day') ||'|' days_of_the_week
from dual
connect by level <= 7;
Results...
|Saturday |
|Sunday |
|Monday |
|Tuesday |
|Wednesday|
|Thursday |
|Friday |
if you would like to use the day names (either full or abbreviated), I suggest to specify the language in the to_char function as follows:
select '|'||to_char(sysdate+level, 'fmDay', 'NLS_DATE_LANGUAGE = American') ||'|' days_of_the_week_enu,
'|'||to_char(sysdate+level, 'fmDay', 'NLS_DATE_LANGUAGE = German') ||'|' days_of_the_week_ger,
'|'||to_char(sysdate+level, 'DY', 'NLS_DATE_LANGUAGE = American') ||'|' days_of_the_week_abbr_enu,
'|'||to_char(sysdate+level, 'DY', 'NLS_DATE_LANGUAGE = German') ||'|' days_of_the_week_abbr_ger
from dual
connect by level <= 7;
This way the returned day names will be independent of session locale settings.
Result:
I am querying from database:
select * from database where id = 12345
and i get a couple of days where it is equal to
3/4/2010 9:16:59 AM
but if i add
and date = to_date('03/04/2010','DD/MM/YYYY')
giving me
select * from database where id = 12345
and date = to_date('03/04/2010','DD/MM/YYYY')
I comeback with completely nothing.
Any pointers?
btw, I know that there is a time on there, but I don't know how to compare just based on the day!!!
That's because when you are creating the date, you implicitly set the time to 0:00:00, and as 0:00:00 is not equal to 9:16:59, you'll not get the date returned.
To tell Oracle to ignore the time part, just do the following:
WHERE id = 12345
AND trunc(date) = to_date('03/04/2010', 'DD/MM/YYYY')
WHERE id = 12345
AND date >= TO_DATE('03/04/2010', 'DD/MM/YYYY')
AND date < TO_DATE('03/04/2010', 'DD/MM/YYYY') + INTERVAL '1' DAY
http://use-the-index-luke.com/sql/where-clause/obfuscation/dates