0

我有一个这样的号码:840106

我需要执行以下操作:

  1. 将数字更改为日期添加 - 并翻转数字:06-01-84
  2. 将 28 添加到日期的最后 2 位数字:06-01-12

84 + 16 = 00 + 12 = 12

数字总是在变化,有时它是 850617 ,但格式总是相同的添加 - 并添加 28 最后 2 位数字。

有什么想法可以在这里帮助我吗?

4

4 回答 4

1

这是一个sqlite解决方案:

create table t( c text);

insert into t (c) values(990831);
insert into t (c) values(840106);
insert into t (c) values(800315);
insert into t (c) values(750527);
insert into t (c) values(700923);
insert into t (c) values(620308);

select c, substr(c,5,2) || '-' || substr(c,3,2) || '-' || 
  case when (substr(c,1,2) + 28) < 100 then (substr(c,1,2) + 28) 
       else case when ((substr(c,1,2) + 28) - 100) < 10 then '0' || ((substr(c,1,2) + 28) - 100) 
         else ((substr(c,1,2) + 28) - 100) 
        end
  end
from t;
于 2013-07-11T16:18:35.040 回答
0

这是一个 t-sql 解决方案,您可以使用并迁移到 mysql。

declare @myDate as char(8) = '840106';
declare @y as char(2), @m as char(2), @d as char(2);

set @y = LEFT (@myDate, 2);
declare @yi as int = Convert (int, @y);

IF @y between 30 and 99 ----------- pick a cut-off year 
    SET @yi = (28 - (100-@yi));
SET @y = CONVERT(char, @yi)

set @m = SUBSTRING(@myDate, 3, 2);
set @d = SUBSTRING(@myDate, 5, 2);
SET @myDate = @d + '-' + @m + '-' + @y;
PRINT @myDate;
于 2013-07-11T07:32:55.537 回答
0

假设 date 是包含您的日期的列的名称:

DATE_FORMAT(DATE_ADD(STR_TO_DATE(date, %y%m%d), INTERVAL 28 YEAR), %d-%m-%y);

这样做是首先将字符串格式化为日期,然后添加 28 年,然后转换回具有新格式的字符串。

SQLite 在这方面有很多技巧,你需要使用子字符串。

substr(date,5) || "-" || substr(date,3,4)  || "-" || CAST(CAST(substr(date,1,2) as integer) + 28 - 100) as text

我对 SQLite 不太熟悉,所以选角可能有点奇怪。

于 2013-07-11T07:04:10.650 回答
0

对于格式化,您可以使用 http://www.w3schools.com/sql/func_date_format.asp

要为日期添加天数,您应该查看 date_add() 函数

mysql> SELECT DATE_ADD('1998-01-02', INTERVAL 31 DAY);

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add

于 2013-07-11T06:56:45.763 回答