Solved

Query MYSQL Date and Time

Posted on 2014-03-14
8
435 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 82

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 82

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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 82

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 82

Expert Comment

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now