将varchar转换为datetime(Converting varchar to datetime)

编程入门 行业动态 更新时间:2024-10-24 00:20:36
将varchar转换为datetime(Converting varchar to datetime)

有谁可以帮我转换1218100860到datetime ?

我试过这样的:

SELECT CONVERT(VARCHAR(11), DATEADD(s,1218100860, '1970-01-01 00:00:00'), 101)

它的工作原理但是当我尝试这样时,它会抛出一个错误:

SELECT CONVERT(VARCHAR(11), DATEADD(s, rd.Request_Date, '1970-01-01 00:00:00'), 101) FROM dbo.RequestDetails rd

其中Request_Date是必须将值转换为datetime的列,错误是:

参数数据类型varchar对于dateadd函数的参数2无效。

Can anyone please help me with converting 1218100860 to datetime?

I tried like this:

SELECT CONVERT(VARCHAR(11), DATEADD(s,1218100860, '1970-01-01 00:00:00'), 101)

and it works but when I try like this, it throws an error:

SELECT CONVERT(VARCHAR(11), DATEADD(s, rd.Request_Date, '1970-01-01 00:00:00'), 101) FROM dbo.RequestDetails rd

where Request_Date is the column where the the value has to be converted to datetime the error is:

Argument data type varchar is invalid for argument 2 of dateadd function.

最满意答案

我不知道你正在使用什么RDBMS(也许是sQL Server?),但是:

参数数据类型varchar对于dateadd函数的参数2无效

这意味着参数2( rd.Request_Date )不应该是varchar 。 因为在你的第一个例子中你使用了一个整数并且它有效,所以你似乎只需要将rd.Request_Date转换为整数,它应该可以工作,如下所示:

SELECT CONVERT(VARCHAR(11),DATEADD(s,CONVERT(INT, rd.Request_Date), '1970-01-01 00:00:00'),101 )

I don't know what RDBMS you're using (maybe sQL Server?), but:

Argument data type varchar is invalid for argument 2 of dateadd function

This means that argument 2 (rd.Request_Date) should not be a varchar. Since in your first example you used an integer and it worked, then it seems you just have to convert rd.Request_Date to integer and it should work, like this:

SELECT CONVERT(VARCHAR(11),DATEADD(s,CONVERT(INT, rd.Request_Date), '1970-01-01 00:00:00'),101 )

更多推荐

本文发布于:2023-08-04 08:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1412476.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   varchar   Converting   datetime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!