how to save only time not date in database through sql query

百般思念 提交于 2021-02-16 15:31:28

问题


this query is saving complete date and time. but i want to save only time not date in database. is there any query to do this?

update table set current_time=now();

回答1:


Your column must be set to either DATETIME or TIMESTAMP.

If you use the TIME type then your query would work as expected.

If you are using any other type of column then you could use CURTIME() method or CAST(column AS TIME) as mentioned by other answers, however this would use more space on disk, and make for much slower queries if you use to select, and prevent you from various operators:

e.g. SELECT * FROM table WHERE current_time<'12:00'

You can see more information about the different DATE column types here: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-types.html

Note that the CURTIME() method is not a standard SQL function, so this would only work on MySql




回答2:


U can use CONVERT (time, SYSDATETIME()) as the value.

This automates your process without using Current_time=now();

INSERT INTO table SET current_time = CONVERT (time, SYSDATETIME());

You can also use curtime();

INSERT INTO table SET current_time = curtime();

Credits: Salmaan C



来源:https://stackoverflow.com/questions/42315746/how-to-save-only-time-not-date-in-database-through-sql-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!