How to get the month and current year in SQL?

Asked

Viewed 9,972 times

3

How can I print the current month and year on the screen in sql ? I need the month to be extended. I’m using the query below.

select GETDATE() from DUAL

Currently it is being printed as follows.

2018-08-21 10:34:03.253

I need it printed in the form below:

August - 2018

  • 1

    What I found on GETDATE() was this link: http://www.sqlympho.net/sqlserver/sql_server_SELECT-Formatting_Date_Time.php

4 answers

5


  • 2

    Datename is affected by the sql configuration, if it is in.English, the names will come in English

4

Try it like this:

    select 
    case month(getdate())
        when  1 then 'Janeiro'
        when  2 then 'Fevereiro'
        when  3 then 'Março'
        when  4 then 'Abril'
        when  5 then 'Maio'
        when  6 then 'Junho'
        when  7 then 'Julho'
        when  8 then 'Agosto'
        when  9 then 'Setembro'
        when 10 then 'Outubro'
        when 11 then 'Novembro'
        when 12 then 'Dezembro'
    end + ' - ' + cast(year(getdate()) as varchar(4))
 As MesAno

2

Script to return a full date.

SET LANGUAGE Português

SELECT DATENAME(weekday, GetDate()) + ', '   +
       DATENAME(day, GetDate())     + ' de ' +
       DATENAME(month, GetDate())   + ' de ' +
       DATENAME(year, GetDate())

Remember to use the SET LANGUAGE to return the date in the desired language, to check the default language of your run session DBCC UserOptions

To view what is the name of existing languages in SQL check in the table name column sysLanguages.

select * from master.dbo.syslanguages

Note: Avoid using SET LANGUAGE within procedures as this will cause RECOMPILE.

  • The quotes you used in the example are invalidating your syntax.

-1

   select convert(char(02),(getdate()),101)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.