[转载]SQL语句中的日期计算

1. 本月的第一天
SELECT  DATEADD(mm,  DATEDIFF(mm,0,getdate()),  0)

2. 本月的最后一天
SELECT  dateadd(ms,-3,DATEADD(mm,  DATEDIFF(m,0,getdate())+1,  0))

3.上个月的第一天
select dateadd(m,-1,  DATEADD(mm,  DATEDIFF(mm,0,getdate()),  0))

4. 上个月的最后一天 
SELECT  dateadd(ms,-3,DATEADD(mm,  DATEDIFF(mm,0,getdate()),  0))

5. 本周的星期一
SELECT  DATEADD(wk,  DATEDIFF(wk,0,getdate()),  0)

6.本周的周日
select dateadd(d,+6 ,DATEADD(wk,  DATEDIFF(wk,0,getdate()),  0) )

7.上周的星期一
select dateadd(d,-7 ,DATEADD(wk,  DATEDIFF(wk,0,getdate()),  0) )

8.上周的周日
select dateadd(d,-1 ,DATEADD(wk,  DATEDIFF(wk,0,getdate()),  0) )

9. 一年的第一天
SELECT  DATEADD(yy,  DATEDIFF(yy,0,getdate()),  0)

10. 季度的第一天
SELECT  DATEADD(qq,  DATEDIFF(qq,0,getdate()),  0)

11. 去年的最后一天
SELECT  dateadd(ms,-3,DATEADD(yy,  DATEDIFF(yy,0,getdate()),  0))

12. 本月的第一个星期一
select  DATEADD(wk,  DATEDIFF(wk,0,dateadd(dd,6-datepart(day,getdate)),getdate))),0)

13. 本年的最后一天
SELECT  dateadd(ms,-3,DATEADD(yy,  DATEDIFF(yy,0,getdate())+1,  0))。

Sql Server中的日期与时间函数 
1.  当前系统日期、时间 
    select getdate()

2. dateadd  在向指定日期加上一段时间的基础上,返回新的 datetime 值
   例如:向日期加上2天 
   select dateadd(day,2,'2004-10-15')  --返回:2004-10-17 00:00:00.000

3. datediff 返回跨两个指定日期的日期和时间边界数。
   select datediff(day,'2004-09-01','2004-09-18')   --返回:17

4. datepart 返回代表指定日期的指定日期部分的整数。
  SELECT DATEPART(month, '2004-10-15')  --返回 10

5. datename 返回代表指定日期的指定日期部分的字符串
   SELECT datename(weekday, '2004-10-15')  --返回:星期五

6. day(), month(),year() --可以与datepart对照一下

select 当前日期=convert(varchar(10),getdate(),120) 
,当前时间=convert(varchar(8),getdate(),114)

select datename(dw,'2004-10-15')

select 本年第多少周=datename(week,'2004-10-15')
      ,今天是周几=datename(weekday,'2004-10-15')

http://www.52186.cn/nibablog/blogview.asp?logID=333

sql日期格式转换

--------------------------------------------------------------------------------
来源:CSDN   发布会员:新书城收集整理   发布时间:2006-7-31   人气:384
 
 
在sql中,smalldatetime及datetime型的数据显示不是那么让人满意,因此我找了些格式化的方法列出来"

convert(char(10),datetime,101)

在数据库取出来的时候就转换好
 select getdate()
 
 2006-05-12 11:06:08.177
 
 我整理了一下SQL Server里面可能经常会用到的日期格式转换方法:
 
 举例如下:
 select Convert(varchar(10),getdate(),120)

2006-05-12 
 select CONVERT(varchar, getdate(), 120 )
 2006-05-12  11:06:08

select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','')
 20060512110608
 
 select CONVERT(varchar(12) , getdate(), 111 )
 2006/05/12
 
 select CONVERT(varchar(12) , getdate(), 112 )
 20060512

select CONVERT(varchar(12) , getdate(), 102 )
 2006.05.12
 
 其它几种不常用的日期格式转换方法:

select CONVERT(varchar(12) , getdate(), 101 )
 0612/2005 select CONVERT(varchar(12) , getdate(), 103 )
 12/09/2004

select CONVERT(varchar(12) , getdate(), 104 )
 12.05.2006

select CONVERT(varchar(12) , getdate(), 105 )
 12-05-2006

select CONVERT(varchar(12) , getdate(), 106 )
 12 05 2006

select CONVERT(varchar(12) , getdate(), 107 )
 05 12, 2006

select CONVERT(varchar(12) , getdate(), 108 )
 11:06:08
 
 select CONVERT(varchar(12) , getdate(), 109 )
 0512 2006 1

select CONVERT(varchar(12) , getdate(), 110 )
 09-12-2004

select CONVERT(varchar(12) , getdate(), 113 )
 12 052006

select CONVERT(varchar(12) , getdate(), 114 )
 11:06:08.177

============================================================================================
实例演练

select * from salesmb
select count(*) from salesdreport where employeeid='11'and productsid='7' and (updatetime between '2006-8-1 00:00:00'and '2006-8-30 00:00:00') --销售数量
select sum(sales) from salesmb where eid='11'and productsid='7' and (mbrq between '2006-8-1 00:00:00'and '2006-8-30 00:00:00') --目标数量

select dateadd(d,-day(getdate()),getdate())

select cast(year(getdate()) as nvarchar)+'-'+cast(month(getdate()) as varchar)+'-1 00:00'
select cast(year(getdate()) as nvarchar)+'-'+cast(month(getdate()) as varchar)+'-'+cast(day(getdate()) as varchar)

select dateadd(m,1,dateadd(d,-day(getdate()),getdate()))

select dateadd(m,1,getdate())

=====================================================================================================================================================

select * from salesmb
select count(*) from salesdreport where employeeid='11'and productsid='7' and (updatetime between '2006-8-1 00:00:00'and '2006-8-30 00:00:00') --销售数量
select sum(sales) from salesmb where eid='11'and productsid='7' and (mbrq between '2006-8-1 00:00:00'and '2006-8-30 00:00:00') --目标数量

select dateadd(d,-day(getdate()),getdate())

select cast(year(getdate()) as nvarchar)+'-'+cast(month(getdate()) as varchar)+'-1 00:00'
select cast(year(getdate()) as nvarchar)+'-'+cast(month(getdate()) as varchar)+'-'+cast(day(getdate()) as varchar)

select dateadd(m,1,dateadd(d,-day(getdate()),getdate()))

select dateadd(m,1,getdate())

======================================================================================================================================================

select * from salesmb
select count(*) from salesdreport where employeeid='11'and productsid='7' and (updatetime between '2006-8-1 00:00:00'and '2006-8-30 00:00:00') --销售数量
select sum(sales) from salesmb where eid='11'and productsid='7' and (mbrq between '2006-8-1 00:00:00'and '2006-8-30 00:00:00') --目标数量

select dateadd(d,-day(getdate()),getdate())

select cast(year(getdate()) as nvarchar)+'-'+cast(month(getdate()) as varchar)+'-1 00:00'
select cast(year(getdate()) as nvarchar)+'-'+cast(month(getdate()) as varchar)+'-'+cast(day(getdate()) as varchar)

select dateadd(m,1,dateadd(d,-day(getdate()),getdate()))

select dateadd(m,1,getdate())

===============================================================================================================================

select getdate()
select dateadd(d,1,dateadd(d,-day(getdate()),getdate()))+"00:00"

declare @sdate smalldatetime
select cast(year(getdate()) as nvarchar)+'-'+cast(month(getdate()) as varchar)+'-1 00:00'
select @sdate
select dateadd(day,-1,dateadd(Month,1,@sdate))

select cast(year(getdate()) as nvarchar)+'-'+cast(month(getdate()) as varchar)+'-1 00:00'

select dateadd(d,-day(getdate()),getdate())

select dateadd(day,-1,dateadd(Month,1,cast(year(getdate()) as nvarchar)+'-'+cast(month(getdate()) as varchar)+'-1 00:00'))

select dateadd(d,-day(getdate()),getdate())

select dateadd(d,1,dateadd(d,-day(getdate()),getdate()))
select dateadd(m,

SELECT dateadd(ms,-3,DATEADD(mm, DATEDIFF(mm,0,getdate()), 0))
SELECT dateadd(ms,0,DATEADD(mm, DATEDIFF(mm,0,getdate()), 0))

原文出处:http://www.cnblogs.com/qfb620/archive/2008/02/26/1082644.html

上一篇:PHP之时间和日期函数


下一篇:js格式化日期