Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

simple date range prompt in sql view

Posted on 2011-03-22
6
Medium Priority
?
701 Views
Last Modified: 2012-08-13
I want my view to prompt me for a date range when I run it.  Currently the dates are hard coded.
SELECT     TOP (100) PERCENT tDate, tActivityType, tUnits AS Total
FROM         dbo.AxTransaction
WHERE     (tUnits <> 0) AND (tActivityType = 1) AND (tDate >= CONVERT(DATETIME, '2010-01-01 00:00:00', 102)) AND (tDate <= CONVERT(DATETIME, '2010-12-31 00:00:00', 102))

How do I replace hard coded dates with prompts or parameters?

Thanks
0
Comment
Question by:mbrennan_pacbell_net
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 35195778
You cannot do that in view. Create an sp and pass the dates as parameters.
0
 
LVL 9

Expert Comment

by:kaminda
ID: 35195793
You will have to use either a function of sp to do this. My advice is to use an Table Valued Function so it can be used as a view in your queries.

Here is TBV Function script for your view,
FUNCTION [dbo].[fnGetAxTransaction]
(
	@FromDate DateTime,
    @ToDate DateTime
)


RETURNS  TABLE
AS 
RETURN
(
SELECT     TOP (100) PERCENT tDate, tActivityType, tUnits AS Total
FROM         dbo.AxTransaction
WHERE     (tUnits <> 0) AND (tActivityType = 1) AND (tDate >= @FromDate AND (tDate <= @ToDate)
)

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 35198549
And please lose the TOP (100) PERCENT, it is pointless.
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Author Comment

by:mbrennan_pacbell_net
ID: 35200260
kaminda,

Thanks your solution worked somwhat.  I was able to create the table value funcation based on your reply.  However when I try to use it in a view, I get the following error:

"Incorrect Syntax Near ","

I am a SQL nube, so be gentle with me.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 35200466
The paranthesis are wrong it should be:
     WHERE  tUnits <> 0
            AND tActivityType = 1
            AND tDate BETWEEN @FromDate AND @ToDate
0
 
LVL 9

Accepted Solution

by:
kaminda earned 500 total points
ID: 35204342
It should change as below as acperkins correctly points out there is a additional paranthesis in the where clause
FUNCTION [dbo].[fnGetAxTransaction]
(
	@FromDate DateTime,
    @ToDate DateTime
)


RETURNS  TABLE
AS 
RETURN
(
SELECT     TOP (100) PERCENT tDate, tActivityType, tUnits AS Total
FROM         dbo.AxTransaction
WHERE     tUnits <> 0 AND tActivityType = 1 AND (tDate >= @FromDate AND tDate <= @ToDate)
)

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

610 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