Solved

Executing a Stored Procedure with a DATE

Posted on 2006-11-14
3
370 Views
Last Modified: 2008-02-26
Hi,

I've got a stored procedure:

CREATE PROCEDURE TestTable.spTest
      @StartDateGreaterThan SMALLDATETIME,
      @StartDateLessThan SMALLDATETIME
AS
   SELECT * FROM TestTable
   WHERE StartDate > @StartDateGreaterThan AND StartDate < @StartDateLessThan

When I try to execute it, via:

EXECUTE TestTable.spTest 1-1-1900, 1-1-2100

I get:

Incorrect syntax near '-'.

Here's what else I've tried:

EXECUTE TestTable.spTest '1-1-1900', '1-1-2100'
-> Error converting data type varchar to smalldatetime.
EXECUTE TestTable.spTest #1-1-1900#, #1-1-2100#
-> Incorrect syntax near '-'.
EXECUTE TestTable.spTest '1/1/1900', '1/1/2100'
-> Error converting data type varchar to smalldatetime.
EXECUTE TestTable.spTest 1/1/1900, 1/1/2100
-> Incorrect syntax near '/'.


What is the correct way to call this stored procedure?
0
Comment
Question by:CMES-IT
[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
  • 2
3 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17941108
EXECUTE TestTable.spTest '1900-01-01', '2100-01-01'
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 17941114
or, the professional way:

DECLARE @s datetime
DECLARE @e datetime
SET @s = CONVERT(datetime, '1900-01-01', 120)
SET @e = CONVERT(datetime, '2100-01-01', 120)

EXECUTE TestTable.spTest @s, @e
0
 

Author Comment

by:CMES-IT
ID: 17941165
Hi Angel,

Thanks,

This:
EXECUTE TestTable.spTest '1900-01-01', '2100-01-01'
Produces: Error converting data type varchar to smalldatetime.

And This:

DECLARE @s datetime
DECLARE @e datetime
SET @s = CONVERT(datetime, '1900-01-01', 120)
SET @e = CONVERT(datetime, '2100-01-01', 120)

EXECUTE TestTable.spTest @s, @e

Produces:
Error converting data type varchar to smalldatetime.

However, that second one got me on the right track.

The correct syntax is:

DECLARE @s smalldatetime
DECLARE @e smalldatetime
SET @s = CONVERT(smalldatetime, '1950-01-01', 120)
SET @e = CONVERT(smalldatetime, '2050-01-01', 120)

EXECUTE Accounting.spAccountCodeTest @s, @e

That works!

Thanks for getting me on the right track ;)

0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

734 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