我有一个联接,返回今天的结果,但我希望它返回昨天的值。
FROM
Person
JOIN Profit
ON Person.id = Profit.id
AND [DATE] = CAST(getdate () -1 as Date)
^^这将返回当前日期。
然后我试图返回昨天的日期:
[DATE] = DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), -1)
[DATE] = DATEADD(day,DATEDIFF(day,0,GETDATE())-1,0)
[DATE] = dateadd(DD, -1, cast(getdate() as date))
但这些都不起作用。有人可以帮忙吗?感谢
答案 0 :(得分:1)
如果你执行
select DATEADD(day, -1, cast(SYSDATETIME() as DATE))
select dateadd(DD, -1, cast(getdate() as date))
然后返回昨天的日期。因此,唯一的问题可能是[DATE]属性的格式。
答案 1 :(得分:0)
简单地:
FROM
Person
JOIN Profit
ON Person.id = Profit.id
WHERE Table.[DATE] = DATEADD(DAY, -1, GETDATE() );
答案 2 :(得分:0)
可能[DATE]也需要转换:
FROM
Person
JOIN
Profit
ON
Person.id = Profit.id
AND CAST([DATE] as Date) = CAST(getdate () -1 as Date)
答案 3 :(得分:0)
如果您不希望时间成为日期的一部分,请使用此选项。
SELECT *
FROM Person
JOIN Profit ON Person.id = Profit.id
AND [DATE] = CONVERT(VARCHAR, DATEADD(DAY, -1, GETDATE()), 106)
答案 4 :(得分:0)
如果您有包含日期的列:
SELECT * from Person p join Profit pr
on (p.id = pr.person_id and pr.`date` = SUBDATE(CURDATE(),1))
答案 5 :(得分:0)
我认为您应该使用Between
,如下所示:
FROM
Person
JOIN Profit
ON Person.id = Profit.id
AND [DATE] BETWEEN CONVERT(date, GETDATE()-1) AND CONVERT(date, GETDATE()-1)