sql日期时间

1、Sybase:

//周五至周四
//select convert(varchar(10),dateadd(day,-1-datepart(weekday,getdate()),getdate()),111);
//select convert(varchar(10),dateadd(day,5-datepart(weekday,getdate()),getdate()),111);

//上个月第一天   上个月最后一天
select dateadd(mm,datediff(mm,'1900-1-1',dateadd(mm,-1,getdate())),'1900-1-1');
select dateadd(ms,-3,dateadd(mm,datediff(mm,'1900-1-1',getdate()),'1900-1-1'));


//上星期第一天
//select dateadd(mm,datediff(mm,'1900-1-1',dateadd(mm,-1,getdate())),'1900-1-1');

//上星期最后一天
//select dateadd(ms,-3,dateadd(mm,datediff(mm,'1900-1-1',getdate()),'1900-1-1'));

//201003
select convert(varchar(6),dateadd(mm,-1,getdate()),112);


//得到当天日期 2011/11/24
select convert(char(10),getdate(),111);   select getdate()  //当前时间

其他:http://shang.iteye.com/blog/260046

2、Mssql

--当月第一天
--select DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)

--当月最后一天   (下个月第一天减去3毫秒)
--select dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0))

-- 上月第一天
--select dateadd(mm,datediff(mm,0,getdate())-1,0)

--select dateadd(mm,DATEDIFF(mm,0,dateadd(mm,-1,getdate())),0)

-- 上月最后一天  (当月第一天减去3毫秒)
--select dateadd(ms,-3,DATEADD(mm, DATEDIFF(mm,0,getdate()), 0))

 


//上个月第一天   上个月最后一天
select dateadd(mm,datediff(mm,'1900-1-1',dateadd(mm,-1,getdate())),'1900-1-1');
select dateadd(ms,-3,dateadd(mm,datediff(mm,'1900-1-1',getdate()),'1900-1-1'));


---获取201002类似日期
--select convert(varchar(6),dateadd(mm,-1,getdate()),112)


--求上个月天数  (当月第一天-上月第一天)
--select convert(int,datediff(day, dateadd(mm,DATEDIFF(mm,0,dateadd(mm,-1,getdate())),0),DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)))

--求上个月是几月
--select datepart(mm,getdate())-1

--select DATEPART(month,DATEADD(MONTH,-1,DATEADD(DAY,1-DAY(GETDATE()),CONVERT(VARCHAR(8),GETDATE(),112))))


--周一至周日
--select convert(varchar(10),getdate()-(datepart(weekday,getdate())-2),120)as "第一天(周一)",
--convert(varchar(10),getdate()+(8-datepart(weekday,getdate())),120)as "最后一天(周日)"

--(上一周)周五至周四
select convert(varchar(10),getdate()-(datepart(weekday,getdate())+1),120)as "第一天(周五)",
convert(varchar(10),getdate()+(5-datepart(weekday,getdate())),120)as "最后一天(周四)"

 

--上上周
select convert(varchar(10),getdate()-(datepart(weekday,getdate())+8),120)as "第一天(周五)",
convert(varchar(10),getdate()+(-2-datepart(weekday,getdate())),120)as "最后一天(周四)"


--年查询
SELECT DATEADD(YEAR,DATEDIFF(YEAR,0,getdate()),0) AS '所在年的第一天'

--
SELECT DATEADD(WEEK,DATEDIFF(WEEK,-1,getdate()),-1) AS '所在星期的星期日'

 --或者
 SELECT DATEADD(WEEK,DATEDIFF(WEEK,6,'2011-1-1'),6) AS '所在星期的星期日'

---星期六
SELECT DATEADD(Day,6-(DATEPART(Weekday,'2011-1-1')+@@DATEFIRST-1)%7,'2011-1-1')


---以星期一为周一
DECLARE @MondayTime varchar(10)
DECLARE @SundayTime varchar(10)
IF(datepart(weekday,getdate())-2 < 0)
BEGIN
 SET @MondayTime = convert(varchar(10),dateadd(dd,-1,getdate())-(datepart(weekday,dateadd(dd,-1,getdate()))-2),120)
 SET @SundayTime = convert(varchar(10),dateadd(dd,-1,getdate())+(8-datepart(weekday,dateadd(dd,-1,getdate()))),120)
END
ELSE
 BEGIN
  SET @MondayTime =convert(varchar(10),getdate()-(datepart(weekday,getdate())-2),120)
  SET @SundayTime =convert(varchar(10),getdate()+(8-datepart(weekday,getdate())),120)
 END
 SELECT @MondayTime as '第一天(周一)', @SundayTime as '最后一天(周日)'

 其他:http://www.cnblogs.com/wangdaye/archive/2010/07/28/1786709.html

 

 

 

你可能感兴趣的:(sql)