Oracle's conversion functions

Convert a date to character

1. Grammar
 


 
Default format DD-MON-RR
YY YYYY YEAR (English year)
MM MONTH (English Month)
DD DAY
HH24 HH12
MI SS
2. Examples
  1. SQL>select to_char(sysdate,'YYYY-MM-DD HH24:MI:SS')from dual;
  2. TO_CHAR(SYSDATE,'YY
  3. -------------------
  4. 2017-07-02 11:40:14
  5. SQL> select to_char(sysdate,'YYYY-MM-DD HH12:MI:SS') from dual;
  6. TO_CHAR(SYSDATE,'YY
  7. -------------------
  8. 2017-07-0211:40:38
 
Convert two characters to date
1. Grammar
TO_DATE(char[,fmt[,params]])
params: the language used to make the date
Note: to_date() displays in the system default date format
2. Examples
  1. SQL>select to_date('2017-7-2','YYYY-MM-DD')from dual;
  2. TO_DATE('2017-
  3. --------------
  4. 02-7月 -17
 
Convert three numbers to characters
1. Grammar
TO_CHAR(number[,fmt])
9: Display the number and ignore the leading 0
0: Display the number, if the number of digits is insufficient, fill it with 0
.or D: Display decimal point
, or G: display thousands
$: dollar sign
S: plus plus or minus sign (both before and after)
2. Examples
  1. SQL>select to_date('2017-7-2','YYYY-MM-DD')from dual;
  2. TO_DATE('2017-
  3. --------------
  4. 02-7月 -17
  5. SQL> select to_char(12345.678,'$99,999.999') from dual;
  6. TO_CHAR(1234
  7. ------------
  8. $12,345.678
  9. SQL> select to_char(12345.678,'$99,999,999') from dual;
  10. TO_CHAR(1234
  11. ------------
  12. $12,346
  13. SQL> select to_char(12345.678,'S99,999.999') from dual;
  14. TO_CHAR(123
  15. -----------
  16. +12,345.678
 
Convert four characters to numbers
1. Grammar
TO_NUMBER(char[,fmt])
fmt is the converted format and can be omitted
2. Examples
  1. SQL>select to_number('$1,000','$9999')from dual;
  2. TO_NUMBER('$1,000','$9999')
  3. ---------------------------
  4. 1000

Guess you like

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