SQL Conditional Where Clause Not Use Subquery?

Posted on 2014-07-14
Last Modified: 2014-07-14
I have a typical Stored Procedure using a Where clause and Max(Update_Date) subquery. I need to conditional NOT use the Max(Update_Date) query, completely by-pass it.  I haven't written a query like this before and unsure how to proceed, help...

1. If @myVariableString is empty, then use the Max(Update_Date) query
2. If @myVariableString not empty, then by-pass the Max(Update_Date) query all together.

@myVariableString varchar(4000)

Select   * 
From    Advertising a
Where Case When @myVariableString = '' 
                      Then  Update_Date = (Select Max(Update_Date) 
                                                      From Advertising b 
                                                      Where a.Name = b.Name)

Open in new window

Question by:WorknHardr
    LVL 21

    Expert Comment

    by:Randy Poole
    Not 100% what your looking for, but see if this is it:

    Select   *,case when isNull(@myVariableString,'')='' then 
    (Select top 1 Update_Date From Advertising b Where a.Name = b.Name order by Update_Date desc) 
    else Update_Date end as Update_Datenew
    From    Advertising a

    Open in new window

    LVL 68

    Accepted Solution

    Select   *
    From    Advertising a
        (@myVariableString > '' OR a.Update_Date = (Select Max(b.Update_Date)
                                                          From Advertising b
                                                          Where a.Name = b.Name))

    Author Closing Comment

    Excellent! thx

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
    Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now