Solved

Query MYSQL Date and Time

Posted on 2014-03-14
8
439 Views
Last Modified: 2014-03-14
I have a db with a field called starttime and a field call endtime.
Both fields are setup as datetime field in mysql.
I'm using vb.net. I want to be able to select all records that are between a
certain start time and end time. the variables StartDate and EndDate are datetime variables. They get created from a text box and they look like this StartDate = #3/7/2014 8:30:00 AM#  and EndDate = #3/7/2014 10:00:00 AM#
 Here is my query
daData1Adapter = New MySqlDataAdapter("Select  Event, DATE(date)as 'Date', starttime as 'Start Time', endtime as 'End Time'  From  schedule  where locationnumber = '" & ddlLocation.SelectedItem.Value & "' and starttime between DATE_FORMAT('" & StartDate & "', '%Y-%m-%d %H:%i:%s') and DATE_FORMAT('" & EndDate & "', '%Y-%m-%d %H:%i:%s') or locationnumber = '" & ddlLocation.SelectedItem.Value & "' and (endtime > DATE_FORMAT('" & StartDate & "', '%Y-%m-%d %H:%i:%s') and endtime <= DATE_FORMAT('" & EndDate & "', '%Y-%m-%d %H:%i:%s'))  ", myconnectionstring)

The query never returns anything.
0
Comment
Question by:whiwex
  • 4
  • 3
8 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39930119
DateTime fields in MySQL use a very specific format.  As you can see from the examples here http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format , MySQL is expecting the input date or datetime to be in the correct format.  DATE_FORMAT() is intended to convert from a standard format to other formats.  Not the other way around.  To do date arithmetic and comparisons, you need to use the standard format as the input to the query.  YYYY-MM-DD or YYYY-MM-DD HH:MM:SS

In your example 3/7/2014 8:30:00 should be 2014-03-07 08:30:00 .  I have always had to convert other formats to the standard format before putting it in the query.
0
 

Author Comment

by:whiwex
ID: 39930468
When I change the query to this:
daData1Adapter = New MySqlDataAdapter("Select  Event, DATE(date)as 'Date', starttime as 'Start Time', endtime as 'End Time'  From  schedule  where locationnumber = '" & ddlLocation.SelectedItem.Value & "' and starttime between '" & StartDate.ToString("yyyy-MM-dd HH:mm:ss") & "' and '" & EndDate.ToString("yyyy-MM-dd HH:mm:ss") & "' ", myconnectionstring)

It doesn't work.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39930505
Echo or print out the SQL string so I can see it filled out.   Single quotes in a MySQL query are to delimit values, not fields.  I recommend that you do not use spaces in fieldnames.  If you really want to use field names with spaces in them, you need to use back tiks like `Start Time`.
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:whiwex
ID: 39930634
Here's what is happening:
I want to query a database that has a datetime field called Starttime and a datetime field called EndTime.   The use inputs a time called MyTime and I want all the records that Mytime is between the StartTime field and EndTime filed or is equal to the StartTime field.

Thanks
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 39930663
Here is a query that works on a table I set up for this.  I am using values instead of variables for the test query.  You could substitute your variable for the date but it must be in the same format.
SELECT * FROM `startfinish` WHERE '2014-03-14 18:01:01' >= `start` AND '2014-03-14 18:01:01' < `finish`

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39930702
Topics.NET ,MS SQL Server
You may want to get this thread moved to the (you guessed it) MySQL forum:
http://www.experts-exchange.com/Database/MySQL/
0
 

Author Closing Comment

by:whiwex
ID: 39930709
This wasn't the exact query that I needed but it pointed me in the right direction. Thanks
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39930730
Thanks for the points.  What did you end up using?
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

770 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