DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Goose Migrations for Smooth Database Changes
  • Top 5 Key Features of Apache Iceberg for Modern Data Lakes
  • Workarounds for Oracle Restrictions on the Size of Expression Lists
  • SQL Server to Postgres Database Migration

Trending

  • Testing SingleStore's MCP Server
  • Simplify Authorization in Ruby on Rails With the Power of Pundit Gem
  • Scalability 101: How to Build, Measure, and Improve It
  • Comparing SaaS vs. PaaS for Kafka and Flink Data Streaming
  1. DZone
  2. Data Engineering
  3. Data
  4. Dates and Times in SQL Server: DATENAME()

Dates and Times in SQL Server: DATENAME()

Read here to learn more about dates and times in SQL server.

By 
Randolph West user avatar
Randolph West
·
Jun. 26, 18 · Analysis
Likes (2)
Comment
Save
Tweet
Share
4.0K Views

Join the DZone community and get the full member experience.

Join For Free

Last time, we looked at DATEPART(). This post is all about the DATENAME() function.

So Many Similarities

There are many similarities between DATEPART and DATENAME. Where DATEPART returns the date or time part as an integer, DATENAME returns the part as a character string.

This DATENAME function also takes two parameters: the date or time part we want back and the input date. Just as we saw with   DATEPART , the documentation indicates the input date parameter must be an "expression that can resolve to one of the following data types: date, smalldatetime, datetime, datetime2, datetimeoffset, or time."

Similarly, the date and time parts that can be returned look much like those in DATEPART, which gives us another opportunity for the reminder that we should avoid using the available abbreviations in order to help with writing clearly understandable code.

datepart Abbreviations
year yy, yyyy
quarter qq, q
month mm, m
dayofyear dy, y
day dd, d
week wk, ww
weekday dw
hour hh
minute mi, n
second ss, s
millisecond ms
microsecond mcs
nanosecond ns
TZoffset tz
ISO_WEEK isowk, isoww

Settings and Formatting

The  LANGUAGE , DATEFORMAT , and DATEFIRST settings will affect the output, just as with DATEPART.

Speaking of date formatting (which as we've learned previously is easy to get wrong), the documentation addresses the Year-Day-Month format:

In SQL Server 2017, DATENAME implicitly casts string literals as a datetime2 type. In other words, DATENAME does not support the format YDM when the date is passed as a string. You must explicitly cast the string to a datetime or smalldatetime type to use the YDM format.

This is a good thing for consistency across regions. Enforcing the use of the DATETIME2 data type removes ambiguity.

Example code and output

Using the built-in function SYSUTCDATETIME() to get the current date and time in UTC format as a DATETIME2(7) data type, we can get the following output for each date or time part:

DECLARE @dt DATETIME2(7) = SYSUTCDATETIME();
 SELECT @dt AS [Current UTC Date and Time]
SELECT DATENAME(YEAR, @dt) AS [Year];
SELECT DATENAME(QUARTER, @dt) AS [Quarter];
SELECT DATENAME(MONTH, @dt) AS [Month];
SELECT DATENAME(DAYOFYEAR, @dt) AS [Day Of Year];
SELECT DATENAME(DAY, @dt) AS [Day];
SELECT DATENAME(WEEK, @dt) AS [Week];
SELECT DATENAME(WEEKDAY, @dt) AS [Weekday];
SELECT DATENAME(HOUR, @dt) AS [Hour];
SELECT DATENAME(MINUTE, @dt) AS [Minute];
SELECT DATENAME(SECOND, @dt) AS [Second];
SELECT DATENAME(MILLISECOND, @dt) AS [Millisecond];
SELECT DATENAME(MICROSECOND, @dt) AS [Microsecond];
SELECT DATENAME(NANOSECOND, @dt) AS [Nanosecond];
SELECT DATENAME(TZOFFSET, @dt) AS [TZ Offset];
 SELECT DATENAME(ISO_WEEK, @dt) AS [ISO Week];

And here is the output:

Remember that all of these values are being returned as strings. We should keep this in mind if we want to manipulate the values.

Share your favorite date or time part with me in the comments.

Data Types sql

Published at DZone with permission of Randolph West, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Goose Migrations for Smooth Database Changes
  • Top 5 Key Features of Apache Iceberg for Modern Data Lakes
  • Workarounds for Oracle Restrictions on the Size of Expression Lists
  • SQL Server to Postgres Database Migration

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!