DateADD gives error

CouldsInSky 41 Reputation points
2021-02-02T03:29:30.333+00:00

Hello All,
I have this:
Declare @Tenure Table
(EmployeeID int,
LastName VarChar(255)
,DateOfBirth Date
)

Insert into @Tenure
Select EmployeeID, LastName, DateAdd(YY,DateOfBirth,GetDate())
From dbo.Employees

It gives this error:

Msg 8116, Level 16, State 1, Line 43
Argument data type date is invalid for argument 2 of dateadd function.

What is the solution?

Respectfully,
Blue Sky

Developer technologies Transact-SQL
0 comments No comments
{count} votes

Accepted answer
  1. MelissaMa-MSFT 24,221 Reputation points
    2021-02-02T05:39:18.457+00:00

    Hi @CouldsInSky ,

    Welcome to Microsoft Q&A!

    DATEADD is one function which adds a specified number value (as a signed integer) to a specified datepart of an input date value, and then returns that modified value.

    Syntax:

    DATEADD (datepart , number , date )

    For example:

    SELECT  DATEADD(year,-20,GetDate())  --2001-02-02   
    

    It is recommended for you to post CREATE TABLE statements for your table(dbo.Employees) together with INSERT statements with sample data, enough to illustrate all angles of the problem. We also need to see the expected result of the sample.

    Meanwhile you could refer below example:

    drop table if exists dbo.Employees  
      
    create table dbo.Employees  
    (EmployeeID int,  
    LastName VarChar(255)  
    ,Age int)  
      
    insert into dbo.Employees values  
    (1,'Tom',20)  
      
    Declare @Tenure Table  
    (EmployeeID int,  
    LastName VarChar(255)  
    ,DateOfBirth Date  
    )  
      
    Insert into @Tenure  
    Select EmployeeID, LastName, DateAdd(YY,-1*Age,GetDate())  
    From dbo.Employees  
      
    select * from @Tenure  
    

    Output:

    EmployeeID	LastName	DateOfBirth  
    1	Tom	2001-02-02  
    

    Or you could use another function DateDiff if you would like to count the age of the employee as below:

    drop table if exists dbo.Employees  
      
    create table dbo.Employees  
    (EmployeeID int,  
    LastName VarChar(255)  
    ,DateOfBirth date)  
      
    insert into dbo.Employees values  
    (1,'Tom','2001-01-02')  
      
    Declare @Tenure Table  
    (EmployeeID int,  
    LastName VarChar(255)  
    ,Age int  
    )  
      
    Insert into @Tenure  
    Select EmployeeID, LastName, DateDiff(YY,DateOfBirth,GetDate())  
    From dbo.Employees  
      
    select * from @Tenure  
    

    Output:

    EmployeeID	LastName	Age  
    1	Tom	20  
    

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Tom Phillips 17,771 Reputation points
    2021-02-02T17:23:39.877+00:00

    DATEADD adds an integer value to a date.

    It appears you are trying to get the age, which would be DATEDIFF().

    https://learn.microsoft.com/en-us/sql/t-sql/functions/datediff-transact-sql?view=sql-server-ver15

    0 comments No comments

  2. CouldsInSky 41 Reputation points
    2021-02-02T18:49:45.267+00:00

    Hello team,

    With what you said then DateAdd function requires an integer for the second argument. Since I entered a date for second argument, I get an error.

    Would you kindly please confirm this?

    Respectfully,

    Blue Sky


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.