English 中文(简体)
SQL SQL 排除日期
原标题:SQL Date Exclusion

我有一堆 SQL 选择, 收集用于导出和分析的数据。 我已声明了在几个变量中收集数据的日期范围, 并运行查询, 有10个选择的总和使用相同的日期范围变量。 我想做的是很容易地将某些日期( 它们是单日, 而不是一个范围) 从使用某种排除列表的查询中排除, 我可以在一个变量中声明, 所以所有选择都删除了这些特定日期, 而不是编辑每一个选择的语句 。

DECLARE @date_start DATETIME, @date_end DATETIME
SET @date_start =  2011-10-20 00:00:00 
SET @date_end =  2012-05-18 23:59:59 

SELECT
date, col1, col2, col3
FROM
table1
WHERE
date BETWEEN CONVERT(DATETIME, @date_start, 102) AND CONVERT(DATETIME, @date_end, 102)

SELECT
date, col4, col5, col8
FROM
table2
WHERE
date BETWEEN CONVERT(DATETIME, @date_start, 102) AND CONVERT(DATETIME, @date_end, 102)

SELECT
date, col3, col5, col6, col7, col8
FROM
table3
WHERE
date BETWEEN CONVERT(DATETIME, @date_start, 102) AND CONVERT(DATETIME, @date_end, 102)
问题回答

使用 IN:

将此附加到您的查询中: 和日期不在( date1, date2...)

您可以在指定选定日期的条件中使用。 当您考虑只使用日期部分时, 也可以使用给 Yyyy/ MM/ dd 格式的 转换( varchar, datecolum, 111) 。 不需要给时间部分 23: 59: 59 。

创建一个表格变量,该变量将显示您正在查找的日期,并在查询中使用该日期。

您可以使用日期范围变量和另一个显示您不想要的日期的表格来构建该表格。

像这样的事情:

declare @date_start datetime
declare @date_end datetime

-- The date range you want. Don t add time part.
set @date_start =  20010102 
set @date_end =  20010107 

declare @NotTheDatesYouAreLookingFor as table
(
  date datetime
)

insert into @NotTheDatesYouAreLookingFor values
( 20010103 ),
( 20010105 )

declare @TheDatesYouAreLookingFor as table
(
  date datetime primary key
)

;with C(date) as
(
  select @date_start
  union all
  select date + 1
  from C
  where date < @date_end
)
insert into @TheDatesYouAreLookingFor(date)
select date
from C
  where date not in (select date 
                     from @NotTheDatesYouAreLookingFor)

您的查询应该使用此表

select T1.date, T1.col1, T1.col2, T1.col3
from table1 as T1
  inner join @TheDatesYouAreLookingFor as TD
    on T1.date >= TD.date and T1.date < dateadd(day, 1, TD.date)




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...