SQL幾種常用的函數


函數的種類:
  • 算數函數(數值計算的函數)
  • 字符串函數(字符串操作的函數)
  • 日期函數(用來進行日期操作的函數)
  • 轉換函數(用來轉換數據類型和值的函數)
  • 聚合函數(用來進行數據聚合的函數)

算數函數(+-*/):

1.ABS()——函數(計算絕對值)

select m,abs(m) as abs_col from SampleMath;

2.mod()——求余函數

-- 計算n/p的余數(SQL Server不支持此函數)
select n,p, mod(n,p) as mod_col from SampleMath;

3.round()——四舍五入函數,round(對象數值,保留的小數的位數)

-- 對m列的數值進行n列位數的四舍五入處理。
select m, n , round(m,n) as round_col from SampleMath;

字符串函數

1.|| ——字符串拼接(此函數無法在mysql及sql server中運行)

select str1, str2, str1||str2 as str_concat from SampleStr;

2.length(字符串)——字符傳長度函數(確認字符串中包含多少個字符)

select str1, length(str1) as len_str from SampleStr;

3.lower(字符串)——小寫轉換,只能針對英文字母使用

select str1, lower(str1) as low_str 
  from SampleStr
where str1 in ('ABC', 'aBC', 'abc', '山田');

4.replace(對象字符串,替換前的字符串,替換后的字符串)——將字符串替換成其他字符串

-- 如果str1包含str2,則將str1中的str2替換成str3
select str1, str2, str3, replace(str1, str2, str3) as rep_str
from SampleStr;

5.substring函數——字符串截取(是適用於postgresql 和 mysql)

-- substring(對象字符串 from 截取的起始位置 for 截取的字符串)
-- 截取出str1中的第三第四個字符串
select str1, substring(str1 from 3 for 2) as sub_str from SampleStr;

6.upper(字符串)——小寫轉大寫

SELECT str1,UPPER(str1) AS up_str
FROM SampleStr
WHERE str1 IN ('ABC', 'aBC', 'abc', '山田');

日期函數

1.CURRENT_DATE ——當前日期(返回當前日期)

-- mysql寫法
select CURRENT_DATE;

-- oracle寫法
select CURRENT_DATE from dual;

2.CURRENT_TIME ——當前時間

select CURRENT_TIME;

3.CURRENT_TIMESTAMP ——當前日期和時間

select CURRENT_TIMESTAMP;

4.EXTRACT(unit FROM date)——截取日期元素,截取出日期的一部分(年/月/日小時/秒等)

SELECT CURRENT_TIMESTAMP,
			 EXTRACT(YEAR   FROM CURRENT_TIMESTAMP) AS year,
			 EXTRACT(MONTH  FROM CURRENT_TIMESTAMP) AS month,
			 EXTRACT(DAY    FROM CURRENT_TIMESTAMP) AS day,
			 EXTRACT(HOUR   FROM CURRENT_TIMESTAMP) AS hour,
			 EXTRACT(MINUTE FROM CURRENT_TIMESTAMP) AS minute,
			 EXTRACT(SECOND FROM CURRENT_TIMESTAMP) AS second;	

轉換函數

1.CAST()——類型轉換

-- CASE(轉換前的值 AS 轉換后的數據類型)
select CAST('00001' AS SIGNED INTEGER) AS int_col;

2.將字符轉換為日期類型

select cast('2018-11-12' AS DATE) AS sate_col;

3.COALESC()E——將null轉換為其他值
COALESCE(參數1,參數2,參數3.....)參數個數時可變的,可以無限個。

--SQL Server, PostgreSQL, MySQL
SELECT COALESCE(NULL, 1) AS col_1,
	   COALESCE(NULL, 'test', NULL) AS col_2,
	   COALESCE(NULL, NULL, '2009-11-01') AS col_3;
	   
-- 將字段str2中的null值轉成字符串為null的值。
select COALESCE(str2,'null') from samplestr;

(expression_1, expression_2, ...,expression_n)依次參考各參數表達式,遇到非null值即停止並返回該值。如果所有的表達式都是空值,最終將返回一個空值。使用COALESCE在於大部分包含空值的表達式最終將返回空值。

建表語句

 -- DDL:創建表
CREATE TABLE SampleMath
(m  NUMERIC (10,3),
 n  INTEGER,
 p  INTEGER);

-- DML:插入數據
START TRANSACTION;

INSERT INTO SampleMath(m, n, p) VALUES (500,  0,    NULL);
INSERT INTO SampleMath(m, n, p) VALUES (-180, 0,    NULL);
INSERT INTO SampleMath(m, n, p) VALUES (NULL, NULL, NULL);
INSERT INTO SampleMath(m, n, p) VALUES (NULL, 7,    3);
INSERT INTO SampleMath(m, n, p) VALUES (NULL, 5,    2);
INSERT INTO SampleMath(m, n, p) VALUES (NULL, 4,    NULL);
INSERT INTO SampleMath(m, n, p) VALUES (8,    NULL, 3);
INSERT INTO SampleMath(m, n, p) VALUES (2.27, 1,    NULL);
INSERT INTO SampleMath(m, n, p) VALUES (5.555,2,    NULL);
INSERT INTO SampleMath(m, n, p) VALUES (NULL, 1,    NULL);
INSERT INTO SampleMath(m, n, p) VALUES (8.76, NULL, NULL);

COMMIT;

--MySQL
--DDL:創建表
CREATE TABLE SampleStr
(str1  VARCHAR(40),
 str2  VARCHAR(40),
 str3  VARCHAR(40));

--DML:插入數據
START TRANSACTION;

INSERT INTO SampleStr (str1, str2, str3) VALUES ('opx',	        'rt'	,	NULL);
INSERT INTO SampleStr (str1, str2, str3) VALUES ('abc'	,	'def'	,	NULL);
INSERT INTO SampleStr (str1, str2, str3) VALUES ('小明'	,	'小紅'  ,	'小李');
INSERT INTO SampleStr (str1, str2, str3) VALUES ('aaa'	,	NULL    ,	NULL);
INSERT INTO SampleStr (str1, str2, str3) VALUES (NULL	,	'xyz',	        NULL);
INSERT INTO SampleStr (str1, str2, str3) VALUES ('@!#$%',	NULL	,	NULL);
INSERT INTO SampleStr (str1, str2, str3) VALUES ('ABC'	,	NULL	,	NULL);
INSERT INTO SampleStr (str1, str2, str3) VALUES ('aBC'	,	NULL	,	NULL);
INSERT INTO SampleStr (str1, str2, str3) VALUES ('abc行',	'abc'	,	'ABC');
INSERT INTO SampleStr (str1, str2, str3) VALUES ('abcdefabc',   'abc'	,	'ABC');
INSERT INTO SampleStr (str1, str2, str3) VALUES ('micmic',	      'i',        'I');

COMMIT;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM