SQL語法比較
出自ProgWiki
目錄 |
字串
| 功能 | Oracle | MS-SQL Server | IBM-DB2 | Postgre SQL | MySQL | MS-Access | ANSI-SQL |
|---|---|---|---|---|---|---|---|
| 轉英文大寫 | upper(s) | upper(s) | upper(s) ucase(s) | upper(s) | upper(s) | ucase(s) | upper(s) |
| 轉英文小寫 | lower(s) | lower(s) | lower(s) lcase(s) | lower(s) | lower(s) | lcase(s) | lower(s) |
| 關鍵字搜尋 (不存在為0, 存在則大於或等於1) | instr(s,t) | charindex(s,t) | posstr(s,t) | position(t in s) | instr(s,t) locate(s,t) position(t in s) | instr(s,t) | position(t in s) |
| 子字串 | substr(s,n,m) | substring(s,n,m) | substr(s,n,m) | substr(s,n,m) substring(s,n,m) substring(s from n for m) | substring(s,n,m) substring(s from n for m) | substring(s,n,m) | |
| 結合字串 | s1 || s2 | s1 + s2 | s1 || s2 | s1 || s2 | concat(s1,s2) | s1 + s2 | |
| 字串長度 | LENGTH(s) | len(s) | LENGTH(s) | len(s) |
日期與時間
現在
| 功能 | Oracle | MS-SQL Server | IBM-DB2 | Postgre SQL | MySQL | MS-Access | ANSI-SQL |
|---|---|---|---|---|---|---|---|
| 現在日期與時間 | current_timestamp | current_timestamp getdate() | current timestamp | current_timestamp now() | current_timestamp now() | now() | current_timestamp |
| 現在日期 | current_date sysdate | current date | current_date | current_date sysdate() | current_date | ||
| 現在時間 | current_time | current time | current_time | current_time | current_time |
- 備註: Oracle在select時, 如無來源table的話, 須用 from dual 例如 select sysdate from dual
日期或時間分解後的值
| 功能 | Oracle | MS-SQL Server | IBM-DB2 | Postgre SQL | MySQL | MS-Access | ANSI-SQL |
|---|---|---|---|---|---|---|---|
| 年 | extract(year from d) | year(d) datepart(year,d) | year(d) | date_part('year',d) extract('year' from d) | year(d) extract(year from d) | year(d) datepart(year,d) | |
| 季(1~4) | datepart(quarter, d) datepart(qq, d) | quarter(d) | date_part('quarter',d) extract('quarter' from d) | quarter(d) | |||
| 月(1~12) | extract(month from d) | month(d) datepart(month,d) | month(d) | date_part('month',d) extract('month' from d) | month(d) extract(month from d) | month(d) datepart(month,d) | |
| 日(1~31) | extract(day from d) | day(d) datepart(day,d) | day(d) | date_part('day',d) extract('day' from d) | dayofmonth(d) extract(day from d) | day(d) datepart(day,d) | |
| 時(0~23) | extract(hour from d) | datepart(hour,d) | hour(d) | date_part('hour',d) extract('hour' from d) | hour(d) extract(hour from d) | hour(d) datepart(hour,d) | |
| 分(0~59) | extract(minute from d) | datepart(minute,d) | minute(d) | date_part('minute',d) extract('minute' from d) | minute(d) extract(minute from d) | minute(d) datepart(minute,d) | |
| 秒(0~59) | extract(second from d) | datepart(second,d) | second(d) | date_part('second',d) extract('second' from d) | second(d) extract(second from d) | second(d) datepart(second,d) |
日期或時間的增值或減值
| 功能 | Oracle | MS-SQL Server | IBM-DB2 | Postgre SQL | MySQL | MS-Access | ANSI-SQL |
|---|---|---|---|---|---|---|---|
| 月+/-n | add_months(d,n) | dateadd(mm,n,d) | date_add(d, interval n month) date_sub(d, interval n month) | dateadd(mm,n,d) | |||
| 日+/-n | dateadd(dd,n,d) | date_add(d, interval n day) date_sub(d, interval n day) | dateadd(dd,n,d) |
轉換
Hash值
| 功能 | Oracle | MS-SQL Server | IBM-DB2 | Postgre SQL | MySQL | MS-Access | ANSI-SQL |
|---|---|---|---|---|---|---|---|
| MD5 | 詳見備註1 | HashBytes('MD5', str) | md5(str) | MD5(str) | |||
| SHA1 | HashBytes('SHA1', str) | SHA1(str) |
- 備註1:(Oracle用的自訂md5函數)
CREATE OR REPLACE FUNCTION MD5(str IN VARCHAR2) RETURN VARCHAR2 IS retval VARCHAR2(32); BEGIN retval := UTL_RAW.cast_to_raw(DBMS_OBFUSCATION_TOOLKIT.MD5(INPUT_STRING => str)); RETURN retval; END;
Binary轉Hex字串
| 功能 | Oracle | MS-SQL Server | IBM-DB2 | Postgre SQL | MySQL | MS-Access | ANSI-SQL |
|---|---|---|---|---|---|---|---|
| Binary轉Hex字串 | RAWTOHEX(bin) | sys.fn_VarBinToHexStr(bin) |
ASCII碼
| 功能 | Oracle | MS-SQL Server | IBM-DB2 | Postgre SQL | MySQL | MS-Access | ANSI-SQL |
|---|---|---|---|---|---|---|---|
| 字元轉ASCII碼 | ascii(c) | ascii(c) | ascii(c) | ascii(c) | ascii(c) | asc(c) | |
| ASCII碼轉字元 | chr(n) | char(n) | chr(n) | chr(n) | char(n) | chr(n) |
UNICODE碼
| 功能 | Oracle | MS-SQL Server | IBM-DB2 | Postgre SQL | MySQL | MS-Access | ANSI-SQL |
|---|---|---|---|---|---|---|---|
| 字元轉ASCII碼 | unicode(c) | ||||||
| UNICODE碼轉字元 | nchr(n) | nchar(n) |
日期或時間轉字串
| 功能 | Oracle | MS-SQL Server | IBM-DB2 | Postgre SQL | MySQL | MS-Access | ANSI-SQL |
|---|---|---|---|---|---|---|---|
| 格式yyyy.mm.dd | to_char(d,'YYYY.MM.DD') | convert(varchar,d,102) | to_char(d,'YYYY.MM.DD') | date_format(d,'%Y.%m.%d') | |||
| 格式yyyy/mm/dd | to_char(d,'YYYY/MM/DD') | convert(varchar,d,111) | to_char(d,'YYYY/MM/DD') | date_format(d,'%Y/%m/%d') | |||
| 格式yyyymmdd | to_char(d,'YYYYMMDD') | convert(varchar,d,112) | to_char(d,'YYYYMMDD') | date_format(d,'%Y%m%d') |
數值轉字串
| 功能 | Oracle | MS-SQL Server | IBM-DB2 | Postgre SQL | MySQL | MS-Access | ANSI-SQL |
|---|---|---|---|---|---|---|---|
| 數值轉字串 | to_char(n) cast(n as varchar2(10)) | str(n) convert(varchar,n) cast(n as varchar) | cast(n as char(10)) | to_char(n) | cast(n as char) convert(n,char) | str(n) | |
| 字串轉數值 | to_number(e[,f]) | convert(t,e[,s]) | to_number(e,f) |
- n 數值
- t 要轉換的類型 (例如 int 或 float ...等)
- e 字串
- f 轉換前的格式 (例如 '999,999' )
- s 要轉換的格式
其他
資料清理
| 功能 | Oracle | MS-SQL Server | IBM-DB2 | Postgre SQL | MySQL | MS-Access | ANSI-SQL |
|---|---|---|---|---|---|---|---|
| 當n為Null時, 用e取代 | nvl(n,e) coalesce(n,e) | isnull(n,e) coalesce(n,e) | coalesce(n,e) | coalesce(n,e) | coalesce(n,e) | isnull(n,e) | coalesce(n,e) |
列舉
- 列舉資料庫名稱或Table名稱
| DB種類 | 列舉資料庫名稱 | 列舉Table名稱 |
|---|---|---|
| Oracle | select distinct OWNER
from ALL_TABLES
where OWNER not in ('SYS','SYSTEM')
and TABLESPACE_NAME not in ('CWMLITE','DRSYS','EXAMPLE','INDEX'
,'SYSTEM','TEMP','TOOLS','UNDOTBS','USERS','ODM','XDB')
| select OWNER || '.' || TABLE_NAME as Name
from ALL_TABLES
where OWNER not in ('SYS','SYSTEM')
and TABLESPACE_NAME not in ('CWMLITE','DRSYS','EXAMPLE','INDEX'
,'SYSTEM','TEMP','TOOLS','UNDOTBS','USERS','ODM','XDB')
|
| MS-SQL Server | select name from sys.databases | select name from [sysobjects] where type='U' |
| IBM-DB2 | list active databases | |
| Postgre SQL | ||
| MySQL | show databases | show tables |
| MS-Access | ||
| ANSI-SQL |
- 列舉DB-Link
| DB種類 | 列舉DB-Link |
|---|---|
| Oracle | select DB_LINK as name from ALL_DB_LINKS |
| MS-SQL Server | select name from sys.servers where is_linked=1 |
| IBM-DB2 | |
| Postgre SQL | |
| MySQL | |
| MS-Access | |
| ANSI-SQL |
資料庫版本
| DB種類 | 資料庫版本 |
|---|---|
| Oracle | select BANNER as SqlVer from v$version where BANNER like 'Oracle%'; |
| MS-SQL Server | select @@version as SqlVer; |
| IBM-DB2 | select * from sysibm.sysversions |
| Postgre SQL | select version(); |
| MySQL | select version() |
| MS-Access | |
| ANSI-SQL |
清除Log
| DB種類 | 清除Log |
|---|---|
| Oracle | |
| MS-SQL Server | USE MyDB; BACKUP LOG MyDB WITH no_log; DBCC SHRINKFILE (N'MyDB_Log' , 0, TRUNCATEONLY); |
| IBM-DB2 | |
| Postgre SQL | |
| MySQL | delete from log where date_field < '2007-12-11 00:00:00' |
| MS-Access | |
| ANSI-SQL |
執行SQL字串
| DB種類 | 執行SQL字串 |
|---|---|
| Oracle | EXECUTE IMMEDIATE StrSQL; |
| MS-SQL Server | EXEC SP_EXECUTESQL @StrSQL; |
| IBM-DB2 | |
| Postgre SQL | |
| MySQL | |
| MS-Access | |
| ANSI-SQL |
關於各種分頁Select
參考資料
- SQL Dialects Reference - wikibooks
- Oracle
- 函數……for Oracle 10g
- MS-SQL
- T-SQL語法 - MSDN Library……for MS-SQL Server 2000
- T-SQL語法 - MSDN Library……for MS-SQL Server 2005
- T-SQL語法 - MSDN Library……for MS-SQL Server 2008
- IBM-DB2
- 函數……for IBM DB2
- IBM DB2 数据库使用小技巧
- PostgreSQL
- MySQL
- 其他