sql - How to update a column using another column value?
问题描述
I have an empty table with rows that have timestamp
and file_url
columns.
When I add a row, timestamp
is filled with Postgres current_timestamp
value. At the same moment, I want to put into file_url
the same value, but formatted differently.
timestamp
can have this format: 2018-07-26 08:37:56.749125
and for file_url
I want to have something like http://somedomain.com/files/20180726083756.749125.jpg
.
How can I do it in one run?
解决方案
您可以尝试TO_CHAR
在时间戳上使用该函数:
select to_char(timestamp '2018-07-26 08:37:56.749125', 'YYYYMMDDHH24MISS.MS');
这将输出20180726083756.749
. 如果您想构建一个 URL,那么您也可以使用字符串连接来实现,例如
INSERT INTO yourTable (ts_col, file_url)
SELECT
current_timestamp,
'http://somedomain.com/files/' ||
TO_CHAR(current_timestamp, 'YYYYMMDDHH24MISS.MS') || '.jpg';
推荐阅读
- swift - 如何在 Swift 中替换数组切片?
- javascript - img src 不能在 Vue js 中工作并且必须有正确的路径?
- github - 发布 GitHub 页面时触发 GitHub 操作
- python - vscode 语法高亮中断
- angular - 如何在 Angular Material 中使用 ckeditor5
- reactjs - 将 React Native 调试器与 React Native 应用程序 (expo) 集成时出错
- django - 如果我在 django 中删除图片 upload_to 会发生什么
- android - Android 如何将 Firebase 数据库参考传递给新活动
- php - 查询字符串可以包含字符串以外的任何内容吗?
- amazon-web-services - 此 AWS EFS 策略有什么问题?