Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • SQL Server - Get Started
  • Install SQL Server
  • SQL Server Management Studio
  • SQL Server - Windows Authentication
  • SQL Server - Authentication
  • SQL Server - Create New User
  • SQL Server - GRANT/REVOKE Permissions to User
  • SQL Server - Data Types
  • SQL Server - Naming Conventions
  • SQL Server - CREATE Database
  • SQL Server - CREATE Table
  • Add Columns
  • Identity Column
  • Rename Column, Table
  • Drop Columns
  • SQL Server - Schema
  • SQL Server - Tables Relations
  • SQL Server - Primary Keys
  • Modify/Delete Primary Keys
  • SQL Server - Foreign Keys
  • Modify/Delete Foreign Keys
  • SQL Server - Check Constraints
  • SQL Server - Unique Constraints
  • SQL Server - Views
  • Modify/Delete Views
  • SQL Server - Functions
  • SQL Server - Stored Procedures
  • Stored Procedure Parameters
  • SQL Server - Indexes
  • Non-clustered Indexes
  • Modify/Delete Indexes
  • SQL Server - Triggers
  • DDL Triggers
  • LOGON Triggers
  • Enable/Disable Triggers
  • Modify/Delete Triggers
  • SQL Server - Sequence
  • SQL Server - Synonyms
  • SQL Server - IF ELSE Statement
  • SQL Server - Loops
  • SQL Server - Insert Data
  • SQL Server - Update Data
  • SQL Server - Delete Data
  • SQL Server - Select Query
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • SQL Server - Inner Join
  • Left Join
  • Right Join
  • Full Join
  • Self Join
  • Dynamic SQL
  • Built-in Functions
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

SQL Server DATEADD() Function

In SQL Server, the DATEADD() function adds a number to a datepart and returns the modified datetime value.

Syntax:

DATEADD(datepart, number, date)

Parameters

datepart: The specific part of the specified date parameter to which the DATEADD() function will add a number. The following table lists all valid datepart values:

DatepartAbbreviation
yearyy, yyyy
quarterqq, q
monthmm, m
day of yeardy, y
daydd, d
weekwk, ww
weekdaydw, w
hourhh
minutemi, n
secondss, s
millisecondms
microsecondmcs
nanosecondns

Note: dayofyear, day, and weekday return the same value.

number: An integer that is to be added to the specified datepart. It can be an expression that returns an integer. The number will be truncated if it has a decimal fraction.

Note: DATEADD() function returns an error if the number argument exceeds the range of int.

date: A datetime value or an expression that resolves to a datetime value. It can be a string expression, a table column, or a variable. A string literal value must resolve to type DATE, DATETIME, DATETIMEOFFSET, DATETIME2, SMALLATETIME, or TIME

Return Value

The return type depends on the argument supplied for the date parameter.

If the value of the date is a string literal date value, then DATEADD() returns a datetime value. If any other valid data type is supplied for the date column, then the same data type is returned from DATEADD() function.

Add Days in DateTime

The following example adds 1 day to the specified date '12/31/2021' (31st December 2021).

Example: Add Days
SELECT DATEADD(day,1,'12/31/2021') AS Result

In the above example, 'day' is the datepart that specifies that a number should be added to a day part of the specified date '12/31/2021'. The second parameter is a number that should be added, and the third parameter is a date where the specified parameter should be added.

Add Months in DateTime

In this example, for the month datepart, 2 is added to date '12/31/2021'. As you can see in the result, two months is added to the input date and the DATEADD() function returns 28th February 2022, which is the last day of the returned date.

Example: Add Months
SELECT DATEADD(month, 2, '12/31/2021') AS Result

You can specify negative number to be added in the date which will deduct a number from the specified datetime value.

Example: Add Negative Number to Date
SELECT DATEADD(month, -2, '12/31/2021') AS Result

Use DATEADD() with Columns

The following DATEADD() function is used with the HireDate column of the Employee table to return the JoinDate. Two weeks is added to the HireDate column to return JoinDate.

Example: DATEADD() with Column
SELECT EmployeeID, FirstName, HireDate, DATEADD(wk, 2, HireDate) AS JoinDate 
FROM Employee

Make sure the specified date is in the correct format; otherwise it will raise an error "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value."

Example: Wrong Date Format
SELECT DATEADD(month, -2, '12/31/2021 123') AS Result
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.