如何使用interval计算sql中的日期范围

时间:2013-07-29 14:52:30

标签: sql-server tsql

我在一个表中有两个表我有一个列违规日期,在其他表中我有NTO日期列  现在我想找到如何从NTOdate超过50天违规日期的表中检索数据。 但是当我运行此查询时,我会在“' 50'。

附近收到错误的语法错误。”
SELECT [ICPS].[dbo].[tickets].t_vrm
  ,[ICPS].[dbo].[tickets].t_zone_name
  ,[ICPS].[dbo].[tickets].t_street_name
FROM [ICPS].[dbo].[tickets]
INNER JOIN [ICPS].[dbo].[ticket_events]
  ON [ICPS].[dbo].[ticket_events].[te_system_ref] = [ICPS].[dbo].[tickets].[t_number]
WHERE [ICPS].[dbo].[tickets].[t_camera_ticket] = '0'
  AND [ICPS].[dbo].[tickets].[t_date_time_issued] >= convert(DATETIME, '2012/10/01', 101)
  AND [ICPS].[dbo].[ticket_events].[te_event_code] = '300'
  AND [ICPS].[dbo].[ticket_events].[te_date] > DATE (DATE_SUB([ICPS].[dbo].[tickets].[t_date_time_issued], INTERVAL 50 days))

1 个答案:

答案 0 :(得分:0)

您可以使用Datediff函数:DateDiff(DD,'firstDate','SecondDate')。以下是样本:

select [ICPS].[dbo].[tickets].t_vrm,[ICPS].[dbo].[tickets] .t_zone_name, [ICPS].[dbo].[tickets] .t_street_name from  [ICPS].[dbo].[tickets]  inner join 
      [ICPS].[dbo].[ticket_events] on [ICPS].[dbo].[ticket_events].[te_system_ref] = [ICPS].[dbo].[tickets].[t_number] 
where[ICPS].[dbo].[tickets].[t_camera_ticket] = '0' and  [ICPS].[dbo].[tickets].[t_date_time_issued] >= convert(datetime,'2012/10/01',101)
      and [ICPS].[dbo].[ticket_events].[te_event_code] = '300' and 
     DateDiff(DD, [ICPS].[dbo].[tickets].[t_date_time_issued], [ICPS].[dbo].[ticket_events].[te_date]) >50)