473,378 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Passing DateDiff rather than Actual Dates as Parameters

I've gotten sort of fed up with dealing with regional date settings on
the client side and am considering the following scheme - just
wondering if anyone has a negative view of it or not:

Instead of

@StartDate datetime,
@EndDate datetime

Use:

@StartDaysDiff int,
@EndDaysDiff int
In the front end app take the desired date and do a DATEDIFF with the
current date, then pass the date diff as a parameter as an integer
rather than deal with the dates at all.

Then...

DECLARE @TodayDate datetime, @StartDate datetime, @EndDate datetime

SELECT @TodayDate = GETDATE()
SELECT @StartDate = DATEDIFF(DAY,@StartDaysDiff,@TodayDate)
SELECT @EndDate = DATEDIFF(DAY,@EndDaysDiff,@TodayDate)

SELECT
a.Something
FROM
dbo.Appointments a
WHERE
a.AppointmentDate BETWEEN @StartDate and @EndDate

Just wondering ...

Thanks,
lq

Nov 29 '05 #1
9 3263
Oops,
I meant:

SELECT @TodayDate = GETDATE()
SELECT @StartDate = DATEADD(DAY,@StartDaysDiff,@TodayDate)
SELECT @EndDate = DATEADD(DAY,@EndDaysDiff,@TodayDate)

Nov 29 '05 #2
laurenq uantrell (la*************@hotmail.com) writes:
I've gotten sort of fed up with dealing with regional date settings on
the client side and am considering the following scheme - just
wondering if anyone has a negative view of it or not:

Instead of

@StartDate datetime,
@EndDate datetime

Use:

@StartDaysDiff int,
@EndDaysDiff int
In the front end app take the desired date and do a DATEDIFF with the
current date, then pass the date diff as a parameter as an integer
rather than deal with the dates at all.


You could run into nasty surprises if the client and server are in
different time zones. Then again, if you instead of using the current
date as your base date, took a fixed date such as 1900-01-01, you'd be safe.
Then again, this is also confusing as 1900-01-01 is the base date in SQL
Server while in Visual Basic it's 1899-12-30.

And I don't really see how this relieves you from dealing with regional
settings on the client side. After all, the user inputs a date, and the
interpretation of 1/7/9 is different depending on where you are. And
to transform that to a number, you still have to interpret it.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Nov 30 '05 #3
On the client side, it's pretty simple since date() remains in the
chosen format as long as I never format it, so Nov.29, 2005 remains
29/11/2005 or 11/29/2005 depending on the regional format - the
DATEDIFF on the client side will always calculate correctly. What I
need to do is to translate the client date to UTC/GMT and pass that as
an integer to the server to handle the time zone differences as I have
shown below:

@GMTOffset int, /* client GMT offset in minutes - example NYC = 300
*/
@StartDays int, /* Number of days from today's date */
@EndDays int /* Number of days from today's date */

AS

DECLARE @ClientUTCDIFF int, @ServerUTCDIFF int, @ServerStartDate
datetime, @ServerEndDate datetime

/* determine the differnence between user client GMT offset and server
GMT Offset: */
SELECT @ServerUTCDIFF = DATEDIFF(MINUTE, GETDATE(), GETUTCDATE())
SELECT @ClientUTCDIFF = @GMTOffset - @ServerUTCDIFF

/* set server start date/time to GMT hour at midnight user's time today
- example NYC = 5am GMT*/
SELECT @ServerStartDate = DATEADD(MINUTE,@ServerUTCDIFF +
@ClientUTCDIFF,CAST(CONVERT(nvarchar(10),GETDATE() ,101) AS Datetime))

/* set server end date/time to GMT hour at 11:59 pm user's time today -
example NYC = 4:49 am GMT*/
SELECT @ServerEndDate = DATEADD(MINUTE,1439,@ServerStartDate)

/* adjust the server date/time to the filter date range requested by
the user: */
SELECT @ServerStartDate = DATEADD(DAY,@StartDays, @ServerStartDate)
SELECT @ServerEndDate = DATEADD(DAY,@EndDays,@ServerEndDate)

/* display the date parameters that will be used: */

SELECT
@ServerStartDate AS ServerStartDate,
@ServerEndDate AS ServerEndDate

Nov 30 '05 #4
Lauren,
I'll repeat the advice I gave you a couple of days ago in
comp.databases.ms-access
------------------------------------
The easiest way to handle passing dates to SQL Server is to pass it in one
of the formats which Convert understands, then immediately convert it to a
datetime variable.

So for instance you could pass a date in international format.

Format(Date, "YYYYMMDD")

and then in your SP

Create Procedure usp_some_proc
@indate varchar(10)
AS
Declare @real_date datetime

Select @real_date = Convert(datetime, @indate, 112)
-- Then you can just work with @real_date and not worry about the format
------------------------------------

--
Terry Kreft

"laurenq uantrell" <la*************@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I've gotten sort of fed up with dealing with regional date settings on
the client side and am considering the following scheme - just
wondering if anyone has a negative view of it or not:

Instead of

@StartDate datetime,
@EndDate datetime

Use:

@StartDaysDiff int,
@EndDaysDiff int
In the front end app take the desired date and do a DATEDIFF with the
current date, then pass the date diff as a parameter as an integer
rather than deal with the dates at all.

Then...

DECLARE @TodayDate datetime, @StartDate datetime, @EndDate datetime

SELECT @TodayDate = GETDATE()
SELECT @StartDate = DATEDIFF(DAY,@StartDaysDiff,@TodayDate)
SELECT @EndDate = DATEDIFF(DAY,@EndDaysDiff,@TodayDate)

SELECT
a.Something
FROM
dbo.Appointments a
WHERE
a.AppointmentDate BETWEEN @StartDate and @EndDate

Just wondering ...

Thanks,
lq

Nov 30 '05 #5
laurenq uantrell (la*************@hotmail.com) writes:
On the client side, it's pretty simple since date() remains in the
chosen format as long as I never format it, so Nov.29, 2005 remains
29/11/2005 or 11/29/2005 depending on the regional format - the
DATEDIFF on the client side will always calculate correctly. What I
need to do is to translate the client date to UTC/GMT and pass that as
an integer to the server to handle the time zone differences as I have
shown below:


Or just pass it as a parameter to your queries, and the client API
will convert the date to a binary format. (Which accidently is based
on the number of days since 1900-01-01.)

Your diff stuff sounds overly complex to me.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Nov 30 '05 #6
Erland,
Why pass it as a string and then convert it to a date? In your example
you're passing the formatted date (Format(Date, "YYYYMMDD") ) as a
string to the varchar parameter, then converting it to a date in
@real_date? Why not just pass it to a datetime parameter in the first
place and have the error reporting of the client deal with formatting
problems rather than the vague-aries of SQL error reporting?

@indate varchar(10)
AS
Declare @real_date datetime
Select @real_date = Convert(datetime, @indate, 112)

Dec 1 '05 #7
Maybe you're right but it's pretty bullet proof and passing integers
seems preferable to passing strings. Plus, I'm always dealing with date
and time as opposed to date when dealing with clients in multiple time
zones, so since everthing is converted to UTC anyway, I figure why not
just look at dealing with DATEDIFF as opposed to date parameters. I
just throw this out as a theoretical since I am already heavily
invested in datetime parameters in my current projects.
I imagine this would have implications on query performance as well
since the stored procedures would be using DECLARE'd variables as WHERE
filters (WHERE StartDate BETWEEN @ServerStartDate and @ServerEndDate...)

Dec 1 '05 #8
laurenq uantrell (la*************@hotmail.com) writes:
Erland,
Why pass it as a string and then convert it to a date? In your example
you're passing the formatted date (Format(Date, "YYYYMMDD") ) as a
string to the varchar parameter, then converting it to a date in
@real_date? Why not just pass it to a datetime parameter in the first
place and have the error reporting of the client deal with formatting
problems rather than the vague-aries of SQL error reporting?

@indate varchar(10)
AS
Declare @real_date datetime
Select @real_date = Convert(datetime, @indate, 112)


Well, it wasn't me who suggested that, and I entirely agree with you.
Passing datetime values as strings is a poor idea.


--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Dec 1 '05 #9
laurenq uantrell (la*************@hotmail.com) writes:
Maybe you're right but it's pretty bullet proof and passing integers
seems preferable to passing strings.
And binary values are even better. You should not pass dates as strings
to SQL Server. You should pass them as datetime values.
I imagine this would have implications on query performance as well
since the stored procedures would be using DECLARE'd variables as WHERE
filters (WHERE StartDate BETWEEN @ServerStartDate and @ServerEndDate...)


Yes, if these are local variables and not parameters, SQL Server cannot
do parameter sniffing on them, but will apply standard guesses.

One way to circumvent this is to have a wrapper SP which unpacks the
integer values, and then call an inner SP which have datetime parameters.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Dec 1 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tim::. | last post by:
I am having some difficulties with the function Datediff! I am trying to calculate the number of days between two given date E.G: Number days between 03-05-2004 and 05-05-2004 = I am using the...
5
by: Andy | last post by:
Hi Could someone clarify for me the method parameter passing concept? As I understand it, if you pass a variable without the "ref" syntax then it gets passed as a copy. If you pass a...
4
by: Paolo | last post by:
I am having some problem with a Year Function. I have form on which I have 4 field which indicate dates and an additional form which sums those dates: These are the fields: YEARS...
5
by: mcbill20 | last post by:
Hello all. I have a really basic question that I hope someone has a better answer for. I apologize in advance-- I know this is probably a really basic question but I am used to Oracle rathern than...
2
by: Contro | last post by:
Hi there! I hope you can help me! I have the following code: SELECT COUNT(Applications.) AS TOTAL FROM Applications WHERE DateDiff('d',,Date()) <6 AND = "Certificate"; Which returns the...
17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
6
by: kevinjwilson | last post by:
I am trying to get the date difference between two dates but I don't want the function to include weekends in the calculation. Does anyone have an idea on how to make this work?
3
by: Mike P | last post by:
I am entering two dates into textboxes and using them as control parameters in an SQL DataSource which is being used to populate a gridview. I am using a custom validator to check if the data...
11
by: =?Utf-8?B?U3VqZWV0?= | last post by:
If there are long strings (like 1MB or 2MB) is it more performant to pass those by ref to methods or by value?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.