Oracle common functions

1. Mathematical functions

  ABS (x)  返回x的绝对值;
  BITAND(x,y)  返回对x,y进行位与( AND )操作的结果;
  CEIL(x)  返回大于或等于x的最小整数(注意负数);
  FLOOR(x)  返回小于或等于x的最大整数;
  EXP(x)  返回e的x次幂,其中e约为2.71828183;
  LN(x)  返回x的自然对数;
  LOG(x,y)  返回以x为底y的对数;
  POWER(x,y)  返回x的y次幂;
  SQRT(x)  返回x的平方根  ;
  MOD(x,y)  返回x除以y的余数;
  SIGN(x)  返回x的符号;
   ROUND(x[,y])    返回对x取整的结果。y为可选参数,说明对第几位小数处取整。没有指定y的时候 Then round to 0 decimal places of x; if it is negative, round x to the |y|th place to the left of the decimal point. This function is rounded to the nearest whole number
  TRUNC(x[,y])    与ROUND类似,之不过是直接舍去尾数
 

2. Aggregate function

  AVG(DISTINCT|ALL)  average;

  MAX(DISTINCT|ALL)  finds the maximum value;

  MIN(DISTINCT|ALL)  finds the minimum value;

  STDDEV(distinct|all)  finds the standard deviation;

  VARIANCE(DISTINCT|ALL)  finds covariance;  

  SUM(DISTINCT|ALL)  summation; 

  COUNT(DISTINCT|ALL)  finds the number of records and data;

  MEDIAN()   finds the median;

 

3. Date function

  SYSDATE   is used to get the current date of the system

  ADD_MONTHS  to add or subtract months

  LAST_DAY  returns the last day of the date

  MONTHS_BETWEEN(date2,date1)  gives the month of date2-date1

  NEW_TIME(date,'this','that')  gives the date and time in this timezone=other timezone    

     shorthand time zone                 

     AST OR ADT Atlantic Standard Time         
     HST OR HDT Alaska-Hawaii Time
     BST OR BDT British Summer Time
     MST OR MDT American Mountain Time
     CST OR CDT American Central Time Zone
     NST New World Standard Time
     EST OR EDT American Eastern Time
     PST OR PDT Pacific Standard Time
     GMT Glenn
     YST OR YDT Yukon Standard Time

  NEXT_DAY    returns the exact date of the number of Sundays you request, within the same week or a week after the specified date. 

    Sunday = 1 Monday = 2 Tuesday = 3 Wednesday = 4 Thursday = 5 Friday = 6 Saturday = 7  

  CURRENT_DATE  The current date in the current session time zone 

  CURRENT_TIMESTAMP  returns the current date in the current session time zone as timestamp with time zone data type

  DBTIMEZONE()    returns the time zone

  SESSIONTIMEZONE  returns the session time zone where DBTIMEZONE is for the database and session is for the current session, because the time zone can be changed at the session level

  EXTRACT   finds field values ​​for date or interval values 

  LOCALTIMESTAMP    returns the date and time in the session 

  TRUNC(for dates)  The TRUNC function truncates the date value for the specified element.

    Its specific syntax is as follows:
      TRUNC(date[,fmt])
      in:
      date a date value
      fmt Date format, the date will be truncated by the specified element format. Omit it to truncate by the most recent date
 
  SELECT TRUNC(SYSDATE,'YYYY') FROM DUAL; --Returns the first day of the current year.
 
  SELECT TRUNC(SYSDATE,'MM') FROM DUAL; --Returns the first day of the current month.
 
  SELECT TRUNC(SYSDATE,'D') FROM DUAL; --Returns the first day of the current week.
 
  SELECT TRUNC(SYSDATE,'DD') FROM DUAL;--return the current year, month and day
 
  example:

  ---- Last day of last month
  SELECT TO_CHAR(LAST_DAY(ADD_MONTHS(SYSDATE, -1)), 'YYYY/MM/DD') FROM DUAL;

  ----: Today of the last month
  SELECT TO_CHAR(ADD_MONTHS(SYSDATE, -1), 'YYYY-MM-DD') FROM DUAL;

  ---- First day of last month
  SELECT TO_CHAR(ADD_MONTHS(LAST_DAY(SYSDATE)+1,-2),'YYYY-MM-DD') FirstDay FROM DUAL;

  --- Count on a weekly basis
  SELECT TO_CHAR(SYSDATE, 'WW') FROM DUAL;

  --- Count monthly
  SELECT TO_CHAR(SYSDATE, 'MM') FROM DUAL;

  ---- Count on a quarterly basis
  SELECT TO_CHAR(SYSDATE, 'Q') FROM DUAL;

  --- Statistics by year
  SELECT TO_CHAR(SYSDATE, 'YYYY') FROM DUAL;


  Format date function:
    Q quarter, January to March is the first quarter 
    SELECT TO_CHAR(SYSDATE, 'Q') FROM DUAL; -- 2 represents the

    MM month number of  the second quarter
    SELECT TO_CHAR(SYSDATE, 'MM') FROM DUAL; --04 represents

    the Roman representation of the month of  April  RM
    SELECT TO_CHAR(SYSDATE, 'RM') FROM DUAL; --IV represents 

    the month name of April Month with a length of 9 characters 
    SELECT TO_CHAR(SYSDATE, 'MONTH') FROM DUAL ; -- Week of April 

    WW in the current year 
    SELECT TO_CHAR(SYSDATE, 'WW') FROM DUAL; -- 24 means June 13, 2002 is the 24th week 

    W Week of the month 
    SELECT TO_CHAR(SYSDATE, 'W' ) FROM DUAL; -- April 26, 2011 is the first day of the 

    year in the fourth week of DDD. January 1 is 001, February 1 is 032 
    SELECT TO_CHAR(SYSDATE, 'DDD') FROM DUAL;

    DD the current month days 
    SELECT TO_CHAR(SYSDATE, 'DD') FROM DUAL;

    D Day of the week 
    SELECT TO_CHAR(SYSDATE, 'D') FROM DUAL;

    DY Abbreviated day of the week 
    SELECT TO_CHAR(SYSDATE, 'DY') FROM DUAL;

    HH or HH12 Hour in decimal
    SELECT TO_CHAR(SYSDATE, 'HH') FROM DUAL;

    HH24 24-hour format 
    SELECT TO_CHAR(SYSDATE, 'HH24') FROM DUAL;     MI

    minutes (0~59) 
    SELECT TO_CHAR(SYSDATE, 'MI') FROM DUAL;
in minutes (minutes should use MI). MM is the format used for months, using it for minutes also works, but the result is wrong.

    SS seconds (0~59) 
    SELECT TO_CHAR(SYSDATE, 'SS') FROM DUAL;

 

4. Character function

  ASCII(str)  returns the ASCII value of the string

  CHR (x)   returns the ASCII character corresponding to the integer

  CONCAT(A,B)  concatenates string A and string B

    If you want to connect two fields in the table, you can use ||

  INITCAP  returns the string and capitalizes the first letter of the string;

  LENGTH  returns the length of the string;

    Returns the actual length of a certain column of data in the table, if the table has no data, returns 0

  LOWER  returns a string with all characters lowercase

  UPPER  returns the string with all characters in uppercase

  LTRIM(X,[TRIM_STRING])  deletes the string TRIM_STRING that appears to the left of X, the default is an empty string

  RTRIM(X, [TRIM_STRING])  deletes the string TRIM_STRING that appears on the right, the default is an empty string.

  SUBSTR(string, start, count)  take substrings, starting from start, take count

  INSTR(C1,C2,I,J)  searches for the specified character in a string and returns the position where the specified character is found;

    C1 The string to be searched
    C2 The string to be searched
    I The starting position of the search, the default is 1
    The position where J appears, the default is 1

  RPAD(str,X,str1)  pastes the character str1 to the right of str

    Note that the length value X is not the length of the pasted character, but the length of the entire string. If the length is less than the original string, the first X bits of the original string are taken.

  LPAD(str,X,str1)  pastes the character str1 to the left of str

    Note the same as above

  REPLACE('string','s1','s2')  replace the string s1 in the string with the string s2

  SOUNDEX  returns a string that sounds the same as the given string

  TRIM('s' from 'string')  trims characters and trims s from string

    LEADING cuts off the preceding characters
    TRAILING cuts off the following characters
    If not specified, the default is the space character 

  NVL(X, VALUE)  If X is a null value, return VALUE, otherwise return X

  NVL2(X, VALUE1, VALUE2)  If X is null, return VALUE1, otherwise return VALUE2

  NANVI(X, VALUE)  returns VALUE if X is not a number, otherwise returns X

 

5. Conversion function

  TO_CHAR(DATE,'FORMAT')  converts the corresponding data to string type

  TO_DATE(STRING,'FORMAT')  converts the string to a date in ORACLE

  TO_NUMBER(str)  converts the given character to a number

  RUNC(for number)  truncates a number according to the specified precision

  TRUNC(number[,decimals])  returns the processed value. Its working mechanism is very similar to that of the ROUND function, except that this function does not perform the corresponding rounding selection process before or after the specified decimal, but truncates it all.

    number The value to be truncated
    decimals indicates the number of digits after the decimal point to be retained. optional, omit it to truncate all fractional parts

  CHARTOROWID  converts a CHAR or VARCHAR2 value containing a ROWID in the external format to an internal binary format. The parameter string must be an 18-character string containing the ROWID in the external format.

  ROWIDTOCHAR  converts the ROWID data type to a character type, and converts a numeric rowid of type ROWID to its external 18-character string representation.

  CONVERT(c, dset, sset)  converts the source string sset from one language character set to another destination dset character set

  HEXTORAW(x)  converts a hexadecimal string x to raw

  RAWTOHEX  converts the RAW class value rawvalue to a corresponding string of hexadecimal representation. Each byte in the rawvalue is converted to a two-byte string. RAWTOHEX and HEXTORAW are two opposite functions.

  TO_MULTI_BYTE  Converts single-byte characters in a string to multi-byte characters, that is, converts the specified character to full-width and returns a char type string 

   DUMP(s,fmt,start,length)  The DUMP function returns a VARCHAR2 value in the internal numeric format specified by fmt

    fmt contains 4 formats: 8||10||16||17, which represent octal, decimal, hexadecimal and single character respectively. The default is decimal. The start parameter indicates the starting position, and the length indicates the length of the string number. 

  EMPTY_BLOB(), EMPTY_CLOB()  These two functions are functions used to initialize large data type fields

 

6. Control flow functions (commonly used in PL/SQL statements)

  Process Control:

    IF (expression) then statement executed end if; (end if: end IF) Judging that a condition is met

    IF (expression) then statement executed else statement executed end if; Judging the conditions that satisfy two conditions

    IF (expression) then statement to execute elseif statement to execute elseif statement to execute .... end if; to determine the conditions that satisfy multiple conditions

    Case (expression) when (conditional expression result 1) then execute statement 1 when (conditional expression result 2) then execute statement 2 else execute statement n end Caase; (end case ends Case)

    Case when (conditional expression 1) then execute statement 1 when (conditional expression 2) then execute statement 2 else execute statement n end case;

  Loop Control:

    LOOP statement segment; exit [when/if conditional expression] end Loop; execute at least once Equivalent to do{} while (conditional expression); when the when/if condition is met, use exit to exit, otherwise, end Loop;

    while conditional expression LOOP executes the statement end LOOP ; it will run only when the conditional expression is true;

    for loop variable IN [reverse] initial value expression ... final value expression LOOP execution statement END LOOP

 

PS: Due to the limited ability of the author, please forgive me if there are any mistakes;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325020886&siteId=291194637