Hi bros and sis
Please help me
I have problems for sql database date time calculation udf function.This udf function is do not refresh table calculation is true.But table refreshed not true value. Please help me and advice.


Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Nitin SontakkePosted Mar 1, 2017, 1:11 AM
U ThuPosted Mar 1, 2017, 12:59 AM
(
)
RETURNS VARCHAR(50) --this is return result type
AS
BEGIN
--declear variable
DECLARE @givenDate datetime
DECLARE @tempDate datetime
DECLARE @years int, @months int, @days int,@ID int
--Assign Value from database
SELECT @ID = ID from dbo.Personal
SELECT @givenDate = Birthday from dbo.Personal where ID = @ID
SELECT @tempDate = @givenDate
-- get year claculation
SELECT @years = DATEDIFF(yy, @tempDate, GETDATE()) -
CASE
WHEN
(MONTH(@givenDate) > MONTH(GETDATE()))
OR
(MONTH(@givenDate) = MONTH(GETDATE())
AND
DAY(@givenDate) > DAY(GETDATE()))
THEN 1
ELSE 0
END
SELECT @tempDate = DATEADD(yy, @years, @tempDate)
-- get months calculation
SELECT @months = DATEDIFF(m, @tempDate, GETDATE()) -
CASE
WHEN
DAY(@givenDate) > DAY(GETDATE())
THEN 1
ELSE 0
END
SELECT @tempDate = DATEADD(m, @months, @tempDate)
-- get days calculation
SELECT @days = DATEDIFF(d, @tempDate, GETDATE())
--print out result
RETURN (SELECT CAST(@years as varchar(5)) + ' years ' +
CAST(@months as varchar(3)) + ' months ' +
CAST(@days as varchar(3)) + ' days' )
END
Nitin SontakkePosted Feb 28, 2017, 11:45 PM
U ThuPosted Feb 28, 2017, 11:32 PM
[ID] INT IDENTITY (1, 1) NOT NULL,
[PersonalID] NVARCHAR (50) NULL,
[Name] NVARCHAR (50) NULL,
[Birthday] DATETIME NULL,
[TEST] NCHAR (10) DEFAULT ([dbo].[TotalYearsMonthsDays]()) NULL,
PRIMARY KEY CLUSTERED ([ID] ASC)
);
MY UDF Function is
(
)
RETURNS VARCHAR(50) --this is return result type
AS
BEGIN
--declear variable
DECLARE @givenDate datetime
DECLARE @tempDate datetime
DECLARE @years int, @months int, @days int,@ID int
--Assign Value from database
SELECT @givenDate = Birthday from dbo.Personal
SELECT @tempDate = @givenDate
-- get year claculation
SELECT @years = DATEDIFF(yy, @tempDate, GETDATE()) -
CASE
WHEN
(MONTH(@givenDate) > MONTH(GETDATE()))
OR
(MONTH(@givenDate) = MONTH(GETDATE())
AND
DAY(@givenDate) > DAY(GETDATE()))
THEN 1
ELSE 0
END
SELECT @tempDate = DATEADD(yy, @years, @tempDate)
-- get months calculation
SELECT @months = DATEDIFF(m, @tempDate, GETDATE()) -
CASE
WHEN
DAY(@givenDate) > DAY(GETDATE())
THEN 1
ELSE 0
END
SELECT @tempDate = DATEADD(m, @months, @tempDate)
-- get days calculation
SELECT @days = DATEDIFF(d, @tempDate, GETDATE())
--print out result
RETURN (SELECT CAST(@years as varchar(5)) + ' years ' +
CAST(@months as varchar(3)) + ' months ' +
CAST(@days as varchar(3)) + ' days' )
END
Nitin SontakkePosted Feb 28, 2017, 11:31 PM
U ThuPosted Feb 28, 2017, 11:23 PM
U ThuPosted Feb 28, 2017, 11:17 PM
Nitin SontakkePosted Feb 28, 2017, 11:01 PM