Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / DB Engine / SQL Server / March 2008

Tip: Looking for answers? Try searching our database.

CASE problems with select statement.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
forest demon - 07 Mar 2008 05:12 GMT
I'm trying to build a select statement using the CASE expression. All
I want is to build the WHERE piece of the select statement based on a
parameter value.

I want to somehow CASE the WHERE clause based on a parameter of let's
say, @DateType

SELECT *
FROM <table>
WHERE   <columnA>   >=   @BeginDate  AND   <columnA>   <= @EndDate

SELECT *
FROM <table>
WHERE   <columnB>   >=   @BeginDate  AND   <columnB>   <= @EndDate

I know there is something simple i'm over looking.

thanks folks...

-fd
Mike C# - 07 Mar 2008 05:23 GMT
Are you talking about something like this? Note this sample works in the
AdventureWorks sample database:

DECLARE @start DATETIME;
DECLARE @end DATETIME;
DECLARE @dateType VARCHAR(5);

SELECT @start = '2001-08-01',
 @end = '2001-08-15',
 @dateType = 'Ship';

SELECT *
FROM Sales.SalesOrderHeader
WHERE CASE @dateType WHEN 'Order' THEN OrderDate
 WHEN 'Due' THEN DueDate
 WHEN 'Ship' THEN ShipDate
 END BETWEEN @start AND @end;

> I'm trying to build a select statement using the CASE expression. All
> I want is to build the WHERE piece of the select statement based on a
[quoted text clipped - 16 lines]
>
> -fd
forest demon - 07 Mar 2008 06:01 GMT
> Are you talking about something like this? Note this sample works in the
> AdventureWorks sample database:
[quoted text clipped - 36 lines]
>
> - Show quoted text -

sorry, i should have added a little more info.  i use this in a stored
procedure and
i have the following thus far.

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[spDataReaderStoredProcedure]
(
    -- Add the parameters for the stored procedure here
    @BeginDate datetime,
    @EndDate datetime,
    @DateType varchar(50)
)
AS
BEGIN

    SET NOCOUNT ON;

SELECT *
FROM Permits
WHERE CASE @dateType
 WHEN 'S_DATE' THEN 'S_DATE >= @BeginDate AND S_DATE <= @EndDate'
 WHEN 'G_DATE' THEN 'G_DATE >= @BeginDate AND G_DATE <= @EndDate'
END

END

the DateType comes in as one of two different date types. DateType
either comes
in as 'G_DATE' or 'S_DATE'.  the columns in the table are also G_DATE
and S_DATE.

thanks for your time...
forest demon - 07 Mar 2008 07:48 GMT
> > Are you talking about something like this? Note this sample works in the
> > AdventureWorks sample database:
[quoted text clipped - 78 lines]
>
> - Show quoted text -

i finally figured it out....damn, i hate making silly mistakes.....

thanks for your input mike....

-fd
Randy Pitkin - 11 Mar 2008 18:38 GMT
There is case, But there is also Between

Select * from table where DateColumn Between @BeginDate and @enddate

Case should not be used in a where.

Better to create a Table Variable and load from an index of Quailifing
Primary keys.
Then Join on the Table variable.

!0 fold performance increase.

-R

> I'm trying to build a select statement using the CASE expression. All
> I want is to build the WHERE piece of the select statement based on a
[quoted text clipped - 16 lines]
>
> -fd
Mike C# - 12 Mar 2008 02:49 GMT
> There is case, But there is also Between
>
[quoted text clipped - 7 lines]
>
> !0 fold performance increase.

BETWEEN replaces the >= AND <= but doesn't do anything for the OP's question
of dynamically determining which column to use to limit rows.  Whether or
not he needs a performance increase probably depends on how much data he's
querying, etc.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.