Dec 4, 2008

MS SQL Server 函数及运算

1. 改变列标题
=, AS
SELECT 'database' = db_name();
SELECT id AS UserId FROM student;

2. 增加一列
SELECT au_fname, au_lname, 'Identification Number: ', au_id FROM
authors;

3. 算术运算 (+、-、*、/、%)
%只能用于 INT, SMALLINT, TINYINT
SELECT title, price, 'price -3' = price -3

4. 数学函数 (ABS, SIN, COS, TAN, PI(), RAND(), SIGN(), SQRT(), SQUARE(),
CEILING(38/3), EXP(2.0), LOG(10) ...)
SELECT LOG(10)
SELECT 69/8

5. 字符串函数
SELECT ascii('abc')
SELECT 'SQL' + 'Server' + '2005'
SELECT char(36)
SELECT charindex('Server', 'SQL Server 2005')
SELECT lower('ABC')
SELECT upper('cde')
SELECT replicate('MSSQL-', 10)
SELECT substring('1234567890', 5, 3)
SELECT len('hello world')
SELECT stuff('1234567890', 3, 2, '-abcdefghigk-')

6. 日期和时间函数 (GETDATE(), DATEADD())
SELECT GETDATE()

7. 系统函数 (DB_NAME(), USER_NAME(), SUSER_NAME(), ISNUMERIC(), ISDATE(), )
ISNUMERIC(title_id):如果是数字类型,返回1;如果是日期、字符串或者
其他数据类型,返回0

8. 数据类型强制转换
SELECT SUBSTRING(title, 1, 30) AS Title, ytd_sales FROM titles
WHERE CONVERT(char(20), ytd_sales) LIKE '3%'

No comments:

Post a Comment

您的评论将使我blog更有动力~