I am a new learner to SQL,and I couldn't understand what the following queries do:
Select to_char(sal ,’$99,999’) salary from emp where ename =’SCOTT’ ;
Select to_date(’07-july-03’,’RM’) from dual ;
Please help.
CodePudding user response:
The best option you could have done would be to actually execute those queries and see what they return.
1st query: to_char
formats number (salary is a number) so that it is displayed in desired format. In your case, it adds dollar sign in front of it as well as the thousands separator.
SQL> select sal original_value,
2 to_char(sal, '$99,999') formatted_value
3 from emp
4 where ename = 'SCOTT';
ORIGINAL_VALUE FORMATTE
-------------- --------
3000 $3,000
2nd query: RM
format model returns Roman month (today is April, it is the 4th month in a year, its Roman representation is IV).
SQL> select sysdate,
2 to_char(sysdate, 'dd.mm.yyyy') formatted,
3 to_char(sysdate, 'RM') roman
4 from dual;
SYSDATE FORMATTED ROMA
--------- ---------- ----
03-APR-22 03.04.2022 IV
SQL>
Your 2nd query, on the other hand, seems to be invalid:
SQL> select to_date('03-apr-22', 'RM') your_query from dual;
select to_date('03-apr-22', 'RM') your_query from dual
*
ERROR at line 1:
ORA-00012: Message 12 not found; product=RDBMS; facility=ORA
; arguments: [12]
SQL>
More about format models in documentation.