0

这对你来说可能很简单(但我是初学者)。

我有一个用以Table_1下列命名的表:

[id]
[command]
[verify_date]
[data_interval_start]
[data_interval_end]

表中的数据如下所示:

id  command     verify_date  data_interval_start   data_interval_end
--------------------------------------------------------------------
1   123456      2013-02-01   2013-05-01            2013-05-06 
2   234513      2012-05-02   2013-01-01            2013-03-01 
3   221342      2013-05-04   2011-01-01            2011-01-02 

我想检查是否verify_date在每个data_interval_start和之间data_interval_end;所以取第一个值并在和verify_date之间的所有记录之间检查它。data_interval_startdata_interval_end

我试过这个查询但没有结果:

SELECT command from Table_1 
WHERE verify_date IN (data_interval_start, data_interval_end)


SELECT command from Table_1 
WHERE verify_date between data_interval_start AND data_interval_end 


SELECT command from Table_1 
WHERE verify_date = data_interval_start OR verify_date = data_interval_end

任何帮助表示赞赏:)。

谢谢

4

3 回答 3

1

假设您的日期是DATE type fields,您可以使用CASE如下;

select id, command, case when verify_date between  
                              data_interval_start and  data_interval_end 
                         then 'Yes'
                         else 'No' 
                    end isBetween
from table1

如果您的日期是字符串类型字段,则将它们放入ISO format( yyyymmdd) 并转换为 DATE 进行比较,如下所示;

select id, command, case when convert(date,replace(verify_date,'-','')) between  
                              convert(date,replace(data_interval_start,'-','')) 
                              and convert(date,replace(data_interval_end,'-',''))  
                         then 'Yes'
                         else 'No' 
                    end isBetween
from table1

编辑:如果您需要比较给定的日期,您可以按如下方式进行。(请注意 between 是包含的。因此,如果您需要排除data_interval_startand data_interval_end,则使用<and>运算符)

--say this is the date you need to compare your records with
declare @verify_date date = '20130201'

select id, command
from table1
where @verify_date between convert(date, data_interval_start) 
                              and convert(date, data_interval_end) 

要做所有的varify_date,你可以使用cross join下面的。

Sql-Fiddle-Demo

select t1.id, t1.command, t2.verify_date
from table1 t1 cross join table1 t2
where t2.verify_date between convert(date, t1.data_interval_start) 
                              and convert(date, t1.data_interval_end)

| ID | COMMAND | VERIFY_DATE |
------------------------------
|  2 |  234513 |  2013-02-01 |
|  1 |  123456 |  2013-05-04 |
于 2013-03-26T11:16:29.973 回答
0

我猜你正在比较两个字符串尝试将它们转换为DATE

像那样

     select command from Table_1 where convert(CHAR(10),verify_date,111) 
     between convert(CHAR(10),data_interval_start,111)  AND convert(CHAR(10),data_interval_end,111)  

这里如何转换日期

于 2013-03-26T11:00:47.263 回答
0

您的第二个查询将为您工作

SELECT command from Table_1 
WHERE verify_date between data_interval_start AND data_interval_end 

id  command     verify_date  data_interval_start   data_interval_end
--------------------------------------------------------------------
1   123456      2013-02-01   2013-05-01            2013-05-06 
2   234513      2012-05-02   2012-01-01            2013-03-01 
3   221342      2013-05-04   2011-01-01            2011-01-02 

现在检查上述数据

于 2013-03-26T11:32:02.317 回答