有如下要求的一个 sql 语句:select A.*,B.* from Company_Info AS A,Company_Pub_Position AS B  where A.Company_ID=B.Company_IDand 当满足以下这个条件时A.StartDate is  null or ( A.DealClerk='hasdel' or A.DealClerk is null )
,则要执行这个条件valid_date =convert( varchar(10),dateadd( month,B.Pub_Position_ValidDate,B.Pub_Position_Date),120)>=getdate()否则执行以下这个条件cast(A.EndDate as datetime)>=getdate()这样的 sql 语句要如何拼写呢?是不是用 case then else 啊,但是我不会俱体如何拼写

解决方案 »

  1.   

    select A.*,B.* from Company_Info AS A,Company_Pub_Position AS B  where A.Company_ID=B.Company_IDand (case
    when
    A.StartDate is  null or ( A.DealClerk='hasdel' or A.DealClerk is null )
    then
    valid_date =convert( varchar(10),dateadd( month,B.Pub_Position_ValidDate,B.Pub_Position_Date),120)>=getdate()
    else
    cast(A.EndDate as datetime)>=getdate()
    end)
      

  2.   

    (A.StartDate is  null or ( A.DealClerk='hasdel' or A.DealClerk is null )) and (valid_date =convert( varchar(10),dateadd( month,B.Pub_Position_ValidDate,B.Pub_Position_Date),120)>=getdate())or (A.StartDate is not null and ( A.DealClerk<>'hasdel' or A.DealClerk is not  null )) and cast(A.EndDate as datetime)>=getdate() )
      

  3.   

    (A.StartDate is  null or ( A.DealClerk='hasdel' or A.DealClerk is null )) and (valid_date =convert( varchar(10),dateadd( month,B.Pub_Position_ValidDate,B.Pub_Position_Date),120)>=getdate())or ((A.StartDate is not null and A.DealClerk<>'hasdel' and A.DealClerk is not  null )and cast(A.EndDate as datetime)>=getdate() )
      

  4.   

    caixia615(┌睜眼為殤.2ǒǒ6.閉眼為塵┐) 你的方法在以下这句上, sql 查询器提示出错:第 9 行: '=' 附近有语法错误。----------------------------------------------------valid_date =convert( varchar(10),dateadd( month,B.Pub_Position_ValidDate,B.Pub_Position_Date),120)>=getdate()
      

  5.   

    fesxe(想飞的鸟) 你没理解题目意思,是多个条件判断的问题,你的答案显然是不对的
      

  6.   

    用union组合语句可不可以阿:
    select A.*,B.* from Company_Info AS A,Company_Pub_Position AS B  
    where A.Company_ID=B.Company_ID
    and valid_date =convert( varchar(10),dateadd( month,B.Pub_Position_ValidDate,B.Pub_Position_Date),120)>=getdate()
    and (A.StartDate is  null or A.DealClerk='hasdel' or A.DealClerk is null ))
    union
    select A.*,B.* from Company_Info AS A,Company_Pub_Position AS B  
    where A.Company_ID=B.Company_ID
    and cast(A.EndDate as datetime)>=getdate()
    and A.StartDate is not null and A.DealClerk<>'hasdel' and A.DealClerk is not null
      

  7.   

    --这样吗?select A.*,B.* from Company_Info AS A,Company_Pub_Position AS B  where A.Company_ID=B.Company_ID
    and ((A.StartDate is  null or ( A.DealClerk='hasdel' or A.DealClerk is null ) and valid_date =convert( varchar(10),dateadd( month,B.Pub_Position_ValidDate,B.Pub_Position_Date),120)>=getdate()) or cast(A.EndDate as datetime)>=getdate())
      

  8.   

    楼上,怎么能全用 and 呢,这是要根据不同条件来判断的,最起码得用到case when else end吧-------------------------自己顶一下!
      

  9.   

    怎么回事,单就这句话就出错呢valid_date =convert( varchar(10),dateadd( month,B.Pub_Position_ValidDate,B.Pub_Position_Date),120)>=getdate())
      

  10.   

    有這樣的條件嗎?       valid_date =convert( varchar(10),dateadd( month,B.Pub_Position_ValidDate,B.Pub_Position_Date),120)
    >=getdate()
    干什么要加上這個判斷 ?  >=getdate()
      

  11.   

    单这一句 sql 语句的条件,在查询分析器中可以通过select ..........................where cast(convert( varchar(10),dateadd( month,B.Pub_Position_ValidDate,B.Pub_Position_Date),120) as datetime) >=getdate()
    这句话没有问题!