Click to See Complete Forum and Search --> : MS SQL -- Datepart


dummyagain
March 19th, 2007, 03:06 AM
i saw a query

SELECT master_customer.cust_code, orders.ord_date, order_citem.fty_delivery_date, order_citem.cust_delivery_date, CASE { fn UCASE(order_citem.vol_unit)
} WHEN 'INCH' THEN (order_citem.width * order_citem.length * order_citem.height )
ELSE (order_citem.width * order_citem.length * order_citem.height ) END AS CFT, orders.ord_confirm_code,
ISNULL(master_unit_conversion.factor, 1) * order_citem.fty_price * order_citem.qty AS citem_sub_total, CONVERT(NVARCHAR(4), DATEPART(yyyy,
order_citem.cust_delivery_date)) + '/' + RIGHT('0' + CONVERT(NVARCHAR(2), DATEPART(mm, order_citem.cust_delivery_date)), 2)
AS cust_delivery_date_yyyy_mm
FROM orders LEFT OUTER JOIN
master_customer ON orders.cust_id = master_customer.cust_id LEFT OUTER JOIN
master_unit RIGHT OUTER JOIN
order_citem ON master_unit.unit_code = order_citem.fty_price_unit LEFT OUTER JOIN
master_unit master_unit_1 ON order_citem.qty_unit = master_unit_1.unit_code LEFT OUTER JOIN
master_supplier ON order_citem.supp_id = master_supplier.supp_id ON orders.ord_id = order_citem.ord_id LEFT OUTER JOIN
master_unit_conversion ON master_unit_conversion.converted_unit_id = master_unit.unit_id AND
master_unit_conversion.convert_unit_id = master_unit_1.unit_id
WHERE (DATEPART(yyyy, order_citem.cust_delivery_date) < @yyyy) AND (master_supplier.supp_code LIKE @supp_code) AND (master_customer.cust_code LIKE @cust_code)
ORDER BY master_customer.cust_code

I would like to know what (DATEPART(yyyy, order_citem.cust_delivery_date) < @yyyy) will return, since i don't understand why it doesnt need to compare with other field?

Thank you

Shuja Ali
March 19th, 2007, 03:28 AM
DatePart function gets the part of the date from the given date. In this case it is getting the Year from the cust_delivery_date field of order_citem table, and compares it with the @yyyy variable that is passed on to the query.

dummyagain
March 19th, 2007, 04:05 AM
Thank you.

But when i run it, it gets an exception of
"Arithmetic overflow error converting expression to data type datetime"
the variable it passed is 2007 but it still shows this error.

Any advices, please?

Thank you.

Shuja Ali
March 19th, 2007, 04:09 AM
The field that you are querying does not contain a valid date then. Check the data that you are trying to query.