4

I am facing problem when i'm trying to select records from a table between two dates.

m using the following query

select * from xxx where dates between '10/10/2012' and '10/12/2012'

this query works for me but when the dates are in format like 1/1/2013.. it doesn't work..

plz solve my problem ASAP.

Andriy M
  • 76,112
  • 17
  • 94
  • 154
Rahul Gautam
  • 1,121
  • 3
  • 11
  • 18

10 Answers10

7

This solution provides CONVERT_IMPLICIT operation for your condition in predicate

SELECT * 
FROM xxx 
WHERE CAST(dates AS date) BETWEEN '1/1/2013' and '1/2/2013'

enter image description here

OR

SELECT * 
FROM xxx 
WHERE CONVERT(date, dates, 101) BETWEEN '1/1/2013' and '1/2/2013'

enter image description here

Demo on SQLFiddle

Aleksandr Fedorenko
  • 16,594
  • 6
  • 37
  • 44
  • What if it is legacy data and the current default date format is different from `m/d/yyyy`? `CONVERT` with an explicit format specifier (probably 101 in this case) would be safer. – Andriy M Feb 04 '13 at 08:17
  • tnx @Andriy M it will be safer – Aleksandr Fedorenko Feb 04 '13 at 08:27
  • hey.. your answer solved my problem.. but when i'm using the same query for another table, it shwoing an error.. "Conversion failed when converting date and/or time from character string." the query is "SELECT * FROM transaction_tbl WHERE CAST(transaction_tbl.Date AS date) BETWEEN '1/1/2013' and '1/4/2013'" pls solve this problem also... – Rahul Gautam Feb 05 '13 at 05:35
  • Try second query in my answer. – Aleksandr Fedorenko Feb 05 '13 at 05:41
  • This syntax will prevent using vailable indexes. I would not use it, specially for large datasets. – iDevlop Oct 14 '16 at 12:12
  • thx @iDevlop, but did you try it before? SQL Server allows you to use indexes even if your column was converted to DATE. – Aleksandr Fedorenko Oct 14 '16 at 12:44
  • @AleksandrFedorenko I did not try, and did not know this. Do you have any reference about it? I always avoid expressions on the "field" side of a criteria, and I know by experience it's a valuable habit. – iDevlop Oct 15 '16 at 12:32
  • Sure @iDevlop, take a look at this topic http://stackoverflow.com/questions/15344661/what-is-the-optimal-way-to-compare-dates-in-microsoft-sql-server/15350079#15350079 – Aleksandr Fedorenko Oct 17 '16 at 08:31
2

Try this

SELECT * 
FROM xxx 
WHERE dates BETWEEN STR_TO_DATE('10/10/2012', '%m/%d/%Y') 
  AND STR_TO_DATE('10/12/2012', '%m/%d/%Y')  ;

or

SELECT * 
FROM xxx 
WHERE STR_TO_DATE(dates , '%m/%d/%Y') BETWEEN STR_TO_DATE('10/10/2012', '%m/%d/%Y') 
  AND STR_TO_DATE('10/12/2012', '%m/%d/%Y')  ;
Aleksandr Fedorenko
  • 16,594
  • 6
  • 37
  • 44
user2001117
  • 3,727
  • 1
  • 18
  • 18
1
$Date3 = date('y-m-d');
$Date2 = date('y-m-d', strtotime("-7 days"));
SELECT * FROM disaster WHERE date BETWEEN '".$Date2."' AND  '".$Date3."'
ByteHamster
  • 4,884
  • 9
  • 38
  • 53
ShadowUC
  • 724
  • 6
  • 19
0

By default Mysql store and return ‘date’ data type values in “YYYY/MM/DD” format. So if we want to display date in different format then we have to format date values as per our requirement in scripting language

And by the way what is the column data type and in which format you are storing the value.

user2001117
  • 3,727
  • 1
  • 18
  • 18
0
select * from xxx where dates between '2012-10-10' and '2012-10-12'

I always use YYYY-MM-DD in my views and never had any issue. Plus, it is readable and non equivocal.
You should be aware that using BETWEEN might not return what you expect with a DATETIME field, since it would eliminate records dated '2012-10-12 08:00' for example.
I would rather use where dates >= '2012-10-10' and dates < '2012-10-13' (lower than next day)

iDevlop
  • 24,841
  • 11
  • 90
  • 149
0
SELECT * FROM tablename WHERE STR_TO_DATE(columnname, '%d/%m/%Y')
  BETWEEN STR_TO_DATE('29/05/2017', '%d/%m/%Y')
    AND STR_TO_DATE('30/05/2017', '%d/%m/%Y')

It works perfectly :)

S M Jobayer Alam
  • 241
  • 1
  • 10
0

you have to split the datetime and then store it with your desired format like dd/MM/yyyy. then you can use this query with between but i have objection using this becasue it will search every single data on your database,so i suggest you can use datediff.

        Dim start = txtstartdate.Text.Trim()
        Dim endday = txtenddate.Text.Trim()
        Dim arr()
        arr = Split(start, "/")
        Dim dt As New DateTime
        dt = New Date(Val(arr(2).ToString), Val(arr(1).ToString), Val(arr(0).ToString))
        Dim arry()
        arry = Split(endday, "/")
        Dim dt2 As New DateTime
        dt2 = New Date(Val(arry(2).ToString), Val(arry(1).ToString), Val(arry(0).ToString))

        qry = "SELECT * FROM [calender] WHERE datediff(day,'" & dt & "',[date])>=0 and datediff(day,'" & dt2 & "',[date])<=0 "

here i have used dd/MM/yyyy format.

Bipul Roy
  • 163
  • 1
  • 14
  • Why answer an already answered question with an unsuitable solution almost 12 months after the answer was accepted? – Nick.Mc Dec 04 '17 at 06:21
  • There are a lot of option of a specific problem, i have answered becasue i thought i should share my way how i format dates and search between two dates – Bipul Roy Dec 04 '17 at 06:29
-2

use this

select * from xxx where dates between '10/oct/2012' and '10/dec/2012'

you are entering string, So give the name of month as according to format...

  • No, this **won't** work. The OP's problem is that the type of `dates` is a _string_, and is in a non-searchable format (for the range he's interested in). He's not having problems getting the starting values, it's the comparison with the stored data. – Clockwork-Muse May 03 '14 at 23:39
-2

Try this:

select * from xxx where dates between convert(datetime,'10/10/2012',103) and convert(dattime,'10/12/2012',103)
jww
  • 97,681
  • 90
  • 411
  • 885
Ankyy
  • 1
  • No, this **won't** work. The OP's problem is that the type of `dates` is a _string_, and is in a non-searchable format (for the range he's interested in). He's not having problems getting the starting values, it's the comparison with the stored data. – Clockwork-Muse May 03 '14 at 23:38
-2
DateTime dt1 = this.dateTimePicker1.Value.Date;
DateTime dt2 = this.dateTimePicker2.Value.Date.AddMinutes(1440);
String query = "SELECT * FROM student WHERE sdate BETWEEN '" + dt1 + "' AND '" + dt2 + "'";
Honza Zidek
  • 9,204
  • 4
  • 72
  • 118
  • You may try to explain why your solution works. It would be better than just put there the code. – Honza Zidek May 03 '14 at 22:07
  • No, this **won't** work. The OP's problem is that the type of `dates` is a _string_, and is in a non-searchable format (for the range he's interested in). He's not having problems getting the starting values, it's the comparison with the stored data. Also, there's no reason to **not** use a parameterized query here - regardless of the fact that we don't even know what his actual application-layer-language is. – Clockwork-Muse May 03 '14 at 23:36