如何在SQL选择语句中将日期列转换为时间戳。

huangapple go评论61阅读模式
英文:

How do I convert a date column to a timestamp in a SQL select statement

问题

I'm trying to write a DB2 query that is selecting a date column and I would like to convert it into a timestamp. For example, the query should take a column that is in a "mm/dd/yyyy" format and convert it to a column that is in a "mm/dd/yyyy hh:mm:ss" format.

For example, if END_DT is a date column in the table, then I would like to select an additional column that is END_DT but it also has a constant value of hh:mm:ss attached to it. I.e '1/29/2023' should be converted to '1/29/2023 11:22:33' (and so on, for each other END_DT value in each of the returned rows of the query).

I tried something like this:

SELECT 
    END_DT, TIMESTAMP(END_DT) AS end_dt_with_timestamp 
FROM <table>

but the TIMESTAMP(END_DT) portion of the query does not seem to work.

英文:

I'm trying to write a DB2 query that is selecting a date column and I would like to convert it into a timestamp. For example, the query should take a column that is in a "mm/dd/yyyy" format and convert it to a column that is in a "mm/dd/yyyy hh:mm:ss" format.

For example, if END_DT is a date column in the table, then I would like to select an an additional column that is END_DT but it also has a constant value of hh:mm:ss attached to it. I.e '1/29/2023' should be converted to '1/29/2023 11:22:33' (and so on, for each other END_DT value in each of returned row of the query).

I tried something like this:

SELECT 
    END_DT, TIMESTAMP(END_DT) AS end_dt_with_timestamp 
FROM &lt;table&gt;

but the TIMESTAMP(END_DT) portion of the query does not seem to work.

答案1

得分: 1

你没有提到你正在使用哪个平台和版本的Db2...

但看一下你的TIMESTAMP()函数版本是否支持两个参数,其中第二个参数允许

expression-2必须是一个返回以下内置数据类型之一的表达式:时间、字符字符串或图形字符串。如果expression-2是字符或图形字符串,其值必须是时间的有效字符串表示。有关日期和时间的字符串表示的有效格式,请参见第75页的“日期时间值的字符串表示”。

因此,你只需要

SELECT END_DT, TIMESTAMP(END_DT, '11:22:33') as end_dt_with_timestamp 
from <table>
英文:

You don't mention what platform and version of Db2 you are working with...

But see if your version of the TIMESTAMP() function supports two arguments, with the second argument allowing for

> expression-2 must be an expression that returns a value of one of the
> following built-in data types: a time, a character string, or a
> graphic string. If expression-2 is a character or graphic string, its
> value must be a valid string representation of a time. For the valid
> formats of string representations of dates and times, see “String
> representations of datetime values” on page 75.

Thus all you need is

SELECT END_DT, TIMESTAMP(END_DT, &#39;11:22:33&#39;) as end_dt_with_timestamp 
from &lt;table&gt;

答案2

得分: 0

You can achieve this by using CONVERT to change datatype to DATETIME and then DATEADD to add your specific timestamp.

Full code for your 1/29/2023 example:

SELECT DATEADD(SECOND, 33, DATEADD(MINUTE, 22, DATEADD(HOUR, 11, CONVERT(DATETIME, '2023-01-29')))

Without the DATEADD portions, the DATETIME datatype will add a time stamp of '00:00:00.000' automatically.

英文:

You can achieve this by using CONVERT to change datatype to DATETIME and then DATEADD to add your specific timestamp.

Full code for your 1/29/2023 example:

SELECT DATEADD(SECOND, 33, DATEADD(MINUTE, 22, DATEADD(HOUR, 11, CONVERT(DATETIME, &#39;2023-01-29&#39;))))

Without the DATEADD portions, the DATETIME datatype will add a time stamp of '00:00:00.000' automatically.

答案3

得分: 0

你可以在新转换的时间戳上添加任何你需要的小时、分钟和秒

CREATE TABLE t1 (END_DT date)
INSERT INTO t1 VALUES ('29/1/2023' )
SELECT END_DT, TIMESTAMP(END_DT) + 11 HOUR + 23 MINUTE + 53 SECOND as end_dt_with_timestamp FROM t1
END_DT END_DT_WITH_TIMESTAMP
2023-01-29 2023-01-29 11:23:53

fiddle

英文:

you can add to the newly converted timestamp any hour, minutes and second you need

CREATE tABLE t1 (END_DT date)
INSERT INTO t1 VALUES (&#39;29/1/2023&#39; )
SELECT END_DT, TIMESTAMP(END_DT)  + 11 HOUR + 23 MINUTE + 53 SECOND as end_dt_with_timestamp FROM t1
END_DT END_DT_WITH_TIMESTAMP
2023-01-29 2023-01-29 11:23:53

fiddle

huangapple
  • 本文由 发表于 2023年4月4日 05:06:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定