[英]How to Convert to Datetime YYYY-MM-DD HH:mi:ss
I have a field where I need to populate GETDATE()
with YYYY-MM-DD HH-MI-SS
format. 我有一个字段,需要用YYYY-MM-DD HH-MI-SS
格式填充GETDATE()
。
Can anyone help me how to write the query in PostgreSQL? 谁能帮我如何在PostgreSQL中编写查询?
I tried this in SQL Server and it worked. 我在SQL Server中尝试了此方法,并成功了。 Not sure how to write in PostgreSQL. 不确定如何在PostgreSQL中编写。
SELECT
SUBSTRING(CONVERT(VARCHAR(23),GETDATE(),112),1,4) + '-' +
SUBSTRING(CONVERT(VARCHAR(23),getdate(),112),5,2) + '-' +
SUBSTRING(CONVERT(VARCHAR(23),getdate(),112),7,2) + ' ' +
CONVERT(VARCHAR(8), GETDATE(), 108)
Your help is appreciated. 感谢您的帮助。 Thank You Swathi. 谢谢你Swathi。
Using NOW()
with TO_CHAR()
should do that for you: 将NOW()
与TO_CHAR()
NOW()
结合使用可以为您做到这一点:
SELECT TO_CHAR(NOW(),'YYYY-MM-DD HH-MI-SS');
Docs: https://www.postgresql.org/docs/9.5/static/functions-formatting.html 文件: https : //www.postgresql.org/docs/9.5/static/functions-formatting.html
To add some additional clarity... 为了增加一些清晰度...
You have a table with a field that is of a datetime type. 您有一个表的日期时间类型的字段。 When you update the field, you must update it with a value of the same type. 更新该字段时,必须使用相同类型的值对其进行更新。
If you want to return the values in that field in a particular format, you would use the TO_CHAR()
in your SELECT
statement. 如果要以特定格式返回该字段中的值,则可以在SELECT
语句中使用TO_CHAR()
。
For example: 例如:
DROP TABLE IF EXISTS mytable;
CREATE TABLE mytable (mydate TIMESTAMP);
INSERT INTO mytable VALUES (NOW() - INTERVAL '20 days');
-- Update with the appropriate type
UPDATE mytable SET mydate = NOW();
-- Select the date in the format you want
SELECT mydate, TO_CHAR(mydate,'YYYY-MM-DD HH-MI-SS') AS mydate_text FROM mytable;
OUTPUT: 输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.