sql通過日期判斷年齡函數(shù)的示例代碼
更新時間:2021年07月15日 16:12:19 作者:kueizheng
這篇文章主要介紹了sql通過日期判斷年齡函數(shù),本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
定義函數(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ù):
到此這篇關于sql通過日期判斷年齡函數(shù)的示例代碼的文章就介紹到這了,更多相關sql日期計算年齡內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SQLServer 2005 實現(xiàn)數(shù)據(jù)庫同步備份 過程-結(jié)果-分析
簡單來說,數(shù)據(jù)庫復制就是由兩臺服務器,主服務器和備份服務器,主服務器修改后,備份服務器自動修改2012-06-06Godaddy 導入導出MSSQL數(shù)據(jù)庫的實現(xiàn)步驟
可以從限制文件中導入SQL共享服務器數(shù)據(jù)庫。如果想把存放在其他地方的數(shù)據(jù)導入,需要先把其內(nèi)容拷到限制文件中。(2010-02-02設定sql server定期自動備份數(shù)據(jù)庫
設定sql server定期自動備份數(shù)據(jù)庫...2007-01-01SQL Server 2017 Developer的下載、安裝、配置及SSMS的下載安裝配置(圖文教程詳解)
這篇文章主要介紹了SQL Server 2017 Developer的下載、安裝、配置及SSMS的下載安裝配置,本文通過圖文并茂的形式給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02數(shù)據(jù)庫更新Sqlserver腳本總結(jié)
數(shù)據(jù)庫更新Sqlserver腳本總結(jié),需要的朋友可以參考下。2011-06-06