--查询
select employeeid,firstname,lastname from test
--插入
insert into test(lastname,firstname)values('yan','xu')
--更新,修改
update test
set firstname='Xu',title='the center of computer',region='XA',country='CH',city='XI AN'
where employeeid=10
--平均值
select avg(employeeid) as 平均值 from test
--将小写的字符数据转化为大写,lower相反
select upper(lastname)as mingzifrom test
where employeeid='1'
select getdate() --用getdate()函数获取当前系统的时间
--从getdate()返回的日期中提取月份数,可以用同样的方法提取年,日--
select datepart(month,getdate()) as 'month number'
--复制表--
select * into test
from Northwind.dbo.Categories
--取最大值--
select max(id) as Max_ID from customer
--返回employees表中首列的名称
select col_name(object_ID('employees'),1)
--取出列并重命列名的两种方法(用as和不用as)
select au_lname "Author last name" from authors
select au_lname as 'Author last name' from authors
--查询结果执行运算
select price "Original price",price*2 "New price" from titles
select price,ytd_sales,price*ytd_sales "Total revenue" from titles
--使用连接运算符(它看起来像个加号)来连接两个字符型字段
select au_fname+' '+au_lname "author name" from authors
select count(*) "sum" from authors --相同于select count(au_id)"sum" from authors
--找出某个条件在某个区间上的数据
select royalty from titles where royalty between 12 and 20
--以下两种写法得到的结果一样,满足其中一个条件
select royalty from titles where royalty=14 or royalty=16
select royalty from titles where royalty in(14,16)
--在非字符串类型的字段后面加字符串时需要先将给字段类型转换为字符串类型,如下:
select convert(char(8),price)+'US Dollars' from titles
select 'The price is'+convert(char(8),price) from titles
--取出名字首字母在A-F间的名字
select au_lname "Author name" from authors where au_lname like '[A-F]%'
--取出名字首字母不在A-F间的名字
select au_lname "Author name" from authors where au_lname like '[^A-F]%'
--以CGH开头的所有名字都将被返回
select au_lname from authors where au_lname like '[CGH]%'