Home > Database > Mysql Tutorial > How Can I Custom Format Date and Time in SQL Server to Display DDMMM and HHMMT?

How Can I Custom Format Date and Time in SQL Server to Display DDMMM and HHMMT?

Barbara Streisand
Release: 2024-12-29 20:26:16
Original
152 people have browsed it

How Can I Custom Format Date and Time in SQL Server to Display DDMMM and HHMMT?

Custom Date/Time Formatting in SQL Server

Customizing the display format of dates and times in SQL Server is essential for presenting data in a user-friendly manner. This article will provide a comprehensive solution to the challenge of formatting dates and times in a specific format, as outlined in the original question.

Problem Statement:

The task at hand involves extracting two additional columns from an existing table's Datetime field. These columns should be formatted as follows:

  1. DDMMM: Represent the day and month in a shorthand notation, e.g., "12OCT" for October 12th.
  2. HHMMT: Display the time in hours and minutes, with an AM/PM indicator, e.g., "0119P" for 1:19 PM.

Solution Using Functions:

Instead of using a stored procedure, it is recommended to use a function to wrap the logic for this date formatting. Here's the code:

CREATE FUNCTION [dbo].[CustomDateFormat](
    @DateTime DATETIME
)
RETURNS VARCHAR(10)
AS
BEGIN
    DECLARE @Day VARCHAR(2) = SUBSTRING(DATENAME(DAY, @DateTime), 1, 2)
    DECLARE @Month VARCHAR(3) = SUBSTRING(UPPER(DATENAME(MONTH, @DateTime)), 1, 3)
    
    RETURN @Day + @Month
END
Copy after login

This function extracts the day ('DD') and month ('MMM') parts of the input Datetime value and returns them concatenated.

Accessing Data in Dynamic SQL:

To access the data using the function, create a dynamic SQL query:

DECLARE @SQLCommand VARCHAR(MAX) = '
    SELECT
        ...,
        dbo.CustomDateFormat(DateTimeField) AS DDMMM,
        ...
' 
EXEC (@SQLCommand)
Copy after login

Optimization Tip:

To avoid performance penalties from data type conversions, avoid using character/string operations on dates whenever possible. Dates in MSSQL are represented as floating-point numbers, and conversions to/from strings can slow down performance.

Additional Resources:

For further reference, here are some additional helper functions for working with dates and times in SQL Server:

  • Getting Parts of a DateTime
  • Relative Dates

The above is the detailed content of How Can I Custom Format Date and Time in SQL Server to Display DDMMM and HHMMT?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template