How to retreive only month and year from datetime colomn

  • Hi,

    How to retreive only month and year from datetime colomn. (dd/mm/yy mm:ss:xx)

    any help or any comment will be highly appreciated.

  • SELECT YEAR( GETDATE() ) AS [Year], MONTH( GETDATE() ) AS [Month]

    --Ramesh


  • All of these get just the month / year in different ways, and with different formatting:

    declare @Date datetime = GetDate()

    select MONTH(@date), YEAR(@date)

    select DATEPART(month, @Date), DATEPART(year, @Date)

    select CONVERT(char(6), @Date, 112)

    select CONVERT(char(7), @Date, 102)

    select RIGHT(convert(char(10),@Date, 103), 7)

    select RIGHT(convert(char(10),@Date, 104), 7)

    select RIGHT(convert(char(10),@Date, 105), 7)

    select LEFT(convert(char(10), @Date, 111), 7)

    select LEFT(convert(char(10), @Date, 120), 7)

    select LEFT(convert(char(10), @Date, 121), 7)

    select LEFT(convert(char(10), @Date, 126), 7)

    select LEFT(convert(char(10), @Date, 127), 7)

    select LEFT(convert(char(10), @Date, 112), 6)

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • Hi,

    great thanks for your help.

    It works perfectly:cool::cool::cool:

    Kind regards.

  • Hi,

    great thanks for your help.

    It works perfectly:cool::cool::cool:

    Kind regards.

  • Hi,

    great thanks for your help.

    It works perfectly:cool::cool::cool:

    Kind regards.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply