Skip to main content

DATEADD - SQL Server

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

The DATEADD function simply allows you to add or subtract the specified number of units of time to a specified date/time value.

Syntax
DATEADD (datepart , number , date)

Arguments
datepart - The part of date to which DATEADD adds an integer number. This table lists all valid datepart arguments.
  • datepart - Abbreviations
  • year - yy, yyyy
  • quarter - qq, q
  • month - mm, m
  • dayofyear - dy, y
  • day - dd, d
  • week - wk, ww
  • weekday - dw, w
  • hour - hh
  • minute - mi, n
  • second - ss, s
  • millisecond - ms
  • microsecond - mcs
  • nanosecond - ns
number - An expression that can resolve to an int that DATEADD adds to a datepart of date. DATEADD accepts user-defined variable values for number. DATEADD will truncate a specified number value that has a decimal fraction. It will not round the number value in this situation.

date - An expression that can resolve to one of the values like date, datetime, datetimeoffset, datetime2, smalldatetime, time.

Example
-- to add 5 days to September 1, 2020 the function would be
DATEADD(DAY, 5, '9/1/2020')

-- to subtract 5 months from September 1, 2020 the function would be
DATEADD(MONTH, -5, '9/1/2020')

DATEADD() Function and Examples
  • Add 30 days to a date SELECT DATEADD(DD,30,@Date)
  • Add 3 hours to a date SELECT DATEADD(HOUR,-3,@Date)
  • Subtract 90 minutes from date SELECT DATEADD(MINUTE,-90,@Date)
  • Check out the chart to get a list of all options.

Popular posts from this blog

What is SQL Server?

SQL Server SQL Server is a relational database management system (RDBMS) developed by Microsoft. SQL Server supports a wide variety of transaction processing, business intelligence and analytics applications in corporate IT environments. SQL Server supports ANSI SQL, which is the standard SQL (Structured Query Language) language. However, SQL Server comes with its own implementation of the SQL language, T-SQL. Why SQL Server? Microsoft SQL Server is a comprehensive database server and information platform offering a complete set of enterprise-ready technologies and tools that help people derive the most value from information at the lowest total-cost-of-ownership. Enjoy high levels of performance, availability, and security; employ more productive management and development tools; and deliver pervasive insight with self-service business intelligence (BI). A complete and integrated platform, Microsoft SQL Server brings it all together to get more value out of existing IT skills and asse

Download SQL Server Management Studio (SSMS)

Summary:  In this post, you will learn &  able to download  SSMS. About  SQL Server Management Studio ( SSMS ):  SSMS is Free!! SQL Server Management Studio (SSMS) is an integrated environment for managing any SQL infrastructure, from SQL Server to Azure SQL Database. SSMS provides tools to configure, monitor, and administer instances of SQL Server and databases. Use SSMS to deploy, monitor, and upgrade the data-tier components used by your applications, and build queries and scripts. Use SSMS to query, design and manage your databases and data warehouses, wherever they are - on your local computer, or in the cloud.   SSMS -  Prerequisites for Installation: SSMS supports the following 64-bit platforms when used with the latest available service pack: Supported Operating Systems: Windows 10 (64-bit) version 1607 (10.0.14393) or later Windows 8.1 (64-bit) Windows Server 2019 (64-bit) Windows Server 2016 (64-bit) Windows Server 2012 R2 (64-bit) Windows Server 2012 (64-bit) Windows Ser

Mastering Common Table Expression or CTE in SQL Server

WITH common_table_expression (Transact-SQL) Summary:  From this post you will learn about Common Table Expression or   CTE  in SQL Server using the with Clause.  About  CTE  Statement:  Specifies a temporary named result set, known as a common table expression (CTE). This is derived from a simple query and defined within the execution scope of a single SELECT , INSERT , UPDATE , DELETE or MERGE statement. A common table expression can include references to itself. This is referred to as a recursive common table expression.  Syntax: [ WITH <common_table_expression> [ ,...n ] ]   <common_table_expression>::=       expression_name [ ( column_name [ ,...n ] ) ]       AS       ( CTE_query_definition )   Arguments expression_name Is a valid identifier for the common table expression. expression_name must be different from the name of any other common table expression defined in the same WITH <common_table_expression> clause, but expression_name can be the same as the name