sql通過(guò)日期判斷年齡函數(shù)的示例代碼
更新時(shí)間:2021年07月15日 16:12:19 作者:kueizheng
這篇文章主要介紹了sql通過(guò)日期判斷年齡函數(shù),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
定義函數(shù):
CREATE FUNCTION [dbo].[GetAge]
(
@BirthDay nvarchar(20) --生日
)
RETURNS varchar(20)
AS
BEGIN
if(@BirthDay is NUlL or @BirthDay='')
return '';
-- Declare the return variable here
DECLARE @age varchar(20)
DECLARE @years int
DECLARE @months int
DECLARE @days int
-- Add the T-SQL statements to compute the return value here
set @age = ''
set @years = year(GETDATE()) - year(@birthday)
set @months = month(GETDATE()) - month(@birthday)
if day(@birthday)<=day(GETDATE())
set @days = day(GETDATE()) - day(@birthday)
else
begin
set @months = @months - 1
if MONTH(@birthday) in (1,3,5,7,8,10,12)
set @days = 31-day(@birthday)+day(GETDATE())
else if MONTH(@birthday) in (4,6,9,11)
set @days = 30-day(@birthday)+day(GETDATE())
else if MONTH(@birthday) = 2
if (year(@birthday)%4 = 0 and year(@birthday)%100 <> 0) or year(@birthday)%400 = 0
set @days = 29-day(@birthday)+day(GETDATE())
else
set @days = 28-day(@birthday)+day(GETDATE())
end
if @months < 0
begin
set @years = @years - 1
set @months = @months + 12
end
if @years = 0 and @months = 0
begin
return convert(varchar,@days+1) + '天'
end
if @years > 0
set @age = cast(@years as varchar(5)) + '歲'
if @years < 3 and @months > 0 and @years>-1
begin
set @age = @age + cast(@months as varchar(5)) + '月'
end
if @years<0
set @age=''
RETURN @age
END
使用函數(shù):

到此這篇關(guān)于sql通過(guò)日期判斷年齡函數(shù)的示例代碼的文章就介紹到這了,更多相關(guān)sql日期計(jì)算年齡內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis collection 多條件查詢的實(shí)現(xiàn)方法
這篇文章主要介紹了mybatis collection 多條件查詢的實(shí)現(xiàn)方法的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-10-10
SQL Server STUFF函數(shù)的用法及應(yīng)用場(chǎng)景
SQLServer中的STUFF函數(shù)用于刪除指定位置的字符并插入新的字符,本文主要介紹了SQL Server STUFF函數(shù)的用法及應(yīng)用場(chǎng)景,具有一定的參考價(jià)值,感興趣的可以了解一下2025-03-03
MSSQL 檢查所使用的語(yǔ)句是否符合標(biāo)準(zhǔn)
標(biāo)準(zhǔn)SQL和T-SQL之間有很多區(qū)別——太多了,這里就不說(shuō)了。還有,如果你在SQL Server上工作, 那么使用這些私有的擴(kuò)展是有好處的。2009-11-11
sqlserver 2000中每個(gè)服務(wù)器角色的解釋
sqlserver 2000中每個(gè)服務(wù)器角色的解釋,方便大家以后對(duì)于數(shù)據(jù)庫(kù)權(quán)限有所了解。2011-03-03

