DQL: Data Query Language
Contains at least two parts:
1: SELECT clause
2: FROM clause
After SELECT, specify the field to be queried, you can
Use "*" to query all fields, or specify individually
A concrete field, or a function, an expression.
Specify the table to query after FROM
SELECT ename,job,sal,deptno
FROM emp
View the result of a function or expression
SELECT ename,sal*12
FROM emp
String functions
1:CONCAT(char1,char2)
connection string
SELECT CONCAT(ename,sal)
FROM emp
SELECT
CONCAT(CONCAT(ename,':'),sal)
FROM emp
Strings can be concatenated using the form "||"
SELECT
ename || ':' || sal
FROM
emp
LENGTH(char) function
Count the length of the specified string
The statistical length for CHAR is fixed, usually
Statistics on VARCHAR2 type fields.
SELECT ename, LENGTH(ename)
FROM emp
UPPER(),LOWER(),INITCAP()
Convert string to all uppercase, all lowercase
and initial capitalization
dual: pseudo-table, when the content of the query is different from the actual
When any table is not related, you can use
Pseudo table to populate the FROM clause, the pseudo table will only query
make a record
SELECT
UPPER('helloworld'),
LOWER('HELLOWORLD'),
INITCAP('hello world')
FROM dual
TRIM,LTRIM,RTRIM
Remove specified characters from both sides of a string
SELECT
TRIM('e' FROM 'eeeeeliteeee')
FROM dual
SELECT
LTRIM('etstesteliteseseses','set')
FROM dual
LPAD,RPAD
complement function
SELECT
RPAD(sal,5,' ')
FROM
emp
SUBSTR(str,m,n)
Intercept the current string, from the specified m
Position starts to truncate n characters in a row.
n can not be specified, if not specified, it will be intercepted to
End of string, if n exceeds the string
When the maximum value that can be intercepted, it is also intercepted to
end of string.
Note: The database subscript starts from 1! ! ! !
SELECT
SUBSTR('thinking in java',1,5)
FROM
dual
INSTR(char1,char2,n,m)
See the position of char2 in char1
n: can not be specified, the default value is 1 if not specified
Indicates how many characters to search from
m: can not be specified, the default value is 1
Indicates the first occurrence
SELECT
INSTR('thinking in java','in',
4,2)
FROM
dual
Numeric function:
ROUND(n,m)
Round up to m decimal places of n.
If m is not specified or is 0, it means reserved to integer
For, negative numbers are numbers with more than ten digits.
SELECT ROUND(45.678, 2) FROM DUAL
SELECT ROUND(45.678, 0) FROM DUAL
SELECT ROUND(45.678, -1) FROM DUAL
TRUNC()
The parameter is consistent with ROUND, the number is intercepted, and no rounding is performed.
input operation
SELECT TRUNC(45.678, 2) FROM DUAL
SELECT TRUNC(45.678, 0) FROM DUAL
SELECT TRUNC(45.678, -1) FROM DUAL
MOD(m,n)
find remainder
SELECT
ename, sal, MOD (sal, 1000)
FROM emp;
CEIL(n),FLOOR(n)
Round up and down
SELECT CEIL(45.678) FROM dual
SELECT FLOOR(45.678) FROM dual
date related functions
SYSDATE, SYSTIMESTAMP
These two keywords denote two inner functions
Returns the current system time, but
One returns as DATE, the other returns as timestamp
Type returned.
SELECT SYSDATE FROM dual
SELECT SYSTIMESTAMP FROM dual
TO_DATE function
A string can be formatted according to the specified date
resolves to a value of type DATE
Date format string appears except English, number, symbol
Use double quotation marks for any other characters.
SELECT
TO_DATE('1992-08-05 22:12:44',
'YYYY"年"MM"月"DD"日" HH24:MI:SS' )
FROM
dual
Dates can be compared in size with calculations
The later the bigger
Subtract two dates, the difference is the difference in days
Adding or subtracting a number to a date is equal to adding or subtracting the specified number of days
SELECT SYSDATE+1 FROM dual
See how many days each employee has been hired to date?
SELECT ename,SYSDATE-hiredate
FROM emp
Check how many days you have lived till today?
SELECT
SYSDATE-TO_DATE('1992-08-02','YYYY-MM-DD')
FROM
dual
View employees who joined after 1982?
SELECT ename, hireate
FROM emp
WHERE hiredate>TO_DATE('1982-01-01','YYYY-MM-DD')
TO_CHAR()
Can convert the given date to the specified date
format to string
SELECT
TO_CHAR(SYSDATE,'YYYY-MM-DD HH24:MI:SS')
FROM
dual
RR is also represented by 2 digits in date format
, but unlike YY, it can be
Determine the century when parsing the year of the string
SELECT
TO_CHAR(
TO_DATE('49-08-08','RR-MM-DD'),
'YYYY-MM-DD'
)
FROM
dual
LAST_DAY(date)
Returns the end of the month for the given date
View the end of the month in which each employee was hired?
SELECT ename,LAST_DAY(hiredate)
FROM emp
ADD_MONTHS(date,i)
Add the specified month to the specified date
If i is negative, it is subtracted.
View each employee's 30th anniversary?
SELECT
ename,ADD_MONTHS(hiredate,12*30)
FROM
emp
MONTHS_BETWEEN(date1,date2)
Calculate the difference in months between two dates
See how many months each employee has been employed so far?
SELECT
ename,MONTHS_BETWEEN(SYSDATE,hiredate)
FROM
emp
NEXT_DAY(date,i)
Returns within one week from the next day of the given date
day of the week
SELECT NEXT_DAY(SYSDATE,4)
FROM dual
LEAST、GREATEST
To find the minimum and maximum values, more than one parameter is sufficient.
Commonly used with date, number evaluation
SELECT
LEAST(SYSDATE,
TO_DATE('2008-08-05',
'YYYY-MM-DD')
)
FROM DUAL;
EXTRACT()
Extract the specified time component of a date
corresponding value
Employees who joined in 1982?
SELECT ename, hireate
FROM emp
WHERE EXTRACT(YEAR FROM hiredate)=1982
NULL value operation
CREATE TABLE student
(id NUMBER(4), name CHAR(20), gender CHAR(1));
INSERT INTO student VALUES(1000, 'Li Mochou', 'F');
INSERT INTO student VALUES(1001, 'Lin Pingzhi', NULL);
INSERT INTO student(id, name) VALUES(1002, 'Zhang Wuji');
UPDATE student
SET gender = NULL
WHERE id=1000
SELECT * FROM student
delete records whose gender is NULL
To determine whether the value of a field is empty, use
IS NULL或IS NOT NULL
Cannot use "=" to judge NULL
DELETE FROM student
WHERE gender IS NOT NULL
CREATE TABLE student
(id NUMBER(4),
name CHAR(20),
gender CHAR(1) NOT NULL);
NULL concatenated with string equals nothing
NULL and number operation demerits are still NULL
Check the income of each employee:
SELECT
ename,sal,comm,sal+comm
FROM emp
Null function:
NVL(f1,f2)
When f1 is NULL, the function returns f2,
If not NULL, the function returns f1 itself.
So the role of the NVL function is to convert the NULL value
Replace with a non-NULL value.
Check the income of each employee:
SELECT
ename,sal,comm,sal+NVL(comm,0)
FROM emp
Check everyone's bonus situation, there are bonuses
It will be displayed as "with bonus", if it is NULL, it will be displayed
for "no bonus"
NVL2(f1,f2,f3)
When f1 is not NULL, the function returns f2
When f1 is NULL, the function returns f3
SELECT
ename,sal,comm,
NVL2(comm, 'with bonus', 'without bonus')
FROM
emp
SELECT
ename,NVL2(comm,sal+comm,sal)
FROM
emp