Home > Database > Mysql Tutorial > 计算指定年月的最后一天的自定义MYSQL函数_MySQL

计算指定年月的最后一天的自定义MYSQL函数_MySQL

WBOY
Release: 2016-06-01 13:56:17
Original
1179 people have browsed it

/*
    Name :    Fun_YiYueDeZuiHouYiTian
    Function :   根据传入的格式如“200311”的年月值计算出当月的最后一天
    Parameters :  
                        BenYue  本月
                            Type :  Char(6) 
                            Format :  "200311"
    ReturnValue :  
                        格式如“200310”的年月值
                            Type :  Char(6)
    Steps :
    Author :   Waxdoll Cheung
    Date :    2004-04-04
*/

CREATE FUNCTION dbo.Fun_YiYueDeZuiHouYiTian
 (
  @BenYue CHAR(6)
 )
RETURNS CHAR(10)
AS
 BEGIN
  DECLARE @ReturnValue CHAR(2)
  DECLARE @YueFen CHAR(2)
  DECLARE @NianFen CHAR(4)

  SET @YueFen = RIGHT(@BenYue, 2)
  SET @NianFen = LEFT(@BenYue, 4)

  IF @YueFen IN ('01', '03', '05', '07', '08', '10', '12')
  BEGIN
   SET @ReturnValue = '31'
  END

  IF @YueFen IN ('04', '06', '09', '11')
  BEGIN
   SET @ReturnValue = '30'
  END

  IF @YueFen = '02'
  BEGIN
   IF ((@NianFen % 4 = 0 AND @NianFen % 100 0) OR (@NianFen % 400 = 0))
   BEGIN
    SET @ReturnValue = '29'
   END
   ELSE
   BEGIN
    SET @ReturnValue = '28'
   END
  END

  RETURN @NianFen + '-' + @YueFen + '-' + @ReturnValue
 END

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template