Sybase查询:
查询某天是星期几
set datefirst 1
select datepart(weekday,TB_STARTTIME) 星期几 from CC_TELEBILL
查询时间的方法
获得具体时间: TB_STARTTIME between '2010-12-27 20:00:05.000' and '2010-12-27 23:59:05.000'
获得年份: datepart(yy,TB_STARTTIME)=2011
获得月份: substring (convert(char,TB_STARTTIME,112),1,6) = '201101'
获得日 convert(char,TB_STARTTIME,112) between '20110301' and '20110301'
获取时段 substring(convert(char,TB_STARTTIME,108),1,2)=‘08’
获取长度
datalength(TB_BILLNO )
不等于
datalength( TB_BILLNO)<>8
模糊查询
TB_BILLNO like '[0186]%'
保留两位小数
round(avg(总费用),2)
日期函数处理:
日期函数
getdate()
得到当前时间,可以设置得到各种时间格式.
datepart(日期部分,日期)
取指定时间的某一个部分,年月天时分秒.
datediff(日期部分,日期1,日期2)
计算指定的日期1和日期2的时间差多少.
dateadd(日期部分,数值表达式,日期)
计算指定时间,再加上表达式指定的时间长度.
--取时间的某一个部分
select datepart(yy,getdate()) --year
select datepart(mm,getdate()) --month
select datepart(dd,getdate()) --day
select datepart(hh,getdate()) --hour
select datepart(mi,getdate()) --min
select datepart(ss,getdate()) --sec
--取星期几
set datefirst 1
select datepart(weekday,getdate()) --weekday
--字符串时间
select getdate() -- '03/11/12'
select convert(char,getdate(),101) -- '09/27/2003'
select convert(char,getdate(),102) -- '2003.11.12'
select convert(char,getdate(),103) -- '27/09/2003'
select convert(char,getdate(),104) -- '27.09.2003'
select convert(char,getdate(),105) -- '27-09-2003'
select convert(char,getdate(),106) -- '27 Sep 2003'
select convert(char,getdate(),107) --'Sep 27, 2003'
select convert(char,getdate(),108) --'11:16:06'
select convert(char,getdate(),109) --'Sep 27 2003 11:16:28:746AM'
select convert(char,getdate(),110) --'09-27-2003'
select convert(char,getdate(),111) --'2003/09/27'
select convert(char,getdate(),112) --'20030927'
select rtrim(convert(char,getdate(),102))+' '+(convert(char,getdate(),108)) -- '2003.11.12