Functions in Oracle (Conversion, Character)

Introduction to Conversion Functions


The conversion function is used to convert the data type from one data type to another. In some cases, the data type of the value allowed by the oracle server is different from the actual one. At this time, the oracle server will implicitly convert the data type

such as
create table t1 (id int);
insert into t1 values('10') --> this way oracle will automatically '10'-->10

create table t2(id varchar2(10));
insert into t2 values(1); - ->In this way, oracle will automatically convert 1-->'1';

what we want to say is that although oracle can perform implicit data type conversion, it is not suitable for all situations, in order to improve the reliability of the program , we should use conversion function to convert

*************to_char()****************
you can use select ename,hiredate,sal from emp where deptno=10; display information, however, in some cases, this does not meet your needs.


? Whether the date can display hours/minutes/seconds
SQL> select ename,to_char(hiredate,'yyyy/mm/dd hh24:mi:ss') from emp;

Note:
If the data is not added in the format of hh24:mi:ss To add, the system will default to 0, that is, hh24-->00:00:00 hh12-->12:00:00 is displayed when querying


? Whether the salary can display the specified currency symbol
SQL> select ename,to_char(sal,'L99999.99') salary from kkkk;
SQL> select ename,to_char(sal,'$99,999.99') salary from kkkk;
//The currency symbol is displayed after the salary
SQL> select ename, to_char(sal,'99,999.99L') Salary from kkkk;

Among them: L in to_char represents the currency type, 99999.99 represents the format to be displayed is five integers and two decimals
The reason for this design is: in the table The type of the sal column is
SAL NUMBER(7,2) Y   
where 7 represents a total of 7 digits including integers and decimals, 2 represents two decimals


yy: two-digit year 2004-->04
yyyy: four digits year 2004
mm: 2 digit month august --> 08
dd: 2 digit day 30 --> 30
hh24: 8 o'clock --> 20
hh12: 8 o'clock --> 08
mi, ss -- -> Display minutes\seconds

9: Display numbers, ignoring the preceding 0
0: Display numbers, if the number of digits is insufficient, fill in with 0
.: Display a decimal point
at the specified position,: Display a comma at the specified position
$: Before the number Add the dollar currency symbol
L: Add the local currency symbol before the
number C: Add the international currency symbol before the number
G: Display the group separator,
D: Display a decimal point symbol (.) at the specified position

to_char

? Show all employees who joined in 1980
SQL> select * from emp where to_char(hiredate,'yyyy')=1980;

? Display all employees who joined in December
SQL> select * from emp where to_char(hiredate,'mm')=12;


to_date
function to_date is used to convert string to date type data

? Can I add the date in the way Chinese people are accustomed to year-month-day
SQL> insert into kkkk values(3243,'HELEN','ANALYST',7902,to_date('1994-5-2','yyyy-mm-dd '),900,30,20);

Introduction to Character Functions


The character function is the most commonly used function in oracle, there are
1.lower(char): convert the string to lowercase format.
2.upper(char): Convert the substring to uppercase format.
3.length(char): Returns the length of the string.
4.substr(char,m,n): Take the substring of the string. It means starting from the
mth character and taking n characters String, followed by the specified string 6.instr(char1,char2,[,n[,m]]): take the position of the substring in the string ? Display the names of all employees in lowercase SQL> select lower(ename) from emp; SQL> select lower(ename),sal from emp; ? Display the names of all employees in uppercase SQL> select upper(ename) from emp; SQL> select upper(ename),sal from emp; ? Display the name of the employee with exactly 5 characters SQL> select * from emp where length(ename)=5; ? Display the first three characters of all employees' names

















SQL> select substr(ename,1,3) from emp;
The meaning of the SQL statement is to intercept 3 characters from the first character of the ename attribute

? Display the names of all employees in

uppercase Step

1. Complete the first letter in uppercase
select upper(substr(ename,1,1)) from emp;
2. Complete the latter letter in lowercase
select lower(substr(ename,2,length( ename)-1)) from emp;
3. Combine together
SQL> select upper(substr(ename,1,1))||lower(substr(ename,2,length(ename))) from emp;

? Display the names of all employees in lowercase

steps

1. Complete the first letter lowercase
select lower(substr(ename,1,1)) from emp;
2. Complete the latter letter uppercase
select upper(substr(ename,2,length( ename)-1)) from emp;
3. Merge together
select lower(substr(ename,1,1))||upper(substr(ename,2,length(ename)-1)) from emp;


? Display the names of all employees, replace all "A" with "I am A"
SQL> select replace(ename,'A','I am a tiger'
SQL> select replace(ename,'SMIT','替换') from emp;

Guess you like

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