如何在Oracle数据库中将原始日期转换为标准日期格式

时间:2018-06-02 20:00:13

标签: sql oracle date sql-date-functions

在Oracle中,我的日期格式为2018-06-02T17:31:55.461Z,在varchar字段中。如何将其转换为Oracle db中的标准日期字段?

double frand() {
    return 2*((rand()/(double)RAND_MAX) - 0.5);
}

int main() {

    //initial values for the kalman filter
    float x_est_last = 0;
    float P_last = 0;
    //the noise in the system
    float Q = 0.022;
    float R = 0.617;

    float K;
    float P;
    float P_temp;
    float x_temp_est;
    float x_est;
    float z_measured; //the 'noisy' value we measured
    float z_real = 0.5; //the ideal value we wish to measure

    srand(0);

    //initialize with a measurement
    x_est_last = z_real + frand()*0.09;

    float sum_error_kalman = 0;
    float sum_error_measure = 0;

    for (int i=0;i<30;i++) {
        //do a prediction
        x_temp_est = x_est_last;
        P_temp = P_last + Q;
        //calculate the Kalman gain
        K = P_temp * (1.0/(P_temp + R));
        //measure
        z_measured = z_real + frand()*0.09; //the real measurement plus noise
        //correct
        x_est = x_temp_est + K * (z_measured - x_temp_est); 
        P = (1- K) * P_temp;
        //we have our new system

        printf("Ideal    position: %6.3f \n",z_real);
        printf("Mesaured position: %6.3f [diff:%.3f]\n",z_measured,fabs(z_real-z_measured));
        printf("Kalman   position: %6.3f [diff:%.3f]\n",x_est,fabs(z_real - x_est));

        sum_error_kalman += fabs(z_real - x_est);
        sum_error_measure += fabs(z_real-z_measured);

        //update our last's
        P_last = P;
        x_est_last = x_est;
    }

    printf("Total error if using raw measured:  %f\n",sum_error_measure);
    printf("Total error if using kalman filter: %f\n",sum_error_kalman);
    printf("Reduction in error: %d%% \n",100-(int)((sum_error_kalman/sum_error_measure)*100));


    return 0;
}

4 个答案:

答案 0 :(得分:2)

您似乎拥有的是标准JavaScript时间戳。在这种情况下,您要做的是将它们转换为Oracle TIMESTAMP值,然后可能转换为Oracle DATE值。

将文本转换为TIMESTAMP的格式掩码如下:

YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"

TZ用双引号"括起来;它们与Oracle无关,根本不需要。您使用FF3来获取小数秒,因为有3位精度。总而言之,你得到:

SELECT TO_TIMESTAMP(raw_date, 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')
  FROM testmp1;

INSERT INTO testmp2
  ( new_date )
SELECT CAST(TO_TIMESTAMP(raw_date, 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"') AS DATE)
  FROM testmp1;
编辑:@ Egret的评论让我想到了时区。时间戳中的Z表示零偏移或UTC时间;要将其转换为您当地的时间,您将要执行以下操作:

SELECT TO_TIMESTAMP(raw_date, 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"') - (SYS_EXTRACT_UTC(CURRENT_TIMESTAMP) - CURRENT_TIMESTAMP)
  FROM testmp1;

如果转换为DATE

SELECT CAST(TO_TIMESTAMP(raw_date, 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"') - (SYS_EXTRACT_UTC(CURRENT_TIMESTAMP) - CURRENT_TIMESTAMP) AS DATE)
  FROM testmp1;

希望这有帮助。

答案 1 :(得分:0)

在日期和时间中拆分字符串,然后将其转换为日期,例如:

select to_date(substr('2015-04-29T19:51:30.0Z', 1, 10) ||
    substr('2015-04-29T19:51:30.0Z', 12, 8),
    'YYYY-MM-DDHH24:MI:SS')  from dual;

这会给你一个约会。它假定时区总是相同的。

答案 2 :(得分:0)

select cast(to_timestamp(raw_date,'YYYY-MM-DD"T"HH24:MI:SS.ff3"Z"') as DATE) from testmp1;

insert into testmp2 (new_date) (select cast(to_timestamp(raw_date,'YYYY-MM-DD"T"HH24:MI:SS.ff3"Z"') as DATE) from testmp1);
commit;

答案 3 :(得分:-1)

可能是您需要转换

 (TO_DATE('2015-04-29T19:51:30.0Z', 'yyyy/mm/dd hh24:mi:ss'));