简体   繁体   中英

how recovery ONLY datetime and TIME from TIMESTAMP(6) mysql?

I have this database:

    CREATE TABLE `pruebas` (
    `id` mediumint(8) UNSIGNED NOT NULL,
    `datetime` datetime(6) NOT NULL,
    `timestamp` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
    `time` time(6) NOT NULL
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

    INSERT INTO `pruebas` (`id`, `datetime`, `timestamp`, `time`) VALUES
    (1, '2020-10-27 15:27:30.039951', '2020-10-27 20:27:30.039951', '15:27:30.039951'),
    (2, '2020-10-27 15:27:31.162827', '2020-10-27 20:27:31.162827', '15:27:31.162827'),
    (3, '2020-10-27 15:28:20.158641', '2020-10-27 20:28:20.158641', '15:28:20.158641'),
    (4, '2020-10-27 15:32:17.708642', '2020-10-27 20:32:17.708642', '15:32:17.708642'),
    (5, '2020-10-27 15:32:25.938338', '2020-10-27 20:32:25.938338', '15:32:25.938338'),
    (6, '2020-10-27 15:32:42.150837', '2020-10-27 20:32:42.150837', '15:32:42.150837'),
    (7, '2020-10-27 15:32:50.849249', '2020-10-27 20:32:50.849249', '15:32:50.849249'),
    (8, '2020-10-27 15:34:57.296413', '2020-10-27 20:34:57.296413', '15:34:57.296413'),
    (9, '2020-10-27 15:35:21.746913', '2020-10-27 20:35:21.746913', '15:35:21.746913'),
    (10, '2020-10-27 15:35:36.356490', '2020-10-27 20:35:36.356490', '15:35:36.356490'),
    (11, '2020-10-27 15:37:50.815179', '2020-10-27 20:37:50.815179', '15:37:50.815179'),
    (12, '2020-10-27 15:39:13.878842', '2020-10-27 20:39:13.878842', '15:39:13.878842'),
    (13, '2020-10-27 15:39:24.146655', '2020-10-27 20:39:24.146655', '15:39:24.146655'),
    (14, '2020-10-27 15:39:51.128294', '2020-10-27 20:39:51.128294', '15:39:51.128294');

    ALTER TABLE `pruebas`
    ADD UNIQUE KEY `id` (`id`);

    ALTER TABLE `pruebas`
    MODIFY `id` mediumint(8) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=22;
    COMMIT;

how I can recovery ONLY DATETIME from any of this columns?

I try with DATE_FORMAT, FROM_UNIXTIME, UNIX_TIMESTAMP, TRUNCATE, SEC_TO_TIME, and others "tricks/functions", but never I get: datetime :

by example from ID 1 I need the result datetime = "2020-10-27 15:27:30", time = 15:27:30

Thanks

You can cast() :

select cast('2020-10-27 15:27:30.039951' as datetime) dt

For your sample data, this query:

select p.*, 
    cast(`datetime` as datetime) datetime1,
    cast(`timestamp` as datetime) timestamp1,
    cast(`time` as datetime) time1
from pruebas p

Produces:

id | datetime                   | timestamp                  | time            | datetime1           | timestamp1          | time1              
-: | :------------------------- | :------------------------- | :-------------- | :------------------ | :------------------ | :------------------
 1 | 2020-10-27 15:27:30.039951 | 2020-10-27 20:27:30.039951 | 15:27:30.039951 | 2020-10-27 15:27:30 | 2020-10-27 20:27:30 | 2020-10-27 15:27:30
 2 | 2020-10-27 15:27:31.162827 | 2020-10-27 20:27:31.162827 | 15:27:31.162827 | 2020-10-27 15:27:31 | 2020-10-27 20:27:31 | 2020-10-27 15:27:31
 3 | 2020-10-27 15:28:20.158641 | 2020-10-27 20:28:20.158641 | 15:28:20.158641 | 2020-10-27 15:28:20 | 2020-10-27 20:28:20 | 2020-10-27 15:28:20
 4 | 2020-10-27 15:32:17.708642 | 2020-10-27 20:32:17.708642 | 15:32:17.708642 | 2020-10-27 15:32:18 | 2020-10-27 20:32:18 | 2020-10-27 15:32:18
 5 | 2020-10-27 15:32:25.938338 | 2020-10-27 20:32:25.938338 | 15:32:25.938338 | 2020-10-27 15:32:26 | 2020-10-27 20:32:26 | 2020-10-27 15:32:26
 6 | 2020-10-27 15:32:42.150837 | 2020-10-27 20:32:42.150837 | 15:32:42.150837 | 2020-10-27 15:32:42 | 2020-10-27 20:32:42 | 2020-10-27 15:32:42
 7 | 2020-10-27 15:32:50.849249 | 2020-10-27 20:32:50.849249 | 15:32:50.849249 | 2020-10-27 15:32:51 | 2020-10-27 20:32:51 | 2020-10-27 15:32:51
 8 | 2020-10-27 15:34:57.296413 | 2020-10-27 20:34:57.296413 | 15:34:57.296413 | 2020-10-27 15:34:57 | 2020-10-27 20:34:57 | 2020-10-27 15:34:57
 9 | 2020-10-27 15:35:21.746913 | 2020-10-27 20:35:21.746913 | 15:35:21.746913 | 2020-10-27 15:35:22 | 2020-10-27 20:35:22 | 2020-10-27 15:35:22
10 | 2020-10-27 15:35:36.356490 | 2020-10-27 20:35:36.356490 | 15:35:36.356490 | 2020-10-27 15:35:36 | 2020-10-27 20:35:36 | 2020-10-27 15:35:36
11 | 2020-10-27 15:37:50.815179 | 2020-10-27 20:37:50.815179 | 15:37:50.815179 | 2020-10-27 15:37:51 | 2020-10-27 20:37:51 | 2020-10-27 15:37:51
12 | 2020-10-27 15:39:13.878842 | 2020-10-27 20:39:13.878842 | 15:39:13.878842 | 2020-10-27 15:39:14 | 2020-10-27 20:39:14 | 2020-10-27 15:39:14
13 | 2020-10-27 15:39:24.146655 | 2020-10-27 20:39:24.146655 | 15:39:24.146655 | 2020-10-27 15:39:24 | 2020-10-27 20:39:24 | 2020-10-27 15:39:24
14 | 2020-10-27 15:39:51.128294 | 2020-10-27 20:39:51.128294 | 15:39:51.128294 | 2020-10-27 15:39:51 | 2020-10-27 20:39:51 | 2020-10-27 15:39:51

Demo on DB Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM