Avatar of imranasif17
imranasif17Flag for United States of America

asked on 

SQL to get data between 2 date fields

Hi Experts:

I need help with an SQL query.  I have 2 tables:

Table A, Table B

Table A columns are - order #, Item
Table B columns are - order #, order_date, delivery_date

I want to get all orders/items that are delivered within 1 day of the order date.

Eg: Only return values, If the order date is December 1st, 2014, the delivery date is December 2nd, 2014

Thanks in advance...
MySQL ServerSQL

Avatar of undefined
Last Comment
mankowitz
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

For starters, use DateDiff and something like the below code...
SELECT b.OrderNumber, a.Item, b.OrderDate, b.DeliveryDate
FROM TableB b
  JOIN TableA a ON b.OrderNumber = a.OrderNumber
WHERE DATEDIFF(hour, b.OrderDate, b.DeliveryDate) BETWEEN 0 AND 24

Open in new window

Avatar of mankowitz
mankowitz
Flag of United States of America image

select * from A join B on (A.order_num=B.order_num)
where DATEDIFF ( day, b.order_date, b.delivery_date) <= 1
Avatar of imranasif17
imranasif17
Flag of United States of America image

ASKER

I am getting error:

Error Code: 1582. Incorrect parameter count in the call to native function 'DATEDIFF'
Avatar of imranasif17
imranasif17
Flag of United States of America image

ASKER

Here is my query:

select * from tableA a
inner join tableB b on a.ORD_NBR=b.ORD_NBR
where b.HDR_STAT_CD in ('F') and b.PMT_CD<>'B'
and b.ord_dt between '2013-07-01 00:00:00' and '2014-06-30 00:00:00'
and b.dlv_dt = DATEDIFF(day, b.ord_dt, b.dlv_dt) <= 1;
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

>and b.dlv_dt = DATEDIFF(day, b.ord_dt, b.dlv_dt) <= 1;

And something equals some number less than or equal to 1
Suggest you rewrite the above line.

Perhaps you mean...
AND DATEDIFF(day, b.ord_dt, b.dlv_dt) <= 1
Avatar of imranasif17
imranasif17
Flag of United States of America image

ASKER

Thanks, Jim.  I am getting the same error.  I am using MS-SQL.
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Which version?  The below mockup code works on my 2012 box:
 Declare @dt_start datetime = '2014-12-01 08:30:00', @dt_end datetime = getdate()
 
SELECT
	@dt_start, 
	@dt_end, 
	DATEDIFF (hour, @dt_start, @dt_end) as hours_diff

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of mankowitz
mankowitz
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Simon
Simon
Flag of United Kingdom of Great Britain and Northern Ireland image

It looks like you're using MySQL, rather than SQL Server.

The datediff function is different in MySQL. It simply counts number of days between two dates and you need to have the later date as the first parameter...
where datediff(delivery_date,order_date)<=1;
Avatar of imranasif17
imranasif17
Flag of United States of America image

ASKER

Thanks everyone for the help.  The solution works.
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

So out of curiosity, were you looking for a MySQL solution and not a SQL Server solution?
Avatar of imranasif17
imranasif17
Flag of United States of America image

ASKER

Yes, sorry my bad. Thanks, Jim
Avatar of mankowitz
mankowitz
Flag of United States of America image

@simon: caution with datediff - it only looks at the date portion of the datetime, not the whole thing, so if you have two timestamp separated by midnight, it will always report an extra day.
SQL
SQL

SQL (Structured Query Language) is designed to be used in conjunction with relational database products as of a means of working with sets of data. SQL consists of data definition, data manipulation, and procedural elements. Its scope includes data insert, query, update and delete, schema creation and modification, and data access control.

61K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo