Solved

Query MYSQL Date and Time

Posted on 2014-03-14
8
442 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Syntax 6 42
UPDATE JOIN multiple tables 5 23
Delete old Sharepoint backups 2 24
Estimating my database size 7 21
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

730 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