Mhmt
Mhmt

Reputation: 769

How to list data between two dates in sql?

I tried display 1 monthly data but I want display e.g between 01.06.2013 and 01.07.2013 ? How to change this query for display between two date ? Thanks in advance

   select b.ID ID, bt.type BULTEN_TYPE, b.ID TOPIC, b.Bul_Effect EFFECTT, b.Bul_Comment COMMENTS, 

     concat(date_format(b.Bul_date,'%d.%m.%Y'), ' ', b.Bul_hour, ':', b.Bul_min) as BEGIN,      
     concat(date_format(b.bitdate,'%d.%m.%Y'), ' ', b.bithour
     , ':', b.bitmin) as FINISH from bulten b, bulten_type bt, statu s WHERE b.Bul_Type = bt.ID and   
      b.Status = s.ID and Bul_date >= date(now() - interval 1 month) order by ID desc;

Upvotes: 0

Views: 318

Answers (4)

user2575950
user2575950

Reputation:

I have find something for you. I think this might helps you

SQL check record in between two dates and time

Check this question

Upvotes: 1

Manish Sharma
Manish Sharma

Reputation: 2426

use this

Bul_date BETWEEN @StartDate AND @EndDate;

Upvotes: 1

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

This WHERE clause will get you exactly what you used as an example:

... and Bul_date BETWEEN '1/6/2013' AND '1/7/2013' ...

Now, a more dynamic way of getting at what I think you want would be:

... and Bul_date BETWEEN GETDATE() AND DATEADD(DAY, 1, GETDATE()) ...

that would get you everything between now, and a day from now.

Now, the problem with the last example is that GETDATE() has a time on it so if you wanted to strip that (i.e. to start from midnight) you could do this:

... and Bul_date BETWEEN DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())) AND
        DATEADD(DAY, 1, DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())))

Upvotes: 1

Amit Singh
Amit Singh

Reputation: 8109

 select b.ID ID, 
      bt.type BULTEN_TYPE,
      b.ID TOPIC,
      b.Bul_Effect EFFECTT,
      b.Bul_Comment COMMENTS, 
     concat(date_format(b.Bul_date,'%d.%m.%Y'), ' ', b.Bul_hour, ':', b.Bul_min) as BEGIN,
     concat(date_format(b.bitdate,'%d.%m.%Y'), ' ', b.bithour
     , ':', b.bitmin) as FINISH 
     from bulten b, bulten_type bt, statu s
      WHERE b.Bul_Type = bt.ID and   
      b.Status = s.ID and 
     Month(Bul_date)=Month(GetDate()) and Year(Bul_date)=Year(GetDate()) 
     order by ID desc;

Upvotes: 2

Related Questions