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
Solved

Query MYSQL Date and Time

Posted on 2014-03-14
8
440 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

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
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 SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

856 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