【问题标题】:Update to use 1st of a given Month更新以使用给定月份的第一天
【发布时间】:2018-05-08 15:13:00
【问题描述】:

我没有任何示例 SQL,但使用 Advantage Architect。我有一张桌子,上面有年份(例如 2017 年)和月份(例如 04)。 我想更新另一个表(在我已经确定的字段上使用内部连接,所以不要担心那个位)为 dd/mm/yyyy 格式的日期,并且每一天都为 01使用的日期,使用上面提到的另一个表中的月份和年份。

【问题讨论】:

  • 请向我们展示您的示例数据,并让我们知道您使用的是什么 RDBM
  • 考虑发布表格的 DDL。
  • 好的,所以我正在使用 Advantage Architect SQL,我已将 table1 中的两列(CalendarYear、CalendarMonth)放在下面,我需要使用它来插入或更新另一个表(table2)(有效),这是一个日期字段,但要添加到这个01上的每一天。连接来自 table1 中与 table2 相同的列,但我无法在此处显示,因为它是敏感信息。

标签: sql advantage-database-server


【解决方案1】:

ADS 提供的日期处理功能有些有限。最容易使用的日期格式似乎是 YYYY-MM-DD,但是从您的列转换为您要查找的日期有点复杂。

这是一个可以帮助您解决问题的示例。

create table #temp (calyear numeric(4, 0), calmonth numeric(2,0));
insert into #temp (calyear, calmonth) values (2017, 10);
insert into #temp (calyear, calmonth) values (2017, 7);

/* 
  Start convoluted workaround here. Cast the result of the concatenation below 
  (which creates a string in the format 'YYYY-MM-DD') into a date value
*/
select cast(txtdate as SQL_DATE) as newdate from 
(
  /* 
     Concatenate year, month (adding leading zero if needed to make two-digit)
     and month
  */
  select 
    trim(cast(calyear as SQL_Char)) + '-' + 
    right('00' + trim(cast(calmonth as SQL_Char)), 2) + 
    '-01' as txtdate from #temp
) t;

【讨论】:

    猜你喜欢
    • 2011-11-24
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    • 2022-01-04
    • 2020-12-08
    • 1970-01-01
    相关资源
    最近更新 更多