1

I want to get the values between particular date and time. But each values - time, hour and minutes are there in separate fields.

Following is the table format i have,

Date        Hour    Min     values

1/1/2011    10      30      Test1
2/1/2011    8       10      Test2
3/1/2011    15      40      Test3
5/1/2011    11      10      Test5
10/1/2011   3       04      Test6
12/1/2011   5       00      Test6

Now i want to get the "values" between '3/1/2011 12.00' and '10/1/2011 11.10' using MYSQL SELECT query. If anyone knows pls help me

3
  • show us your try query.. Commented Jun 30, 2017 at 8:51
  • You shouldn't store your datetime in such format Commented Jun 30, 2017 at 8:58
  • Store dates and times as a single entity. Commented Jun 30, 2017 at 10:49

3 Answers 3

2

Updating @Suresh answer, as the default date format is 'Y-m-d'.

SELECT values
FROM table_name
WHERE `Date` between '2011-01-03' and '2011-10-01' 
Sign up to request clarification or add additional context in comments.

Comments

2

You can do like this:

SELECT id, CONCAT(dateval,' ',hourval,':',minval,':00') as t 
 FROM ForgeRock 
 WHERE CAST(CONCAT(dateval,' ',hourval,':',minval,':00') as DATETIME) 
  between CAST('2006-01-21 17:10:00' as DATETIME) AND 
   CAST('2006-01-25 22:10:00' as DATETIME) 

Because of concate() we need to use CAST to convert them as datetime. SQL fiddle for it: http://sqlfiddle.com/#!9/4a072/12

8 Comments

I need to include time also in where condition, then only i will get correct output.
Thanks for your response. I will check @Suresh Kamrushi
Suppose my query is, SELECT values FROM table_name WHERE CONCAT(date,' ',Hour,':',Min,':00') between '3/1/2011 12.00' and '10/1/2011 11.10' then it is returning 3/1/2011 11.00 value too.. @Suresh Kamrushi
share your query
SELECT values FROM table_name WHERE CONCAT(date,' ',Hour,':',Min,':00') between '2011/1/03 16.00' and '2011/1/10 11.10'
|
0

We can also use alternate of Between..

SELECT *
FROM table
WHERE Date >= '2011-01-03' and Date < '2011-10-01'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.