Skip to content
Software connecting all databases
  • Home
  • Software
    • Download Nexus
    • Purchase Nexus License
    • Activate Nexus License
    • SmartCompress
    • 60-Second Feature Videos
  • Training
    • Corporate Education
  • Data Migration
    • Data Migration Software
    • Data Migration Services
  • Books
  • Partners
  • Press
  • Blog
  • About Us
    • What We Do
    • Who We Are
  • Contact
Coffing Data Warehousing - Federated Query and ETL Software, Training, and Books
FacebookTwitterGoogleLinkedinYoutubeEmail
Software connecting all databases
  • Home
  • Software
    • Download Nexus
    • Purchase Nexus License
    • Activate Nexus License
    • SmartCompress
    • 60-Second Feature Videos
  • Training
    • Corporate Education
  • Data Migration
    • Data Migration Software
    • Data Migration Services
  • Books
  • Partners
  • Press
  • Blog
  • About Us
    • What We Do
    • Who We Are
  • Contact

Mastering Date Functions on SQL Server

April 10, 2024

You will love this blog because date functions and formatting differ dramatically between databases, but I have covered that with brilliant examples and clear and concise explanations below.

All of these examples have come from my books and training classes. Please do me a favor and tell your training coordinator that you know the best technical trainer in the world. Ask them to hire me to train at your company, either on-site or with a virtual class. They can see our classes, outlines, and a sample of my teaching at this link on our website.

https://coffingdw.com/education/

Below the video are great date functions on SQL Server. However, you can first witness a two-minute video that joins a Synapse table with tables from Databricks, Snowflake, Redshift, Google BigQuery, Vertica, Teradata, Oracle, SQL Server, DB2, Yellowbrick, MySQL, Postgres, Microsoft Access, Excel in a single federated query.

The Getdate command will return today’s date and time, just like the Current_Timestamp command. However, the Getdate function is not ANSI standard.

The DATEADD function adds or subtracts an interval from a date. Valid values for the part argument include the year, quarter, Month, dayofyear, day, week, weekday, hour, minute, second, millisecond, microsecond, nanosecond, TZoffset, and ISO_WEEK.

The Dateadd command adds a specified time interval to a date or timestamp value. Below we use examples of DAY, WEEK, MONTH, and QUARTER.

You can also use the DATEADD to add hours, minutes and seconds.

Below are some excellent examples to pull from using the DATEPART function.

The YEAR, MONTH, and DAY functions are abbreviations for the DATEPART function. 

The DATENAME function returns the name of the requested part rather than the number. Notice above that only the Month returns the actual name of the Month, but both the Year and the Day still return the integer values.

The syntax for the DATEDIFF function is DATEDIFF (part, dt_val1, dt_val2).  Below, we are using literal dates, but you can use column values.  We then can see the differences in the number of years, months, days, hours, minutes, and seconds.

The FORMAT  function returns a value formatted with the specified format and optional culture. For example, as we have done for the United States and Great Britain, you can use the FORMAT function for locale-aware formatting of date/time and number values as strings.

The FORMAT  function returns a value formatted with the specified format and optional culture. For example, as we have done for Germany and China, you can use the FORMAT function for locale-aware formatting of date/time and number values as strings.

The FORMAT  function returns a value formatted with the specified format and optional culture. For example, you can use the FORMAT function for locale-aware formatting of date/time and number values as strings.

You can also format time as in the example below.

The DATEFROMPARTS function returns a date value, with the date portion set to the specified year, month, and day and the time portion set to the default. For invalid arguments, DATEFROMPARTS kicks out an error. DATEFROMPARTS returns null if at least one required argument has a null value.

GETDATE(): Returns the current local date and time.
CURRENT_TIMESTAMP: Returns the current local date and time, following ANSI SQL standards.
GETUTCDATE(): Returns the current UTC date and time.

SYSDATETIME(): This function returns the current system date and time with high precision, including fractional seconds. It is similar to GETDATE() but provides a higher level of precision. The value returned is in the SQL Server’s local time zone.

SYSUTCDATETIME(): This function also returns the current system date and time with high precision, including fractional seconds, but in Coordinated Universal Time (UTC). Unlike SYSDATETIME() or GETDATE(), which return local time, SYSUTCDATETIME() ensures that the returned time is in UTC, which is useful for maintaining consistency across different systems or time zones.

SYSUTCDATETIME(): Returns the current UTC date and time with high precision.
SYSDATETIMEOFFSET(): Returns the current local date and time with the corresponding time zone offset from UTC.

EOMONTH finds the end of the month for a given date. It’s useful for various reporting and analysis tasks where you need to reference the end of a month. The function takes two parameters: the input date and an optional parameter indicating the number of months to add or subtract from the input date before calculating the end of the month. If the second parameter is omitted or set to 0, it returns the end of the current month. If it’s positive, it calculates the end of the month for a future date, and if it’s negative, it calculates the end of the month for a past date.

Posted in Blog
← Mastering Netezza Date FunctionsDate Functions and Formatting for all Major Database Systems →

Latest Posts

  • Mastering Derived Tables in Greenplum September 19, 2025
  • Mastering Derived Tables in Vertica September 19, 2025
  • Mastering Derived Tables in Yellowbrick September 19, 2025
  • Mastering Derived Tables in Oracle September 19, 2025
  • Mastering Derived Tables in MySQL July 16, 2025
  • Mastering Derived Tables in SQL Server July 15, 2025
  • Mastering Derived Tables in DB2 July 15, 2025
  • Mastering Derived Tables in Postgres July 15, 2025

From The Blog

  • Mastering Derived Tables in Greenplum
  • Mastering Derived Tables in Vertica
  • Mastering Derived Tables in Yellowbrick
  • Mastering Derived Tables in Oracle
  • Mastering Derived Tables in MySQL
Copyright © 2024 · Coffing DW
All Logos & Trademarks Belong To Their Respective Owners
Facebook Twitter Google Linkedin Youtube Email