Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Friday, 16 February 2024

ADD MONTHS in MSSQL

In Microsoft SQL Server (MSSQL), there isn't a built-in ADD MONTHS function like in some other databases. However, you can achieve the same functionality using the DATEADD() function along with some arithmetic on the number of months.

Here's how you can add months to a date in MSSQL:

SELECT DATEADD(MONTH, 3, '2022-01-15') AS new_date;

This query will add 3 months to the date '2022-01-15', resulting in the date '2022-04-15'.

You can also use a negative value to subtract months:

SELECT DATEADD(MONTH, -2, '2022-01-15') AS new_date;

This query will subtract 2 months from the date '2022-01-15', resulting in the date '2021-11-15'.

So, to add or subtract months in MSSQL, you use DATEADD() with the appropriate positive or negative number of months.

No comments:

Post a Comment

Please provide your feedback in the comments section above. Please don't forget to follow.