1

i've got a DB with invoices and datetime that they were created, i want to have a New columm with the name of The month according to The date of each invoice. I mean if The date is 2013-01-15, i would like to have " january" on The New columm. Thanks in advance, i've few knowledge about sql.

user2640011
  • 21
  • 1
  • 1
  • 2
  • 2
    You must specify the RDBMS – Ernesto Campohermoso Jul 31 '13 at 23:03
  • possible duplicate of [Get month from DATETIME in sqlite](http://stackoverflow.com/questions/650480/get-month-from-datetime-in-sqlite) – JustinDanielson Jul 31 '13 at 23:07
  • 1
    Be sure to do a search before posting. A quick search for: "Sql query get month name from datetime" turns up many promising results, such as: [Returning month name in sql server query](http://stackoverflow.com/questions/5650830/returning-month-name-in-sql-server-query) . Obviously include your database type to narrow the results. – Leigh Jul 31 '13 at 23:13

4 Answers4

1

If your database is MySQL, try:

DATE_FORMAT(date, '%M')
Alex
  • 11,451
  • 6
  • 37
  • 52
1

For MS SQL Server use

SELECT DATENAME(MONTH,invoiceDate)
D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

Extract the month from the datetime object and then use it in a CASE statement.

Build upon this

select 
  case strftime('%m', date('now')) 
    when '01' then 'January' 
    when '02' then 'Febuary' 
    when '03' then 'March' 
    when '04' then 'April' 
    when '05' then 'May' 
    when '06' then 'June' 
    when '07' then 'July' 
    when '08' then 'August' 
    when '09' then 'September' 
    when '10' then 'October' 
    when '11' then 'November' 
    when '12' then 'December' else '' end
  as month 

I would suggest copying the schema of your table, adding another column for the month. Then using the following statement.

INSERT INTO TABLE newTable (col1, col2, col3, ..., colLast, colMonth)
SELECT col1, col2, col3, ..., colLast, 
      case strftime('%m', date('now')) 
        when '01' then 'January' 
        when '02' then 'Febuary' 
        when '03' then 'March' 
        when '04' then 'April' 
        when '05' then 'May' 
        when '06' then 'June' 
        when '07' then 'July' 
        when '08' then 'August' 
        when '09' then 'September' 
        when '10' then 'October' 
        when '11' then 'November' 
        when '12' then 'December' else '' end
      as colMonth
FROM oldTable;

Then

drop table oldTable;

Then

Some alter to change the name of the new table to the name of the old table.
JustinDanielson
  • 3,155
  • 1
  • 19
  • 26
0

In Oracle:

TO_CHAR(date, 'Month')
PM 77-1
  • 12,933
  • 21
  • 68
  • 111