将列作为参数传递给 SQL Server 中的 dateadd

2022-11-15数据库问题
74

本文介绍了将列作为参数传递给 SQL Server 中的 dateadd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想将一列 UTC 时间转换为本地时间.

我的数据如下所示:

time_utc TZID 时区------------------------------------------------2014-02-27 12:00:39.0 美国/多伦多-52013-05-21 09:35:30.0 America/Goose_Bay -42015-01-08 06:58:58.0 美国/克雷斯顿 -7

我知道使用

select *, DATEADD(hour, 5,time_utc)来自 mytable

将向 time_utc 列添加 5 小时.

但是,如您所见,我有一个可变时区列.

如何将此变量传递给 dateadd 函数?

我尝试了以下 2 个命令,但它们不起作用:

尝试 #1:

select *, DATEADD(hour, timezone, time_utc)来自 mytable

尝试 #2:

select *, DATEADD(hour, (select timezone from mytable), time_utc)来自 mytable

两者都抛出这个错误:

<块引用>

参数数据类型 varchar 对 dateadd 函数的参数 2 无效.[SQL 状态=S0001,数据库错误代码=8116]

对于时区的十进制值,例如 -3.5,这将如何工作?

谢谢

解决方案

如何将此变量传递给 datetime 函数?

只需在函数调用中引用列:

select *, DATEADD(hour, timezone, time_utc)来自 mytable

<块引用>

对于时区的十进制值,例如 -3.5,这将如何工作?

DATEADD 的数字"参数采用整数,因此您必须更改为分钟并缩放小时偏移量.由于您的 timezone 列显然是一个 varchar 列,因此也将其转换为十进制值:

select *, DATEADD(minute, cast(timezone as decimal(4,2)) * 60 , time_utc)来自 mytable

I want to convert a column of UTC time to local time.

My data looks like this:

time_utc                TZID            timezone
------------------------------------------------
2014-02-27 12:00:39.0   America/Toronto     -5
2013-05-21 09:35:30.0   America/Goose_Bay   -4
2015-01-08 06:58:58.0   America/Creston     -7

I know that using

select *, DATEADD(hour, 5,time_utc)
from mytable

will add 5 hours to column time_utc.

However, as you can see, I have a variable time zone column.

How can I pass this variable to the dateadd function?

I tried the following 2 commands but they don't work:

Attempt #1:

select *, DATEADD(hour, timezone, time_utc)
from mytable

Attempt #2:

select *, DATEADD(hour, (select timezone from mytable), time_utc)
from mytable

Both throws this error:

Argument data type varchar is invalid for argument 2 of dateadd function. [SQL State=S0001, DB Errorcode=8116]

For decimal values of timezone, for instance -3.5, how would this work?

Thanks

解决方案

How can I pass this variable to datetime function?

Just reference the column in the function call:

select *, DATEADD(hour, timezone, time_utc)
from mytable

For decimal values of timezone, for instance -3.5, how would this work?

The "number" parameter of DATEADD takes an integer, so you'd have to change to minutes and scale the hour offset. Since your timezone colume is apparently a varchar column, convert it to a decimal value as well:

select *, DATEADD(minute, cast(timezone as decimal(4,2)) * 60 , time_utc)
from mytable

这篇关于将列作为参数传递给 SQL Server 中的 dateadd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End
SQLServer

相关推荐

将可变参数列表传递给 SqlServer2008 存储过程的理智/快速方法
Sane/fast method to pass variable parameter lists to SqlServer2008 stored procedure(将可变参数列表传递给 SqlServer2008 存储过程的理智/快速方法)...
2023-10-26 数据库问题
1

为什么SqlServer select语句会选择匹配的行和匹配并带有尾随空格的行
Why would SqlServer select statement select rows which match and rows which match and have trailing spaces(为什么SqlServer select语句会选择匹配的行和匹配并带有尾随空格的行)...
2023-10-08 数据库问题
3

SQLSERVER 中的 ListAGG
ListAGG in SQLSERVER(SQLSERVER 中的 ListAGG)...
2023-07-18 数据库问题
7

相当于 mySQL 中的 SQLServer 函数 SCOPE_IDENTITY()?
The equivalent of SQLServer function SCOPE_IDENTITY() in mySQL?(相当于 mySQL 中的 SQLServer 函数 SCOPE_IDENTITY()?)...
2023-04-28 数据库问题
59

使用 spark sql 在 sqlserver 上执行查询
execute query on sqlserver using spark sql(使用 spark sql 在 sqlserver 上执行查询)...
2023-04-04 数据库问题
8

Debezium 如何使用 Kafka Connect 正确注册 SqlServer 连接器 - 连接被拒绝
Debezium How do I correctly register the SqlServer connector with Kafka Connect - connection refused(Debezium 如何使用 Kafka Connect 正确注册 SqlServer 连接器 - 连接被拒绝)...
2023-04-03 数据库问题
3