문제

I need to trim the last seven characters off of all the cells in a column.

This query returns the results that I need

SELECT test_date, SUBSTRING(test_date FROM 1 FOR CHAR_LENGTH(test_date) - 7) as test from results;

I now need to take the computed result from the substring and replace the original values.

Here's what the data looks like, if it helps.

Data Table

도움이 되었습니까?

해결책

Make a simple Update

CREATE TABLE Table1
    (`test_date` datetime)
;
INSERT INTO Table1
    (`test_date`)
VALUES
    ('2020-06-10 00:06:17.185449'),
    ('2020-06-10 00:11:05.943217'),
    ('2020-06-10 00:13:45.375109'),
    ('2020-06-10 00:13:45.461321'),
    ('2020-06-10 00:15:32.497548'),
    ('2020-06-10 00:15:32.544873')
;
UPDATE Table1 SET test_date = SUBSTRING(test_date FROM 1 FOR CHAR_LENGTH(test_date) - 7)
SELECT * FROm Table1
| test_date           |
| :------------------ |
| 2020-06-10 00:00:00 |
| 2020-06-10 00:00:00 |
| 2020-06-10 00:00:00 |
| 2020-06-10 00:00:00 |
| 2020-06-10 00:00:00 |
| 2020-06-10 00:00:00 |

db<>fiddle here

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 dba.stackexchange
scroll top