char(10) 定長 非Unicode 數據經常變的情況下使用,因爲不會導致數據頁更改
varchar(10) 變長 非Unicode
nchar(10) 定長 Unicode 適用於多國語言的情況
nvchar(10) 變長 Unicode
執行sql語句的順序
from>where>group by>having>select>order by
升序 order by asc
降序 order by desc
默認升序
case 表達式
select staff.id,
case gender
when '男' then 'male'
else 'female' end
from staff
取前幾條記錄
select top (3) with ties id from staff order by...
top (10)percent
跳過第一條 取之後兩條
select id from staff order by id offset 1 rows fetch next 2 rows only
轉化
標準SQL cast
T-SQL convert
--代碼轉自互聯網
select CAST('123' as int) -- 123
select CONVERT(int, '123') -- 123
select CAST(123.4 as int) -- 123
select CONVERT(int, 123.4) -- 123
select CAST('123.4' as int)
select CONVERT(int, '123.4')
-- Conversion failed when converting the varchar value '123.4' to data type int.
select CAST('123.4' as decimal) -- 123
select CONVERT(decimal, '123.4') -- 123
select CAST('123.4' as decimal(9,2)) -- 123.40
select CONVERT(decimal(9,2), '123.4') -- 123.40
declare @Num money
set @Num = 1234.56
select CONVERT(varchar(20), @Num, 0) -- 1234.56
select CONVERT(varchar(20), @Num, 1) -- 1,234.56
select CONVERT(varchar(20), @Num, 2) -- 1234.5600