Welcome to Dream.In.Code
Become an Expert!

Join 149,824 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,605 people online right now. Registration is fast and FREE... Join Now!




Using DateTime

 
Reply to this topicStart new topic

Using DateTime, with msSQL

myNameIsBob
11 Feb, 2007 - 03:46 PM
Post #1

New D.I.C Head
*

Joined: 11 Feb, 2007
Posts: 1


My Contributions
I have 2 datetime fields in my database and i need help with how to use them

one of the things i need to do is find all the records that are say 2002 Dec and then something else am trying to do is take the first datetime field and add 10 days to it and see if its larger then the second field

Thanks for any help
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Using DateTime
19 Feb, 2007 - 02:44 AM
Post #2

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(myNameIsBob @ 11 Feb, 2007 - 04:46 PM) *

I have 2 datetime fields in my database and i need help with how to use them

one of the things i need to do is find all the records that are say 2002 Dec and then something else am trying to do is take the first datetime field and add 10 days to it and see if its larger then the second field

Thanks for any help

Hi Bob

Some DB SQL implementations will not be exactly like this. But something like:

CODE
SELECT *
FROM TBL
WHERE DateField BETWEEN DATE("12-01-2002) AND DATE("12-31-2002")


CODE
SELECT *
FROM TBL
WHERE DateField1+10 > DateField2


You may have to look up date and time manipulation to ensure this is correct for your DB..

Good luck
User is offlineProfile CardPM
+Quote Post

jstephens
RE: Using DateTime
23 Feb, 2007 - 08:19 PM
Post #3

D.I.C Head
**

Joined: 7 Nov, 2005
Posts: 192



Thanked: 1 times
My Contributions
Datetime is a combination of date data type and the time data type so when pulling queries the format would be somewhat like
YYYY-MM-DD HH:MM:SS.

So take what gregoryH said and put in your time.
CODE

SELECT *
FROM TBL
WHERE DateField BETWEEN DATE("12-01-2002 00:00:00") AND DATE("12-31-2002 23:59:59")


which the above code would select information from 12-01-2002 begining of the day to 12-31-2002 end of day. which really is the whole month.

Even though the datetime combination is nice when I know I am going to be sorting with dates I usally use like to seperate the date and time into their own fields. Just a suggestion I use.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 09:13AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month