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

x
?
Solved

SQL stored procedure datetime question

Posted on 2008-06-25
9
Medium Priority
?
2,640 Views
Last Modified: 2010-04-21
Hi i have a stored procddue please see attched

to execute the procedure i need to use

execute empcount 'Dec 1 2007', 'dec 31 2008'

the problem is the application that send the execute command want to use

exec empcount(2000-01-01 00:00:00.000000000,
2000-01-01 00:00:00.000000000)

can i tweak the procedure to accept these values?

--Declare Start and End variables (these can also be passed into a stored procedure)
DECLARE @startdate 	smalldatetime,
	@endDate   	smalldatetime
 
--this drops the hours and minutes and sets them to 00:00
Set @startdate  = convert(varchar,'Nov 1, 2008' ,111)
Set @endDate  = convert(varchar,'Dec 31 2008' ,111)
 
--Declare EnD Date tmp variable to be used in the loop
DECLARE @tmpEndDate	smalldatetime
--Declare tmp table to act as a temporary holding place for your data
DECLARE @tmpTable	TABLE(
			StartDate	smalldatetime,
			EndDate		smalldatetime,
			noemp 		integer)
--Set @startdate to beginning of month span
Set @startdate =  DATEADD(dd,-(DAY(DATEADD(m,1,@startdate))-1),@startdate)
--set @endDate to end of month span
Set @endDate =  DATEADD(dd, -DAY(DATEADD(m,1,@endDate)), DATEADD(m,1,@endDate))
 
--loop through each month
While @startdate < @endDate
BEGIN
         --set @tmpEndDate to last minute of month
	Set @tmpEndDate = dateadd(n,-1,dateadd(m,1,@startdate))
         --insert values in table
	Insert into @tmpTable(StartDate,EndDate,noemp) 
		Select @startdate as startdate, @tmpEndDate as enddate, NoEmployees as noemp 
		      from CTContracts 
		      Where Fromdate  <= @tmpEndDate And  EndDate >= @startdate
		      and LKContractTypeID = 8
         --increment @startdate 
	Set @startdate = dateadd(m,1,@startdate)
END
 
--Now you can do what you want with the results
Select * from @tmpTable
Select Min(StartDate) as StartDate, Max(EndDate) as EndDate, Sum(noemp) as noemp from @tmpTable

Open in new window

0
Comment
Question by:ac_davis2002
[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
9 Comments
 
LVL 2

Accepted Solution

by:
pierky earned 2000 total points
ID: 21864795
Well,

this syntax is wrong
exec empcount(2000-01-01 00:00:00.000000000, 2000-01-01 00:00:00.000000000)

At least you have to remove "(" and ")" and add quotes to the date:

exec empcount '2000-01-01 00:00:00.000000000', '2000-01-01 00:00:00.000000000'

If you can run this command (and if input date is always in this format) you can change the stored-procedure in this way:

CREATE PROCEDURE empcount (
@strStart varchar(30),
@strStop varchar(30)
) AS
 
DECLARE @startdate 	smalldatetime
DECLARE @endDate   	smalldatetime
SET @startdate = CONVERT( datetime, LEFT( @strStart, 23 ), 121 )
SET @endDate = CONVERT( datetime, LEFT( @strStop, 23 ), 121 )
 
[...]

Open in new window

0
 
LVL 2

Expert Comment

by:HengTime
ID: 21864809
you could pass the parameters (the dates)  as nvarchar  and then convert them to datetime

have a look at this. its the convert syntax

http://msdn.microsoft.com/en-us/library/ms187928.aspx

the style you need I think would be 121 which is odbc canonical with millisecs
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21864994
if the parameter is already datetime (or smalldatetime), you don't need to convert them "again" to varchar or datetime!

>exec empcount(2000-01-01 00:00:00.000000000, 2000-01-01 00:00:00.000000000)
I must assume that it actually calls the proc like this:
exec empcount('2000-01-01 00:00:00.000000000', '2000-01-01 00:00:00.000000000')

in which case, if the parameter is datetime, it will accept it (smalldatetime might be a problem)
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

Author Comment

by:ac_davis2002
ID: 21865012
Hi chaps

pierky, i can remove thr brackets but I need to insert a quote
in otherwords I can send this
2000-01-01 00:00:00.000000000, 2000-01-01 00:00:00.000000000

but need a way of inserting quotes

'2000-01-01 00:00:00.000000000', '2000-01-01 00:00:00.000000000'
0
 
LVL 2

Expert Comment

by:pierky
ID: 21865264
The syntax

exec empcount 2000-01-01 00:00:00.000000000, 2000-01-01 00:00:00.000000000

is wrong, SQL server will not parse this command.
You need to change the way your application call the stored-procedure, adding quotes.
What's the language used to build the app? Do you have source code?
0
 

Author Comment

by:ac_davis2002
ID: 21865371
well actuall its cognos, I am calling a sql procedue from cognos. do you have any cognos knowledge?
0
 
LVL 2

Expert Comment

by:pierky
ID: 21865427
No, sorry!
0
 

Author Comment

by:ac_davis2002
ID: 21865514
no worries, thanks for your help anyway

I'll pass you the poinst as you answered my original question!
0
 

Author Closing Comment

by:ac_davis2002
ID: 31470522
thnaks again!
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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

650 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