?
Solved

Query MYSQL Date and Time

Posted on 2014-03-14
8
Medium Priority
?
445 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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 2000 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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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
Suggested Courses

741 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