SQL - 带子查询的 DATEDIFF
SQL - DATEDIFF with a subquery
我需要从 SQL Server
数据库向下游传递一个值,这本质上是两个 timestamps
之间的区别。使用 DATEDIFF
函数就足够简单了。
SELECT DATEDIFF(day, '2015-11-06 00:00:00.000','2015-12-25 00:00:00.000') AS DiffDate ;
但是,在向下传递值时,代码只知道订单 ID 值,而不知道上面显示的 2 个时间戳。因此,我需要 timestamp
信息来自子查询,或者我认为的其他东西。子查询的主要细节在这里:
select O.DATE1 , C.DATE2
from TABLE1 O, TABLE2 C
WHERE O.VALUE1_ID = C.VALUE1
AND O.order_id = '12345678'
我尝试了几种不同的方法,但是 none 都成功了。最新的我已经厌倦了下面,从语法的角度来看它看起来不错,但我得到了错误:
Conversion failed when converting date and/or time from character
string
我从来都不太确定如何应对或处理。
select DATEDIFF (day,'(select O.VALUE1
from TABLE1 O
where O.VALUE1 = 16650476)' ,
'(SELECT C.VALUE1
from TABLE1 O, TABLE2 C
WHERE O.VALUE1 = C.VALUE2 AND O.order_id = 12345678)') AS DIFFDATE;
如有任何指点或帮助,我们将不胜感激。
我想你想要以下内容:
SELECT DATEDIFF(DAY, cj.SomeDateColumn, C.SomeDateColumn)
FROM TABLE1 O
JOIN TABLE2 C ON O.VALUE1 = C.VALUE2
CROSS JOIN (SELECT O.SomeDateColumn FROM TABLE1 WHERE order_id = 16650476) cj
WHERE O.order_id = 12345678
你能声明两个datetime变量然后datediff这两个变量吗?
DECLARE @FirstDate AS datetime
DECLARE @SecondDate AS datetime
SET @FirstDate = (select O.VALUE1 from TABLE1 O where O.VALUE1 = 16650476)
SET @SecondDate = (SELECT C.VALUE1 from TABLE1 O, TABLE2 C WHERE O.VALUE1 = C.VALUE2 AND O.order_id = 12345678)
SELECT DATEDIFF(day, @FirstDate, @SecondDate)
你能试试下面的方法吗?我已经使用了你的子查询,但有关表 "Table1" 和 "Table2" 中数据的更多信息会很有用
SELECT
DATEDIFF(day, D.Date1, D.Date2) AS DiffDate
FROM
(
select O.DATE1 as Date1 , C.DATE2 as Date2
from TABLE1 O, TABLE2 C
WHERE O.VALUE1_ID = C.VALUE1
AND O.order_id = '12345678'
) D
您收到错误的原因
Conversion failed when converting date and/or time from character
string
是因为您将字符串(下方)传递给 datediff 函数而不是使用日期
'(select O.VALUE1
from TABLE1 O
where O.VALUE1 = 16650476)'
子查询不应该是字符串,所以去掉单引号。另外,您谈论的是 O.DATE1 和 C.DATE2,所以您的意思可能是这样的:
select DATEDIFF (day,
(select O.DATE1
from TABLE1 O
where O.VALUE1 = 16650476) ,
(SELECT C.DATE2
from TABLE1 O, TABLE2 C
WHERE O.VALUE1 = C.VALUE2 AND O.order_id = 12345678)) AS DIFFDATE;
我需要从 SQL Server
数据库向下游传递一个值,这本质上是两个 timestamps
之间的区别。使用 DATEDIFF
函数就足够简单了。
SELECT DATEDIFF(day, '2015-11-06 00:00:00.000','2015-12-25 00:00:00.000') AS DiffDate ;
但是,在向下传递值时,代码只知道订单 ID 值,而不知道上面显示的 2 个时间戳。因此,我需要 timestamp
信息来自子查询,或者我认为的其他东西。子查询的主要细节在这里:
select O.DATE1 , C.DATE2
from TABLE1 O, TABLE2 C
WHERE O.VALUE1_ID = C.VALUE1
AND O.order_id = '12345678'
我尝试了几种不同的方法,但是 none 都成功了。最新的我已经厌倦了下面,从语法的角度来看它看起来不错,但我得到了错误:
Conversion failed when converting date and/or time from character string
我从来都不太确定如何应对或处理。
select DATEDIFF (day,'(select O.VALUE1
from TABLE1 O
where O.VALUE1 = 16650476)' ,
'(SELECT C.VALUE1
from TABLE1 O, TABLE2 C
WHERE O.VALUE1 = C.VALUE2 AND O.order_id = 12345678)') AS DIFFDATE;
如有任何指点或帮助,我们将不胜感激。
我想你想要以下内容:
SELECT DATEDIFF(DAY, cj.SomeDateColumn, C.SomeDateColumn)
FROM TABLE1 O
JOIN TABLE2 C ON O.VALUE1 = C.VALUE2
CROSS JOIN (SELECT O.SomeDateColumn FROM TABLE1 WHERE order_id = 16650476) cj
WHERE O.order_id = 12345678
你能声明两个datetime变量然后datediff这两个变量吗?
DECLARE @FirstDate AS datetime
DECLARE @SecondDate AS datetime
SET @FirstDate = (select O.VALUE1 from TABLE1 O where O.VALUE1 = 16650476)
SET @SecondDate = (SELECT C.VALUE1 from TABLE1 O, TABLE2 C WHERE O.VALUE1 = C.VALUE2 AND O.order_id = 12345678)
SELECT DATEDIFF(day, @FirstDate, @SecondDate)
你能试试下面的方法吗?我已经使用了你的子查询,但有关表 "Table1" 和 "Table2" 中数据的更多信息会很有用
SELECT
DATEDIFF(day, D.Date1, D.Date2) AS DiffDate
FROM
(
select O.DATE1 as Date1 , C.DATE2 as Date2
from TABLE1 O, TABLE2 C
WHERE O.VALUE1_ID = C.VALUE1
AND O.order_id = '12345678'
) D
您收到错误的原因
Conversion failed when converting date and/or time from character string
是因为您将字符串(下方)传递给 datediff 函数而不是使用日期
'(select O.VALUE1
from TABLE1 O
where O.VALUE1 = 16650476)'
子查询不应该是字符串,所以去掉单引号。另外,您谈论的是 O.DATE1 和 C.DATE2,所以您的意思可能是这样的:
select DATEDIFF (day,
(select O.DATE1
from TABLE1 O
where O.VALUE1 = 16650476) ,
(SELECT C.DATE2
from TABLE1 O, TABLE2 C
WHERE O.VALUE1 = C.VALUE2 AND O.order_id = 12345678)) AS DIFFDATE;