如何在oracle中向varchar字段添加数字字段。 varchar字段是HH:MM:SS格式的时间

时间:2017-10-24 20:43:19

标签: sql oracle

我有2列

Start_Time (VARCHAR)           Duration_in_sec  (NUMBER)
12:03:11                          220
11:05:33                          345

我想将col2添加到col1以获取结束时间。 我怎么做我试过

Select TO_NUMBER(col1)+col2 as end_time from ABC. 

这给了我错误。有人可以用正确的方式帮助我 这是我得到的错误,这是直截了当但如何获得正确的数字。我需要对col1进行一些修剪,但我不知道如何做到这一点

ORA-01722: invalid number
01722. 00000 -  "invalid number"
*Cause:    The specified number was invalid.
*Action:   Specify a valid number.

2 个答案:

答案 0 :(得分:1)

您可以将开始时间和持续时间都转换为间隔,然后添加两个间隔。这样你可以在持续时间超过一天时处理:

SQL Fiddle

Oracle 11g R2架构设置

SELECT t.*,
       (   TO_TIMESTAMP( start_time, 'HH24:MI:SS' )
         - TO_TIMESTAMP( '00:00:00', 'HH24:MI:SS' )
       ) + NUMTODSINTERVAL( Duration_in_sec, 'SECOND' ) AS end_time
FROM   table_name t

查询1

| START_TIME | DURATION_IN_SEC |     END_TIME |
|------------|-----------------|--------------|
|   12:03:11 |             220 |  0 12:6:51.0 |
|   11:05:33 |             345 | 0 11:11:18.0 |
|   23:59:59 |               1 |    1 0:0:0.0 |

<强> Results

CREATE TABLE table_name (
  start_time      INTERVAL DAY TO SECOND,
  duration_in_sec INTERVAL DAY TO SECOND
);

INSERT INTO table_name
SELECT INTERVAL '12:03:11' HOUR TO SECOND, INTERVAL '220' SECOND FROM DUAL UNION ALL
SELECT INTERVAL '11:05:33' HOUR TO SECOND, INTERVAL '345' SECOND FROM DUAL UNION ALL
SELECT INTERVAL '23:59:59' HOUR TO SECOND, INTERVAL '1' SECOND FROM DUAL;

更好的解决方案是将开始时间和持续时间存储为间隔;然后你可以添加值:

SQL Fiddle

Oracle 11g R2架构设置

SELECT t.*,
       start_time + Duration_in_sec AS end_time
FROM   table_name t

查询1

|   START_TIME | DURATION_IN_SEC |     END_TIME |
|--------------|-----------------|--------------|
|  0 12:3:11.0 |      0 0:3:40.0 |  0 12:6:51.0 |
|  0 11:5:33.0 |      0 0:5:45.0 | 0 11:11:18.0 |
| 0 23:59:59.0 |       0 0:0:1.0 |    1 0:0:0.0 |

<强> Results

#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 4

int Array[MAX_SIZE];

void swap(int one, int two) {
    int temp = Array[one];
    Array[one] = Array[two];
    Array[two] = temp;
}

int partition(int left, int right, int pivot) {
    int leftPointer = left - 1;
    int rightPointer = right;
    while (Array[++leftPointer] < pivot) {
    }
    while (rightPointer > 0 && Array[--rightPointer] < pivot) {
        if (leftPointer >= rightPointer) {
            break;
        } else {
            swap(leftPointer, rightPointer);
            swap(leftPointer, right);
        }
    }
    return leftPointer;
}

void Quicksort(int left, int right) {
    if (right - left <= 0) {
        return 0;
    } else {
        int pivot = Array[right];
        int PartitionPoint = partition(left, right, pivot);
        Quicksort(left, PartitionPoint - 1);
        Quicksort(PartitionPoint + 1, right);
    }
}

int main() {
    int i;
    printf("\nGive values");
    for (i = 0; i <= 3; i++) {
        scanf("%d", &Array[i]);
    }
    Quicksort(0, MAX_SIZE - 1);
    printf("\nOutput: ");
    for (i = 0; i <= 3; i++) {
        printf("%d  ", Array[i]);
    }
    return 0;
}

答案 1 :(得分:0)

这是一种方式,使用POST /endpoint1功能:

[GET|PUT|DELETE] /endpoint1

(使用名为&#34的表; time_seconds&#34;)

只返回时间字符串:

numToDSInterval

首先,它将时间戳从VARCHAR转换为DATE,然后使用select to_date(start_time,'HH24:MI:SS')+numToDSInterval(Duration_in_sec, 'second') from time_seconds; 将该DATE增加秒数。

但是,我想补充说,将时间存储为字符串似乎是一个坏主意。您的时间是否以24小时格式存在?或AM / PM 12小时?你怎么能确定你的字符串可以解析为日期?   等等。?您应该使用正确的数据类型来完成正确的工作。