473,385 Members | 1,445 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,385 software developers and data experts.

How to compare date and time field in sql query?

106 100+
Hi,
I am practising SQL Serverso my question is from the basics. I could not get the right answer from other search engines but i am sure i will be rightly guided here.

date and time are stored in a single field 'gps_date' and each field shows details like this: 2011-02-05 10:21:47.000
I need to filter the date and time field to list:
  • Todays Data
  • Data Between Two Dates

my very basic sql statement is show below. How can I add the above features to it.
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM gps_data where gps_device_id = '0123456'
  2.  
Feb 5 '11 #1

✓ answered by gpl

I frequently use this to strip off the time part (it gets converted to midnight)

Expand|Select|Wrap|Line Numbers
  1. SELECT DATEADD(D, 0, DATEDIFF(D, 0, GETDATE()))
so your query would be
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM gps_data where gps_device_id = '0123456'
  2. and
  3. DATEADD(D, 0, DATEDIFF(D, 0, gps_date))=DATEADD(D, 0, DATEDIFF(D, 0, GETDATE()))
  4.  
likewise for a daterange,
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM gps_data where gps_device_id = '0123456'
  2. and
  3. DATEADD(D, 0, DATEDIFF(D, 0, gps_date)) between convert(datetime,'2011-01-21') 
  4. and convert(datetime, '2011-02-22')
  5.  
Im pretty sure that putting the date into military format you dont need to specify a format code.
The above code pulls out all data between 2011-01-21 and 2011-02-21 (2011-02-22 is the same as the last moment of the 21st Feb)

3 8363
gpl
152 100+
I frequently use this to strip off the time part (it gets converted to midnight)

Expand|Select|Wrap|Line Numbers
  1. SELECT DATEADD(D, 0, DATEDIFF(D, 0, GETDATE()))
so your query would be
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM gps_data where gps_device_id = '0123456'
  2. and
  3. DATEADD(D, 0, DATEDIFF(D, 0, gps_date))=DATEADD(D, 0, DATEDIFF(D, 0, GETDATE()))
  4.  
likewise for a daterange,
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM gps_data where gps_device_id = '0123456'
  2. and
  3. DATEADD(D, 0, DATEDIFF(D, 0, gps_date)) between convert(datetime,'2011-01-21') 
  4. and convert(datetime, '2011-02-22')
  5.  
Im pretty sure that putting the date into military format you dont need to specify a format code.
The above code pulls out all data between 2011-01-21 and 2011-02-21 (2011-02-22 is the same as the last moment of the 21st Feb)
Feb 6 '11 #2
ck9663
2,878 Expert 2GB
Here, read this for additional info...

Good Luck!!!

~~ CK
Feb 7 '11 #3
tasawer
106 100+
Thanks for both replies..


great help
Feb 7 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: gil | last post by:
I'm trying to INSERT based on info selected from another table. How can I concatenate a string of time to the column 'myDate' below (the value being inserted)? INSERT INTO c5...
2
by: JP SIngh | last post by:
Can someone please suggest the correct way to store the date & time in SQL Server? I want to store the date and time a record was created. What should the field type be in SQL Server? How can...
5
by: Javier | last post by:
I have a field type Date/Time that automatically defaults to Now(). I'm having a problem writing a query that will retrieve all the records for a specific date the user to enters when prompted. I...
9
by: MLH | last post by:
I have a database (datatrek.mdb) with a table named DATA. The table has a date/time field with default value = Now(). It has 100 records in it entered over a 50-minute period. I would like the...
2
by: kaka | last post by:
I'm runnig Redhat 7.3 with postgresql 7.3.4 builded from sources and upgraded recently. I receive this error ( never received before upgrade ) and I don' t know what to do. Error while executing...
11
by: Dixie | last post by:
How can I programatically, take some Date/Time fields present in a table in the current database and change their type to text? dixie
3
by: matturbanowski | last post by:
Hi, I have a date/time field in a SQL2000 database, and what I would like to do is to filter on a specific part of the field, for example the time or hour. Supposing I have a set of data for...
1
by: vai | last post by:
hi all, i'm developing a stock management system. in this system i'm developing a billing system which on daily & monthly basis. i'm using ms-acess database as backend & visual baic as front end....
9
by: Kyote | last post by:
I have a textbox that is databound to a table in an access database. The field only contains month/day/year but my databound textbox is also showing a time. Is there any way to prevent it from...
13
by: dizzydangler | last post by:
Just a quick question...I'm running an MS Access 2007 db that tracks appointments in a single table. Date and time are entered as separate fields in short date (mm/dd/yyyy) and short time (hh:mm)...
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.