Functions in Oracle (Date, Math, System)

Date Functions Introduction
Date functions are used to process data of type date .
By default the date format is dd-mon-yy i.e. December-July-86


1) sysdate: This function returns the system time

2) add_months(d,n) adds n months to the date of d
For example: add_months(hiredate,8) means 8 months after joining the

company 3)last_day(d): return The last day of the month in which the specified date d is located


? Find employees who have been employed for more than 8 months
SQL> select * from emp where sysdate > add_months(hiredate,8);

? Find the name and date of employment of an employee who has completed 10 years of service
SQL> select ename,hiredate from emp where sysdate >= add_months(hiredate,12*10);

? For each employee, display the number of days they joined the company
SQL> select ename name,trunc(sysdate-hiredate) days of employment from emp;

? Find all employees hired on the third to last day of each month
SQL> select hiredate,ename from emp where last_day(hiredate)-2=hiredate;

Dual table can be used in oracle test

 


Introduction to
Mathematical Functions The data types of input parameters and return values ​​of mathematical functions are both numeric types.
Mathematical functions include cos, cosh, exp, ln, log, sin, sinh, sqrt, tan, tanh, acos, asin, atan, round

commonly used:
1.round(n,[m])
n stands for rounding Numerical value, m means rounding to several decimal points, if not, rounding to integer
SQL> select round(sal,1) from kkkk where ename='kkkk';

2.trunc(n,[m])
n means to Intercepted, m is to intercept a few decimal places, and no rounding is used. If there is no m, no decimal is required. If m is a negative number, it will be intercepted from the integer.
SQL> select trunc(comm,1),comm from kkkk where ename ='kkkk1';

3.mod(m,n): modulo


4.floor(n): Returns the largest integer less than or equal to n


5. ceil(n): Returns the largest integer greater than or equal to n.

The processing of numbers is most used in financial systems or banking systems. Different processing methods have different results on financial statements.


Introduction of specific functions
1.round(n,[m]): This function is used for rounding. If m is omitted, the rounding will be rounded to an integer; if m is a positive number, it will be rounded to m digits after the decimal point. If m is negative, round to m digits before the decimal point.

2.trunc(n,[m]): This function is used to truncate numbers. If m is omitted, the fractional part will be truncated. If m is a positive number, it will be truncated to m digits of the decimal point. If m is a negative number, it will be truncated to The first m digits of the decimal point

3.mod(m,n): modulo
SQL> select mod(10,3) from dual;
 MOD(10,3)
----------
         1

4.floor(n ) : return the largest integer less than or equal to n
SQL> select floor(comm),comm from kkkk where ename='kkkk2';
FLOOR(COMM) COMM
----------- ----- ----
         65 65.34

5.ceil(n): returns the largest integer greater than or equal to n
SQL> select ceil(comm),comm from kkkk where ename='kkkk2';
CEIL(COMM) COMM
--------- ---------
        66 65.34


Case data: 2345.56 45.94
? Displays the daily salaries of all employees in a 30-day month, ignoring the remainder.
SQL> select trunc(sal/30), ename from emp;
or
SQL> select floor(sal/30), ename from emp;


other mathematical functions
abs(n): return the absolute value
of n acos(n): return Arc cosine of a number
asin(n): Returns the arc sine of a number
atan(n): Returns the arc tangent of a number
cos(n): Finds the cosine of
n exp(n): Returns e raised to the nth power
log( m,n): returns the logarithmic value
power(m,n): returns m raised to the power of n

 

Introduction to System Functions

This is the result of displaying the system properties of each data,
that is, how many data there are and how many rows will be displayed.

sys_context

1) terminal: the identifier of the terminal corresponding to the current session client
2) language: language
3) db_name: current database
4) nls_date_format: the date format corresponding to the current session client
SQL>alter session set nls_date_format = 'yyyy-mm-dd'; change the default format of the date
5) session_user: the database user name corresponding to the current session client
6) current_schem a: The default scheme name corresponding to the current session client
7) host: Returns the name of the host where the database is located.
Through this function, you can query some important information, such as which database are you using?
select sys_context('userenv','db_name') from dual;

where the first element 'userenv' in sys_context() is a fixed
userev: the name of the user environment

scheme is the same as the name of the user.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327031662&siteId=291194637