This question already has answers here:
SQL string comparison, greater than and less than operators
(4 answers)
Closed 5 years ago.
In my Oracle db, I noticed some strange behavior when doing a <= comparison on two numbers. And I found the problem. The .net code was passing in one of the values as a DbParameter of type string. And the Oracle column type of the other number was a varchar. So essentially what was happening was a <= on two numbers that were actually varchars.
Can someone explain why the following statement is true in PLSQL?
'10000001' <= '50000'
Becuase-
SQL> select ascii('10000001') from dual;
ASCII('10000001')
-----------------
49
SQL> select ascii('50000') from dual;
ASCII('50000')
--------------
53
SQL> select 'true' from dual where ascii('10000001') < ascii('50000');
'TRU
----
true
Related
This question already has answers here:
How to show the zero in 0.24 [duplicate]
(2 answers)
Fetching value from a number column removes 0 before decimal
(1 answer)
Closed 5 months ago.
For exmaple:
select 'A='||(0.22*0.11)||'' as A from dual;
it does return A=.0242
expected A=0.0242enter code here
If you use to_char function with desired format mask, then you get
SQL> select 'A='|| to_char(0.22*0.11, 'fm999G990D0000')||'' as A from dual;
A
---------------
A=0,0242
SQL>
As of a "generic" format model: you can't "dynamically" set it, but - if you use 9 instead of 0 after decimal point, you might get what you wanted:
SQL> select 'A='|| to_char(88.223*99.112, 'fm999G990D9999999999')||'' as A from dual;
A
---------------------
A=8.743,957976
SQL>
Benefit: it returns result you want
Drawback: how many 9s will you have to put in there? You can't tell - set it to the "worst" case you expect (such as 10 of them in my example)
How can I get the Year to Date (YTD) count of a certain data using Oracle query?
Assume that we are interested in summing the total number of vouchers filed since the beginning of the current year.
This is the query I came up with
WITH cteDAYSCOUNT AS (SELECT TO_NUMBER(TO_CHAR(SYSDATE, 'DDD'))
FROM dual)
SELECT COUNT(VOUCHER_FILED_DATE), SYSDATE AS "AS OF" FROM CLAIMS
WHERE VOUCHER_FILED_DATE > sysdate - cteDAYSCOUNT;
This part of it returns the number of days since the beginning of the year
WITH cteDAYSCOUNT AS (SELECT TO_NUMBER(TO_CHAR(SYSDATE, 'DDD')) FROM dual)
And this part attempts to use the sysdate - {number of days} to calculate the count
SELECT COUNT(VOUCHER_FILED_DATE), SYSDATE AS "AS OF"
FROM CONTINUED_CLAIMS WHERE VOUCHER_FILED_DATE > sysdate - cteDAYSCOUNT;
But the problem is the although cteDAYSCOUNT holds the number of days since the year starts, it is not being recognized as a number, so it's throws an error
Is there a better query for calculating YTD count or a fix of the above query?
I'm not sure I'm following you.
Query you posted is incomplete; CTE lacks in column name, while FROM clause misses join with the CTE. Therefore, your query can't work at all.
If it is fixed, then:
SQL> WITH
2 ctedayscount (ctedayscount) AS
3 -- this is yours
4 (SELECT TO_NUMBER (TO_CHAR (SYSDATE, 'DDD')) FROM DUAL),
5 claims (voucher_filed_date) AS
6 -- this is my CTE, so that query would actually return something
7 (SELECT DATE '2021-01-15' FROM DUAL)
8 -- final SELECT; you're missing JOIN with CTEDAYSCOUNT
9 SELECT COUNT (voucher_filed_date), SYSDATE AS "AS OF"
10 FROM claims CROSS JOIN ctedayscount
11 WHERE voucher_filed_date > SYSDATE - ctedayscount;
COUNT(VOUCHER_FILED_DATE) AS OF
------------------------- ----------
1 09.02.2021
SQL>
So, it works.
Furthermore, you said:
But the problem is the although cteDAYSCOUNT holds the number of days since the year starts, it is not being recognized as a number, so it's throws an error
How do you know it isn't a NUMBER? Which error is it? It is difficult to debug an unknown error. Could it, perhaps, be that CLAIMS table's voucher_filed_date datatype is something different than DATE (such as VARCHAR2) and contains data which Oracle can't implicitly convert to DATE so WHERE clause (my line #11) fails?
Or is the main problem the fact that you just missed to join CTEDAYSCOUNT with CLAIMS (which I already mentioned)?
This question already has answers here:
How do I limit the number of rows returned by an Oracle query after ordering?
(14 answers)
Closed 4 years ago.
I have to limit to 2 rows.
But can't do it for a SQL-Fetch.
select *
from employee;
You can use something like this:
select *
from
( select *
from emp
order by data desc )
where ROWNUM <= 2;
You can change the query as:
select *
from
top_n_test
order by
num
fetch first 3 rows only;
The select first n rows only selects the first n rows.
Well, the simplest way is to
select *
from employee
where rownum <= 2;
but the question is what exactly do you want to do with that.
This question already has answers here:
Working with dates in Oracle SQL
(3 answers)
Closed 6 years ago.
I am very new to using PL/SQL and I have created a procedure but I cannot figure out the correct syntax in order to get the current month and year's data and another cursor to get the data from exactly 1 year prior:
create or replace procedure data(acc integer, month integer, year integer)
as
Cursor c1 is
select usage
from bill
where account =acc_num and to_char(BILL_DATE, 'MM-YYYY') = 'month-year';
Cursor c3 is
select usage
from bill
where account =acc_num and
to_char(BILL_DATE, 'MM-YYYY') = 'month-year' - 1;
** I do understand this is only part of the code, but I believe my logic is almost complete for finding the data I want. Using PLSQL
I think you are looking for something like this:
select usage
from bill
where account = in_account and
extract(year from bill_date) = in_year and
extract(month from bill_date) = in_month;
If you want to compare the year and month (which are passed in as integers), just extract those attributes from the date.
If you are learning PL/SQL, learn to name your parameters and arguments so you can distinguish them from columns:
create or replace procedure data (
in_account integer,
in_month integer,
in_year integer
) as
begin
. . .
(And "data" is a very curious name for a stored procedure. I would expect a verb in the name.)
I have a cursor which selects date from column with NUMBER type containg floating point numbers. Numbers like 4,3433 are returned properly while numbers smaller then 1 have removed leading zero.
For example number 0,4513 is returned as ,4513.
When I execute select used in the cursor on the database, numbers are formatted properly, with leading zeros.
This is how I loop over the recors returned by the cursor:
FOR c_data IN cursor_name(p_date) LOOP
...
END LOOP;
Any ideas why it works that way?
Thank you in advance.
You're confusing number format and number value.
The two strings 0.123 and .123, when read as a number, are mathematically equals. They represent the same number. In Oracle the true number representation is never displayed directly, we always convert a number to a character to display it, either implicitly or explicitly with a function.
You assume that a number between 0 and 1 should be represented with a leading 0, but this is not true by default, it depends on how you ask this number to be displayed. If you don't want unexpected outcome, you have to be explicit when displaying numbers/dates, for example:
to_char(your_number, '9990.99');
It's the default number formatting that Oracle provides.
If you want to specify something custom, you shall use TO_CHAR function (either in SQL query or PL/SQL code inside the loop).
Here is how it works:
SQL>
SQL> WITH aa AS (
2 select 1.3232 NUM from dual UNION ALL
3 select 1.3232 NUM from dual UNION ALL
4 select 332.323 NUM from dual UNION ALL
5 select 0.3232 NUM from dual
6 )
7 select NUM, to_char(NUM, 'FM999990D9999999') FORMATTED from aa
8 /
NUM FORMATTED
---------- ---------------
1.3232 1.3232
1.3232 1.3232
332.323 332.323
.3232 0.3232
SQL>
In this example, 'FM' - suppresses extra blanks, '0' indicates number digit including leading/trailing zeros, and '9' indicates digit suppressing leading/trailing zeros.
You can find many examples here:
http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm#i34570