Free cookie consent management tool by TermsFeed Policy Generator
  • Azure SQL
  •    1 page views
  •   0 video views
  • 2024, April 05, Friday

#065 Get specific part of a date in Azure SQL

Como obter parte especifica de uma data em Consulta (SELECT) existente na linguagem de Manipulação de Dados (DML) do Azure SQL/SQL Server

This T-SQL code uses the DATEPART function to extract information specific to the current date and time, which is obtained by the GETDATE() function. I'll explain each part of the code:

SELECT 
  DATEPART(YEAR, GETDATE()) AS CurrentYear,
  DATEPART(MONTH, GETDATE()) AS CurrentMonth,
  DATEPART(DAY, GETDATE()) AS CurrentDay
DATEPART(
  1. YEAR, GETDATE()) AS CurrentYear:

    • DATEPART: Function that returns a specific part of a date and time.
    • YEAR: Specifies that the part of the date that will be returned is the year.
    • GETDATE(): Function that returns the current date and time.
    • AS CurrentYear
    • : Renames the result to "CurrentYear".
  2. DATEPART(MONTH, GETDATE()) AS CurrentMonth

    :
    • MONTH: Specifies that the part of the date that will be returned is the month.
    • AS CurrentMonth
    • : Renames the result to "CurrentMonth".
  3. DATEPART(DAY, GETDATE()) AS CurrentDay

    :
    • DAY: Specifies that the part of the date that will be returned is the day.
    • ASCurrentDay: Renames the result to "CurrentDay".
Therefore, the

code returns three columns: "CurrentYear", "CurrentMonth", and "CurrentDay", which represent, respectively, the year, month, and day of the current date and time. This query is useful when you need to extract specific date and time information in SQL Server.

This content contains
  • Content Imagem
  • Language Portuguese
  • Duration
  • Subtitles Não

  • Reading time 1 min 0 seg

avatar
Fabio Santos

Data Scientist and Consultant for Digital and Analytics Solutions


  • Share

Youtube Channel

@fabioms

Subscribe now