有人可以帮我找到mysql上个月的第一个日期吗?

时间:2011-11-09 01:32:30

标签: mysql mysqli mysqldump

我无法运行此功能,是否有任何更改:

CREATE FUNCTION [GetFirstDateofMonth] 
(@Date as DateTime)  

RETURNS DateTime AS  
BEGIN 

Declare @FirstDate DateTime 
Set @FirstDate = DateAdd(Day, 1, @Date - Day(@Date) + 1) -1

RETURN @FirstDate

END
CREATE  FUNCTION [GetLastDateofMonth] 
(@Date as DateTime)  

RETURNS DateTime AS  
BEGIN 

Declare @LastDate DateTime 
Set @LastDate = DateAdd(Month, 1, @Date - Day(@Date) + 1) -1

RETURN @LastDate

END

1 个答案:

答案 0 :(得分:0)

假设你想要它相对于“现在”:

select subdate(adddate(last_day(now()), 1), interval 2 month)

now()替换为您想要的任何内容,如果它相对于“now”以外的某个日期