Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 5th, 2006, 06:18 AM
Registered User
 
Join Date: Jun 2006
Location: Newcastle, , United Kingdom.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default calculate a number between two dates in time

Hi

I hope someone can help -

This is where im at -

Im trying to calculate a number between two dates in time.

I have a table of employees with in which I have a field name called datejoined.

My aim is to calculate the number of employees joined within say the last 2 days.

Here is my code

SELECT count(*) FROM Userdetails WHERE datejoined between convert(varchar(10),getdate()-2,103) and convert(varchar(10),getdate(),103)

This should return all employees joined between 03/09/2006 and 05/09/2006
When I run this today which is on the 5th it returns all counts for dates which have 05, 04, and 03 as the day - and it will return any month! ie it will happily return numbers for employees who joined on 05/06/2006.

Im conpletely stuck with this, any help would be greatly appreciated.

Thanks

Tim

Reply With Quote
  #2 (permalink)  
Old September 5th, 2006, 06:49 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Why are you doing your BETWEEN test on varchar strings?

Since your comparison is using strings, the selection is based on string values, and not dates, so it's doing the 'right' thing for the datatypes involved.

Is the datatype of the 'datejoined' column a varchar? Why?

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
Reply With Quote
  #3 (permalink)  
Old September 5th, 2006, 07:00 AM
Registered User
 
Join Date: Jun 2006
Location: Newcastle, , United Kingdom.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The datejoined field is datetime -

The way I understood it was that getdate() which I need to use to pin point my two dates in time has the format dd/mm/yyyy hh:mm:ss so I used the convert to trim the getdate() to just dd/mm/yy.

If this is my problem - how can I compare using datetime and not string values?

Reply With Quote
  #4 (permalink)  
Old September 5th, 2006, 07:17 AM
Authorized User
 
Join Date: Jun 2003
Location: , , Norway.
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi.

You could do like this:

select cast(convert(varchar,getdate()-2,103 ) as datetime)

Gert

Reply With Quote
  #5 (permalink)  
Old September 5th, 2006, 07:23 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

GetDate() doesn't have a format. The function returns a value of type datetime. Thus you can compare it directly to other datetime values.

Datetime value may have a default format - like dd/mm/yyyy hh:mm:ss which will be used when converting a datetime to a character string (you can override and/or change this), but if all your expressions are using datetime datatypes, then the string format has nothing to do with it.

Also, you'll be better off in the long run to use the date manipulation functions, like DATEADD, to modify datetime values, rather than directly subtracting, as:

    ...WHERE datejoined BETWEEN DATEADD(d, -2, GetDate()) AND GetDate()

Also, be careful of the time portion of a datetime. A datetime always contains a time portion as well as a date, so you must take care to account for that in your comparisons.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
Reply With Quote
  #6 (permalink)  
Old September 5th, 2006, 07:56 AM
Registered User
 
Join Date: Jun 2006
Location: Newcastle, , United Kingdom.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That worked a treat - thanks for a your help and time!

Reply With Quote
  #7 (permalink)  
Old September 5th, 2006, 01:03 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Naperville, IL, USA.
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Another way to handle that is as follows:

Convert the string back to a datetime
CAST(convert(varchar(10),getdate()-2,103) AS Datetime)

SELECT count(*) FROM Userdetails WHERE datejoined between CAST(convert(varchar(10),getdate()-2,103) AS datetime) and CAST(convert(varchar(10),getdate(),103) AS datetime)


Rand
Reply With Quote
  #8 (permalink)  
Old September 5th, 2006, 01:08 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Naperville, IL, USA.
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Jeff:

If you ran your code at 1:00pm, it would ignore any records dated 2006.09.03 but prior to 1:00pm.

Is that what the user really wants?


Rand
Reply With Quote
  #9 (permalink)  
Old September 5th, 2006, 01:14 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Naperville, IL, USA.
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sadly, there isn't a built in function which will return only the date part of a datetime as a datetime. ie. '2006.09.05 13:15:30.123' becomes '2006.09.05 00:00:00.000'.


Rand
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Urgent! how to calculate difference between dates Subuana Beginning VB 6 2 March 22nd, 2006 05:34 PM
How to calculate the work days beetwen two dates. andres_pm Javascript 1 October 7th, 2004 06:23 AM
how to calculate difference in dates ashu_gupta75 Excel VBA 3 August 9th, 2004 06:11 AM
How to Calculate dates in datagrid columns chiefouko VS.NET 2002/2003 2 July 15th, 2004 01:24 AM
calculate time between two dates RMorales2831 Access 1 February 13th, 2004 11:38 PM



All times are GMT -4. The time now is 04:19 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.