1

我有一个包含发票和创建日期时间的数据库,我想根据每张发票的日期创建一个名为月份的新列。我的意思是如果日期是 2013 年 1 月 15 日,我想在新专栏上有“一月”。在此先感谢,我对sql知之甚少。

4

4 回答 4

1

如果您的数据库是 MySQL,请尝试:

DATE_FORMAT(date, '%M')
于 2013-07-31T23:06:25.500 回答
1

对于 MS SQL Server 使用

SELECT DATENAME(MONTH,invoiceDate)
于 2013-07-31T23:08:07.090 回答
0

在甲骨文中:

TO_CHAR(date, 'Month')
于 2013-07-31T23:12:33.340 回答
0

从 datetime 对象中提取月份,然后在 CASE 语句中使用它。

以此为基础

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 

我建议复制您的表的架构,为该月添加另一列。然后使用以下语句。

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;

然后

drop table oldTable;

然后

Some alter to change the name of the new table to the name of the old table.
于 2013-07-31T23:10:21.070 回答