select dateadd(dd,-day(dateadd(month,-1,getdate()))+1,dateadd(month,-1,getdate())) /*上个月一号*/
select dateadd(dd,-day(getdate()),getdate()) /* 上月月底 */
select dateadd(dd,-day(getdate())+1,getdate()) /* 本月一号 */
select dateadd(dd,-day(dateadd(month,1,getdate())),dateadd(month,1,getdate())) /* 本月底 */
select dateadd(dd,-day(dateadd(month,1,getdate()))+1,dateadd(month,1,getdate())) /* 下月一号 */
select dateadd(dd,-day(dateadd(month,2,getdate())),dateadd(month,2,getdate())) /* 下月月底 */
查询本周本月日期
--本周:
select GETDATE()-6,GETDATE(),* from t_rm_saleflow where datediff(week,oper_date,getdate())=0
--本月:
select DATEADD(MM,DATEDIFF(MM,0,GETDATE()),0)
,DATEADD(MS,-3,DATEADD(MM,DATEDIFF(M,0,GETDATE())+1,0))
,* from t_rm_saleflow where datediff(week,oper_date,getdate())=0
--本季:
--前半年1-6,后半年7-12:select * from table where datepart(mm,C_CALLTIME)/7 = datepart(mm,getdate())/7
ONVERT(data_type,expression[,style])
convert(varchar(10),字段名,转换格式)
说明:
此样式一般在时间类型(datetime,smalldatetime)与字符串类型(nchar,nvarchar,char,varchar)
相互转换的时候才用到.
语句 结果
SELECT CONVERT(varchar(100), GETDATE(), 0) 07 15 2009 4:06PM
SELECT CONVERT(varchar(100), GETDATE(), 1) 07/15/09
SELECT CONVERT(varchar(100), GETDATE(), 2) 09.07.15
SELECT CONVERT(varchar(100), GETDATE(), 3) 15/07/09
SELECT CONVERT(varchar(100), GETDATE(), 4) 15.07.09
SELECT CONVERT(varchar(100), GETDATE(), 5) 15-07-09
SELECT CONVERT(varchar(100), GETDATE(), 6) 15 07 09
SELECT CONVERT(varchar(100), GETDATE(), 7) 07 15, 09
SELECT CONVERT(varchar(100), GETDATE(), 8) 16:06:26
SELECT CONVERT(varchar(100), GETDATE(), 9) 07 15 2009 4:06:26:513PM
SELECT CONVERT(varchar(100), GETDATE(), 10) 07-15-09
SELECT CONVERT(varchar(100), GETDATE(), 11) 09/07/15
SELECT CONVERT(varchar(100), GETDATE(), 12) 090715
SELECT CONVERT(varchar(100), GETDATE(), 13) 15 07 2009 16:06:26:513
SELECT CONVERT(varchar(100), GETDATE(), 14) 16:06:26:513
SELECT CONVERT(varchar(100), GETDATE(
select dateadd(dd,-day(getdate()),getdate()) /* 上月月底 */
select dateadd(dd,-day(getdate())+1,getdate()) /* 本月一号 */
select dateadd(dd,-day(dateadd(month,1,getdate())),dateadd(month,1,getdate())) /* 本月底 */
select dateadd(dd,-day(dateadd(month,1,getdate()))+1,dateadd(month,1,getdate())) /* 下月一号 */
select dateadd(dd,-day(dateadd(month,2,getdate())),dateadd(month,2,getdate())) /* 下月月底 */
查询本周本月日期
--本周:
select GETDATE()-6,GETDATE(),* from t_rm_saleflow where datediff(week,oper_date,getdate())=0
--本月:
select DATEADD(MM,DATEDIFF(MM,0,GETDATE()),0)
,DATEADD(MS,-3,DATEADD(MM,DATEDIFF(M,0,GETDATE())+1,0))
,* from t_rm_saleflow where datediff(week,oper_date,getdate())=0
--本季:
--前半年1-6,后半年7-12:select * from table where datepart(mm,C_CALLTIME)/7 = datepart(mm,getdate())/7
ONVERT(data_type,expression[,style])
convert(varchar(10),字段名,转换格式)
说明:
此样式一般在时间类型(datetime,smalldatetime)与字符串类型(nchar,nvarchar,char,varchar)
相互转换的时候才用到.
语句 结果
SELECT CONVERT(varchar(100), GETDATE(), 0) 07 15 2009 4:06PM
SELECT CONVERT(varchar(100), GETDATE(), 1) 07/15/09
SELECT CONVERT(varchar(100), GETDATE(), 2) 09.07.15
SELECT CONVERT(varchar(100), GETDATE(), 3) 15/07/09
SELECT CONVERT(varchar(100), GETDATE(), 4) 15.07.09
SELECT CONVERT(varchar(100), GETDATE(), 5) 15-07-09
SELECT CONVERT(varchar(100), GETDATE(), 6) 15 07 09
SELECT CONVERT(varchar(100), GETDATE(), 7) 07 15, 09
SELECT CONVERT(varchar(100), GETDATE(), 8) 16:06:26
SELECT CONVERT(varchar(100), GETDATE(), 9) 07 15 2009 4:06:26:513PM
SELECT CONVERT(varchar(100), GETDATE(), 10) 07-15-09
SELECT CONVERT(varchar(100), GETDATE(), 11) 09/07/15
SELECT CONVERT(varchar(100), GETDATE(), 12) 090715
SELECT CONVERT(varchar(100), GETDATE(), 13) 15 07 2009 16:06:26:513
SELECT CONVERT(varchar(100), GETDATE(), 14) 16:06:26:513
SELECT CONVERT(varchar(100), GETDATE(