条件搜索日期字段 [英] Conditional search on date field

查看:57
本文介绍了条件搜索日期字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储过程,需要退回所有已接种疫苗或未接种疫苗的宠物。



这是我的存储过程:

I have a stored procedure that needs to return all pets that either have had all their vaccinations or have not had all their vaccinations.

Here is my stored procedure:

SELECT PetName, OwnerLName, OwnerFName, VaccinationDate  
  FROM [dbo].[myVetsOffice]
  WHERE (@PetName IS NULL OR PetName LIKE  '%' + @PetName + '%')
    AND (@OwnerLName IS NULL OR OwnerLName LIKE  '%' + @OwnerLName + '%')
    AND (@OwnerFName IS NULL OR OwnerFName LIKE  '%' + @OwnerFName + '%')
ORDER BY OwnerLname, OwnerFname	





我有一个Y / N参数,如果它是Y,我需要添加VaccinationDate IS NOT NULL如果Y / N参数是N,那么包括一个VaccinationDate为空的条款。



如何在我的选择中包含这个?我尝试包括一个AND CASE WHEN @myparameter ='Y'那么VaccinationDate不是NULL但是这不起作用。我怎么能包括最后一点逻辑呢?



我尝试过:



CASE声明,谷歌搜索,msdn网站,博客,堆栈溢出



I have a Y/N parameter that, if it is Y, I need to add VaccinationDate IS NOT NULL else if the Y/N parameter is N, then include a clause where the VaccinationDate is null.

How do I include this in my select? I tried including a AND CASE WHEN @myparameter = 'Y' THEN VaccinationDate IS NOT NULL but that is not working. How else can I include this last bit of logic?

What I have tried:

CASE statement, google search, msdn site, blogs, stack overflow

推荐答案

而不是在哪里,试试这个:
Instead of case where, try this:
WHERE (@myparameter = 'Y' AND VaccinationDate IS NOT NULL)
OR (@myparameter = 'N' AND VaccinationDate IS NULL)


For then部分情况下它只接受特定的值或列值,即它不会接受列不为null或列为空。



在那部分你必须制作另一个案例陈述并相应地写下你的逻辑。



谢谢
For then part of case it will accept only specific value or column value i.e it will not going to accept column is not null or column is null .

In then part you have to make another case statement and write you logic accordingly.

Thanks


这篇关于条件搜索日期字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆