How do I get the current month and year in SQL?

3

How can I print on the screen the current month and year in sql? I need the month to come out in full. I'm using the query below.

select GETDATE() from DUAL

It is currently being printed as follows.

2018-08-21 10: 34: 03.253

I need it printed as follows:

August - 2018

    
asked by anonymous 21.08.2018 / 15:38

3 answers

4

Try 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
    
21.08.2018 / 15:50
4

Use DATENAME . See example

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

Note: DATENAME applies to: SQL Server (SQL Server 2008 up to the current version), Database   Windows Azure SQL data (initial release to current version).   Source: link

    
21.08.2018 / 15:52
1

Script to return a date in full.

SET LANGUAGE Português

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

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

To see what names of languages exist in SQL, check the name column of the sysLanguages table.

select * from master.dbo.syslanguages

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

    
21.08.2018 / 15:51