Sql server 在where子句中使用if else时出错
我正在向存储过程传递两个变量,如Sql server 在where子句中使用if else时出错,sql-server,Sql Server,我正在向存储过程传递两个变量,如SDate和EDate,我想从VDAte介于SDate和EDate之间的表中选择数据,如果SDate和EDate都不为空。如果EDate为空,我想选择其中的数据VDAte=SDAte。我曾尝试在where子句中使用If..Else和Case,但它给出了不正确的syntx错误。 查询: 任何帮助都将不胜感激。稍微调整一下名称 SELECT Column1, Column2 from MyTable where VDate between @SDate and i
SDate
和EDate
,我想从VDAte
介于SDate
和EDate
之间的表中选择数据,如果SDate
和EDate
都不为空。如果EDate
为空,我想选择其中的数据VDAte
=SDAte
。我曾尝试在where子句中使用If..Else
和Case
,但它给出了不正确的syntx错误。
查询:
任何帮助都将不胜感激。稍微调整一下名称
SELECT Column1, Column2
from MyTable
where VDate between @SDate and isnull(@EDate, @SDate)
应该这样做。稍微调整一下名称
SELECT Column1, Column2
from MyTable
where VDate between @SDate and isnull(@EDate, @SDate)
应该这样做。稍微调整一下名称
SELECT Column1, Column2
from MyTable
where VDate between @SDate and isnull(@EDate, @SDate)
应该这样做。稍微调整一下名称
SELECT Column1, Column2
from MyTable
where VDate between @SDate and isnull(@EDate, @SDate)
应该这样做。我想您可以尝试以下查询:
SELECT
Column1,
Column2
FROM
Table t
WHERE
t.VDate IN (
SELECT
VDate
FROM
Table
WHERE
VDate BETWEEN SDate AND EDate
AND
EDate is not null
)
OR
(
t.VDate = SDate
AND
EDate is null
)
如果
EDate
不为空t.VDate
介于SDate
和EDate
之间,否则t.VDate=SDate
我想您可以尝试以下查询:
SELECT
Column1,
Column2
FROM
Table t
WHERE
t.VDate IN (
SELECT
VDate
FROM
Table
WHERE
VDate BETWEEN SDate AND EDate
AND
EDate is not null
)
OR
(
t.VDate = SDate
AND
EDate is null
)
如果
EDate
不为空t.VDate
介于SDate
和EDate
之间,否则t.VDate=SDate
我想您可以尝试以下查询:
SELECT
Column1,
Column2
FROM
Table t
WHERE
t.VDate IN (
SELECT
VDate
FROM
Table
WHERE
VDate BETWEEN SDate AND EDate
AND
EDate is not null
)
OR
(
t.VDate = SDate
AND
EDate is null
)
如果
EDate
不为空t.VDate
介于SDate
和EDate
之间,否则t.VDate=SDate
我想您可以尝试以下查询:
SELECT
Column1,
Column2
FROM
Table t
WHERE
t.VDate IN (
SELECT
VDate
FROM
Table
WHERE
VDate BETWEEN SDate AND EDate
AND
EDate is not null
)
OR
(
t.VDate = SDate
AND
EDate is null
)
如果
EDate
不为空t.VDate
介于SDate
和EDate
之间,否则t.VDate=SDate
则不能以这种方式在where子句中使用If
因此,如果您比较的数据取决于EDate值,您可以这样做:
SELECT Column1 ,column2
from Table t
WHERE
(t.EDate IS NOT NULL AND t.VDate IN (SELECT VDate FROM Table where VDate BETWEEN SDate AND EDate))
OR (t.EDate IS NULL AND t.VDate IN (SELECT SDate FROM Table))
如果EDate不为null,查询将选择介于之间的VDate。if为null,则为另一个。问题是,不能以这种方式在where子句中使用if 因此,如果您比较的数据取决于EDate值,您可以这样做:
SELECT Column1 ,column2
from Table t
WHERE
(t.EDate IS NOT NULL AND t.VDate IN (SELECT VDate FROM Table where VDate BETWEEN SDate AND EDate))
OR (t.EDate IS NULL AND t.VDate IN (SELECT SDate FROM Table))
如果EDate不为null,查询将选择介于之间的VDate。if为null,则为另一个。问题是,不能以这种方式在where子句中使用if 因此,如果您比较的数据取决于EDate值,您可以这样做:
SELECT Column1 ,column2
from Table t
WHERE
(t.EDate IS NOT NULL AND t.VDate IN (SELECT VDate FROM Table where VDate BETWEEN SDate AND EDate))
OR (t.EDate IS NULL AND t.VDate IN (SELECT SDate FROM Table))
如果EDate不为null,查询将选择介于之间的VDate。if为null,则为另一个。问题是,不能以这种方式在where子句中使用if 因此,如果您比较的数据取决于EDate值,您可以这样做:
SELECT Column1 ,column2
from Table t
WHERE
(t.EDate IS NOT NULL AND t.VDate IN (SELECT VDate FROM Table where VDate BETWEEN SDate AND EDate))
OR (t.EDate IS NULL AND t.VDate IN (SELECT SDate FROM Table))
如果EDate不为null,查询将选择介于之间的VDate。如果为空,则使用另一个。尝试以下操作:
SELECT Column1, Column2
from MyTable
where (VDate between @SDate and @EDate and (@SDate+@EDate is not null)) or (VDate = @Sdate and @Edate is null)
另外@Philip Kelley answer是一个很好的方法尝试以下方法:
SELECT Column1, Column2
from MyTable
where (VDate between @SDate and @EDate and (@SDate+@EDate is not null)) or (VDate = @Sdate and @Edate is null)
另外@Philip Kelley answer是一个很好的方法尝试以下方法:
SELECT Column1, Column2
from MyTable
where (VDate between @SDate and @EDate and (@SDate+@EDate is not null)) or (VDate = @Sdate and @Edate is null)
另外@Philip Kelley answer是一个很好的方法尝试以下方法:
SELECT Column1, Column2
from MyTable
where (VDate between @SDate and @EDate and (@SDate+@EDate is not null)) or (VDate = @Sdate and @Edate is null)
另外@Philip Kelley answer是一个很好的方法您的查询中有一些输入错误,至少
select
关键字是错误的,我猜您在投影中引用了column2
,而不是将column
别名为2
…您的查询中有一些输入错误,至少select
关键字是错误的,我猜您在投影中引用了column2
,而不是将column
别名为2
…您的查询中有一些输入错误,至少select
关键字是错误的,我猜您在投影中引用了column2
,而不是将column
别名为2
…您的查询中有一些输入错误,至少<代码>选择< /COD>关键字是错误的,我猜你在投影中引用了<代码>栏2<代码>代替别名<代码>列< /代码>作为<代码> 2代码>…请考虑对你的答案进行详细说明。你能解释这个代码(在你的答案中)吗?那样你可能会得到更多的选票!请稍作解释,请仔细考虑一下你的答案。你能解释一下(你的答案)吗?那样你可能会得到更多的选票!请稍作解释,请仔细考虑一下你的答案。你能解释一下(你的答案)吗?那样你可能会得到更多的选票!请稍作解释,请仔细考虑一下你的答案。你能解释一下(你的答案)吗?那样你可能会得到更多的选票!试着多解释一点…非常感谢你的帮助:)…非常感谢你的帮助:)…非常感谢你的帮助:)…非常感谢你的帮助:)…非常感谢你的帮助:)