Tip of the Day : Convert Oracle Date Functions to SQL Server Date Functions

Welcome to SQL Server Helper !!!

This site is intended for those who are beginning to use SQL Server as part of their day-to-day activities.  You will find in this site a collection of useful functions, triggers, stored procedures and tips and tricks related to SQL Server.

Should you have any comments or questions regarding this site or if you want to ask SQL Server-related questions, e-mail us here.

We hope we are able to help and we are glad to help!!!

SQL Server Tip of the Day - December 16, 2018

Convert Oracle Date Functions to SQL Server Date Functions

Function

Oracle

SQL Server

Date addition

p1 + p2

DATEADD(DD, p2, p1)

Date subtraction

p1 – p2

DATEDIFF(DD, p2, p1)

Add Months

ADD_MONTHS (p1, p2)

DATEADD(MM, p2, p1)

Last day of the month

LAST_DAY(p1)

SELECT DATEADD(DD, -1, DATEADD(MM, 1, DATEADD(MM, DATEDIFF(MM, 0, p1), 0)))

Time zone conversion

NEW_TIME(p1, p2, p3)

Not Available

First Weekday After Date

NEXT_DAY(p1, p2)

Not Available

Convert Date to String

TO_CHAR(p1, p2)

DATENAME

Convert String to Date

TO_DATE(p1)

CAST(p1 AS DATETIME)

CONVERT(DATETIME, p1)

Get Current Date and Time

SYSDATE

CURRENT_DATE

SYSTIMESTAMP

LOCALTIMESTAMP

CURRENT_TIMESTAMP

GETDATE()

SYSDATETIME()

SYSDATETIMEOFFSET()

Convert Date/Time to Date/Time with Time Zone

FROM_TZ(p1, p2)

TODATETIMEOFFSET(p1, p2)

Get Number of Months Between 2 Dates

MONTHS_BETWEEN(p1, p2)

DATEDIFF( MONTH, CAST(p2 AS float), CAST( DATEADD(DAY, ( -CAST(DATEPART(DAY, p2) AS float(53)) + 1 ), p1) AS float))

Round a Date to a Specific Unit of Measure

ROUND(p1)

ROUND(p1, p2)

Not Available

Truncate a Date to a Specific Unit of Measure

TRUNC(p1)

SELECT DATEADD(DD, DATEDIFF(DD, 0, p1), 0)

Truncate Date to a Specific Unit of Measure

TRUNC(p1, p2)

SELECT DATEADD(p2, DATEDIFF(p2, 0, p1), 0)

Time Zone Offset

TZ_OFFSET(p1)

Not Available

Session Time Zone

SESSIONTIMEZONE

Not Available

Database Time Zone

DBTIMEZONE

Not Available

SQL Server 2012

SQL Server 2008

User-Defined Functions

Date Functions

A collection of useful user-defined functions that deal with dates.

String Functions

A collection of useful user-defined functions that deal with strings (varchar/char/nvarchar/nchar).

Tree Functions

A collection of useful user-defined functions that deal with tree or hierarchical design structures.

Table-Valued Functions

A collection of useful table-valued user-defined functions that can be used to join with other tables.

SQL Server Built-in Functions

A reference to all built-in functions available within SQL Server grouped into categories.

Tips and Tricks

A collection of useful SQL Server-related tips and tricks:

SQL Server Error Messages

A list of SQL Server error messages and for certain error messages, discusses ways on how to solve the error or work around them:

Frequently Asked Questions