===================== MsSQL용===========================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
/**
* 파일명: GET_AGE
* 작성일: 2007-07-21
* 작성자: 얼룩푸우(budget74@nate.com, http://blog.naver.com/budget74)
* 설 명: 생년월일로 부터 나이 리턴
*****************************************************************
*
*/
CREATE FUNCTION [oudisk].[GET_AGE](@now char(8), @jumin char(13)) RETURNS smallint
AS
BEGIN
/* define */
DECLARE @p_age smallint
DECLARE @p_thisday int
DECLARE @p_birthday int
DECLARE @p_jender smallint
SET @p_thisday = CONVERT(int, @now)
SET @p_birthday = CONVERT(int, SUBSTRING(@jumin, 1, 6))
SET @p_jender = CONVERT(int,SUBSTRING(@jumin, 7, 1))
SET @p_age=(
@p_thisday
-
(
@p_birthday +
CASE @p_jender
WHEN 1 THEN 19000000
WHEN 2 THEN 19000000
WHEN 3 THEN 20000000
WHEN 4 THEN 20000000
ELSE 19000000
END
)
)/10000
/* 결과 */
RETURN @p_age
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
===================== MySQL용===========================================================
DELIMITER $$
DROP FUNCTION IF EXISTS `test`.`fAge` $$
CREATE FUNCTION `test`.`fAge` (gJumin INT) RETURNS INT
BEGIN
/**
* COMMENT: Get Age form Jumin Number
* Date: 2007-09-28
* Writer: JongSeok Seo (budget74@nate.com, http://blog.naver.com/budget74)
*/
DECLARE iAge SMALLINT;
DECLARE iThisDay INT;
DECLARE iBirthDay INT;
DECLARE iJender SMALLINT;
SELECT REPLACE(SUBSTRING(NOW(),1,10),'-', '') INTO iThisDay;
SET iBirthday = SUBSTRING(gJumin, 1, 6);
SET iJender = SUBSTRING(gJumin, 9, 1);
SET iAge =TRUNCATE((iThisDay - (iBirthDay + CASE iJender
WHEN 1 THEN 19000000
WHEN 2 THEN 19000000
WHEN 3 THEN 20000000
WHEN 4 THEN 20000000
ELSE 19000000
END))/10000,0);
RETURN iAge;
END $$
DELIMITER ;
| Style : Background0, Font0, Size16 |