简体   繁体   English

如何转换为日期时间YYYY-MM-DD HH:mi:ss

[英]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.

相关问题 将秒转换为 yyyy-mm-dd hh:mm:ss PostgreSQL - Convert seconds to yyyy-mm-dd hh:mm:ss PostgreSQL 如何在 Postgres 中将 YYYY-MM-DDTHH:mm:ss.SSSZ 时间戳转换为 YYYY-MM-DD HH:mm:ss - How to convert YYYY-MM-DDTHH:mm:ss.SSSZ timestamp to YYYY-MM-DD HH:mm:ss in Postgres 将任何输入时间戳 ex (4/25/2021 20.01.42) 转换为正确的 Postgres 时间戳 (YYYY-MM-DD HH12:MI:SS) - Convert any input timestamp ex (4/25/2021 20.01.42) to Correct Postgres TimeStamp (YYYY-MM-DD HH12:MI:SS) 将日期'yyyymmdd hh24:MI:SS'转换为yyyy-mm-ddTHH24:MI:SS - Convert date 'yyyymmdd hh24:MI:SS' to yyyy-mm-ddTHH24:MI:SS 如何将格式为 'YYYY-DD-MM HH:MI:SS.MS +00:00' 的字符串转换为 postgres timestamptz,其中最后四个字符是时区偏移量? - How do I convert a string of the form 'YYYY-DD-MM HH:MI:SS.MS +00:00' where last four chars are a timezone offset into a postgres timestamptz? Sequelize 如何将日期格式化为 YYYY-MM-DD HH:mm:ss 并将列保持为蛇形 - Sequelize How do you format date to YYYY-MM-DD HH:mm:ss and keep the columns as snake case 在Postgresql中存储格式为yyyy-mm-dd hh:mm:ss的日期的数据类型 - Datatype to store date of format yyyy-mm-dd hh:mm:ss in postgresql 以oracle和PostgreSQL查询以yyyy-MM-dd'T'HH:mm:ss.SSSZ格式打印日期 - Query to print date in yyyy-MM-dd'T'HH:mm:ss.SSSZ format in oracle and postgresql Rails转换yyyy-MM-dd'T'HH:mm:ssZ至今 - Rails Convert yyyy-MM-dd'T'HH:mm:ssZ To date 如何将 YYYY-MM-DD 字符串格式转换为 Golang 中的时间戳? - How to convert YYYY-MM-DD string format to timestamp in Golang?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM