Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8193003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:19:47+00:00 2026-06-07T04:19:47+00:00

In SQL Server, I need to wrap the DATEADD built-in function with another function.

  • 0

In SQL Server, I need to wrap the DATEADD built-in function with another function.

The problem is I need to implement this behaviour:

Return Types

The return data type is the data type of the date argument[…]

For example, if I pass in a datetime as argument, DATEADD returns a datetime. If I pass in a date, DATEADD returns a date.

The following example always returns datetime…

create function add_months(@dt date, @interval int) 
   returns datetime
as
begin
   return DATEADD(month, @interval, @dt)
end

How can I implement this in SQL Server?

(edit)

Context

I’m performing a database migration from informix to SQL Server. The database part is not the issue here, the code is. We have hundreds of programs that must be changed because of the SQL queries embedded in them. This is the main reason I’m trying to avoid to use DATEADD(MONTH, 1, foo). This automatic transformation, while simple in most cases, can be quite difficult in some cases. With a udf I could just replace the name of the informix function and not go into a deeper refactoring.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:19:48+00:00Added an answer on June 7, 2026 at 4:19 am

    Overloaded functions aren’t possible. I’m trying to think of a way to shoehorn SQL_VARIANT into this but all of the options I can think of lead to unnecessarily disgusting complications. I would make a function that accepts and returns date, and a function that accepts and returns datetime, and call the appropriate one. The ones with lower precision can call the ones with higher so you don’t have to replicate code, e.g.

    CREATE FUNCTION dbo.add_months_to_datetime
    (@dt DATETIME, @interval INT)
    RETURNS DATETIME
    AS
    BEGIN
      RETURN DATEADD(MONTH, @interval, @dt);
    END
    GO
    
    CREATE FUNCTION dbo.add_months_to_date
    (@dt DATE, @interval INT)
    RETURNS DATE
    AS
    BEGIN
      RETURN dbo.add_months_to_datetime(@dt, @interval);
    END
    GO
    

    Alternatively, just always use the highest precision (1st function), and worry about whether it’s a date or datetime when you present the data. You can do this with an inline convert or wrapping it in another function appropriately.

    EDIT

    Or better yet, just replace all calls to this function of questionable value with proper inline dateadd calls.

    dbo.add_months(foo, 1)
    

    Becomes:

    DATEADD(MONTH, 1, foo)
    

    While it’s not as automatic as you might like, in addition to maintaining your requirement that the output type remains the same as the input, this will also probably improve performance of some queries, depending on where these are used within the query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to transfer data daily from SQL Server (2008) to SQL Server (2005).
I am using SQL Server 2008 and I need to select all data from
I have a table on another SQL server which I need to copy from
I have this SQL Server CE database (with more tables and data) which I
In SQL Server I need to assign a rule with a column but this
In SQL Server (2005+) I need to index a column (exact matches only) that
I'm retrieving a signed int from a SQL Server database and need to convert
In SQL Server, I need to design a User table. It should have a
Need to query my SQL server from Access using an ADO connection (for example),
I need to move a database from SQL Server 2008 to 2005, did the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.