--字符串功能
--substring
print substring('iamagoodperson',1,5)
select substring('iamagoodperson',1,5)
--upper
select upper('he is a good person')
--lower
select LOWER('this is an VERY interesting job')
--ltrim
select ltrim(' i am a good person')
--rtrim
select rtrim(' heihei,i do not know why it likes this ')
--replace
select replace('iwanttoaskyou','ttoa','i love you')
--stuff
select stuff('我的名字是朱旭杰',6,8,'summer')
--Date/Time Fuction
--getdate()
select getdate() as 'today'
--dateadd()
select dateadd(yy,10,getdate())
--datediff()
select datediff(yy,'1982/5/3',getdate()) as
--datepart()
select datepart(dw,getdate())
select datepart(yy,getdate())
select datepart(mm,getdate())
select datepart(dd,getdate())
select datepart(ss,getdate())
select datepart(ms,getdate())
select datepart(dd,'1982/5/3')
print datepart(dw,'1982/8/22')
--day(),相当于datepart(dd,时间)
select day('1982/5/3')
select day(getdate())
--month(),相当于datepart(mm,时间)
select month(getdate())
--year(),相当于datepart(yy,时间)
select year(getdate())
--数学函数
--abs()
select abs(-100.3456)
--sin()
select sin(0.54)
--cos()
select cos(3.14)
--power()
select power(10,2)
--round 返回数字表达式并四舍五入为指定的长度或精度
select round(100.45,1)
select round(123,45,-2)
--floor()
select floor(4.9)
select floor(-123.99)
--ceiling()
select ceiling(4.9)
select ceiling(-123.99)
--sqrt()
select sqrt(100)
--square
select square(10)
select square(-15)
--转换函数
--cast()
select cast(100.45 as int)
select cast(1345 as varchar(10))
--convert()
select convert(int,100.56)
select convert(varchar(10),2345)
--空值函数
--isnull()
declare @temp_table table
(
bookID VARCHAR(10) primary key,
book_price float default null,
bookName varchar(50)
)
insert into @temp_table values('1',50,'c#')
insert into @temp_table values('2',null ,'c')
select bookID AS '书的编号',isnull(book_price,0) as '书的价格'
from @temp_table
--nullif(),只要参数里的两个表达式相同就返回null
select nullif('iam','iam')
--coalesce返回其参数中第一个非空表达式
select coalesce(null,null,'i am a good boy')
SQL Server中文版的默认的日期字段datetime格式是yyyy-mm-dd Thh:mm:ss.mmm
例如:
select getdate()
2004-09-12 11:06:08.177
整理了一下SQL Server里面可能经常会用到的日期格式转换方法:
举例如下:
select CONVERT(varchar, getdate(), 120 )
2004-09-12 11:06:08
select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','')
20040912110608
select CONVERT(varchar(12) , getdate(), 111 )
2004/09/12
select CONVERT(varchar(12) , getdate(), 112 )
20040912
select CONVERT(varchar(12) , getdate(), 102 )
2004.09.12
select CONVERT(varchar(12) , getdate(), 101 )
09/12/2004
select CONVERT(varchar(12) , getdate(), 103 )
12/09/2004
select CONVERT(varchar(12) , getdate(), 104 )
12.09.2004
select CONVERT(varchar(12) , getdate(), 105 )
12-09-2004
select CONVERT(varchar(12) , getdate(), 106 )
12 09 2004
select CONVERT(varchar(12) , getdate(), 107 )
09 12, 2004
select CONVERT(varchar(12) , getdate(), 108 )
11:06:08
select CONVERT(varchar(12) , getdate(), 109 )
09 12 2004 1
select CONVERT(varchar(12) , getdate(), 110 )
09-12-2004
select CONVERT(varchar(12) , getdate(), 113 )
12 09 2004 1
select CONVERT(varchar(12) , getdate(), 114 )
11:06:08.177
select getdate()
结果:2003-12-28 16:52:00.107
select convert(char(8),getdate(),112)
结果:20031228
select convert(char(8),getdate(),108)
结果:16:52:00
select convert(char(8),getdate(),112)
|
指日期格式
规则如下:
1 101 美国 mm/dd/yyyy
2 102 ANSI yy.mm.dd
3 103 英国/法国 dd/mm/yy
4 104 德国 dd.mm.yy
5 105 意大利 dd-mm-yy
6 106 - dd mon yy
7 107 - mon dd, yy
8 108 - hh:mm:ss
- 9 或 109 (*) 默认值 + 毫秒 mon dd yyyy hh:mi:ss:mmmAM(或 PM)
10 110 美国 mm-dd-yy
11 111 日本 yy/mm/dd
12 112 ISO yymmdd
- 13 或 113 (*) 欧洲默认值 + 毫秒 dd mon yyyy hh:mm:ss:mmm(24h)
14 114 - hh:mi:ss:mmm(24h)
- 20 或 120 (*) ODBC 规范 yyyy-mm-dd hh:mm:ss[.fff]
- 21 或 121 (*) ODBC 规范(带毫秒) yyyy-mm-dd hh:mm:ss[.fff]
- 126(***) ISO8601 yyyy-mm-dd Thh:mm:ss:mmm(不含空格)
- 130* 科威特 dd mon yyyy hh:mi:ss:mmmAM
- 131* 科威特 dd/mm/yy hh:mi:ss:mmmAM
关于Emaker中字段的格式转换和字段间的运算代码可以加到属性里的“格式转换(读出)”和“格式转换(写入)”,table字段设定里的“字段”位置也可以灵活加函数。比如:'AF'+ID 或者ID+'/'+PWD ,convert(varchar(50),F1) ,
convert(int,%)-19110000 (读出)
convert(char(8),convert(int,%)+19110000) (写入)
实现行的合计运算等等。加入:%系统变量%,则是调用在Emaker 系统中设定的系统变量
文章出处:http://www.diybl.com/course/7_databases/sql/sqlServer/2007115/84415.html