Join 307,153 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,678 people online right now. Registration is fast and FREE... Join Now!
SELECT cm.customerName, log_items_3.item AS Ordernr, order_main.customer_orderno, order_main.plannedDeliveryDate, order_main.deliveryDate, dbo.log_items.item AS Date,
FROM dbo.log_main
INNER JOIN dbo.log_items ON dbo.log_main.logId = dbo.log_items.fk_logId INNER JOIN dbo.log_items AS log_items_2 ON dbo.log_main.logId = log_items_2.fk_logId INNER JOIN dbo.log_items AS log_items_3 ON dbo.log_main.logId = log_items_3.fk_logId INNER JOIN dbo.log_items AS log_items_4 ON dbo.log_main.logId = log_items_4.fk_logId INNER JOIN dbo.description_orderstatus ON log_items_2.item = dbo.description_orderstatus.id INNER JOIN dbo.description_orderstatus AS description_orderstatus_1 ON log_items_4.item = description_orderstatus_1.id INNER JOIN dbo.order_main ON log_items_3.item = dbo.order_main.orderId INNER JOIN customers_main cm ON order_main.fk_customerId = cm.customerId WHERE (dbo.log_main.fk_logTypeId = 2) AND (dbo.log_items.fk_logItemTypeId = 1) AND (log_items_2.fk_logItemTypeId = 9) AND (log_items_3.fk_logItemTypeId = 8) AND (log_items_4.fk_logItemTypeId = 10) AND cm.customerId = 822 AND description_orderstatus_1.name ='Levererad' AND order_main.deliveryDate <> 0 ORDER BY dbo.order_main.orderId desc
Any suggestions?
Edit: If i run the SQL like 2-3 times, then it works.. this seems to happen every time over 65 lines using TOP 65, but sometimes even TOP 200 works...
This post has been edited by micke: 22 Oct, 2009 - 03:29 AM
What database app are you using? I tried CDate function on both of your date examples and they both converted to the form: 10/22/2009 10:00:00 AM. Although should not be needed on the first as it is a date type and my test without CDate still processed variable set to that value as if it were in the above form.
This post has been edited by June7: 22 Oct, 2009 - 11:52 AM
When I set variables: x = "oct 12 2009 10:00:00AM" y = #10/22/2009 10:00:00# This: DateDiff("d", x, y) Also results in: 12
Try: DateDiff("d", ... As varchar, order_main.deliveryDate)
Also, looks you want to concatenate values in the SELECT statement for the date info? For concatenation should use the ampersand (&) instead of plus (+). Need date type delimiter. Try (I might not have the parenthesis right, is this part of an even more complex query?): "(SELECT DATEDIFF('d', (SELECT #" & SUBSTRING(dbo.log_items.item, 8, 4) & "-" & SUBSTRING(dbo.log_items.item, 1, 3) & "-" & SUBSTRING(dbo.log_items.item, 5, 2) & "# AS varchar)" & order_main.deliveryDate & ")) AS Diff"
This post has been edited by June7: 23 Oct, 2009 - 03:03 PM
the first date is 2009-10-22 10:00:00 (order_main.deliveryDate - datetime) and the second date is oct 22 2009 10:00AM (log_items.item - varchar)
??? Your dates are identical. The result would be 0 no matter what you do, right?
You, um, can't do a datediff on something that isn't a datetime. You seem to want to compare varchar. Compare things that are datetimes.
I'm not even sure if you're looking to use datediff so much as to determine equality.
Play arround with actual dates to see how the function works
e.g.
sql
select cast('2009-10-22 10:00:00' as datetime), cast('Sep 18 2009 2:41PM' as datetime), GetDate(), DateDiff(day, cast('2009-10-22 10:00:00' as datetime), GetDate())
Then, try it in a statement.
sql
SELECT cm.customerName, log_items_3.item AS Ordernr, order_main.customer_orderno, order_main.plannedDeliveryDate, order_main.deliveryDate, dbo.log_items.item AS [Date] FROM dbo.log_main INNER JOIN dbo.log_items ON dbo.log_main.logId = dbo.log_items.fk_logId AND dbo.log_items.fk_logItemTypeId = 1 INNER JOIN dbo.log_items AS log_items_2 ON dbo.log_main.logId = log_items_2.fk_logId AND log_items_2.fk_logItemTypeId = 9 INNER JOIN dbo.log_items AS log_items_3 ON dbo.log_main.logId = log_items_3.fk_logId AND log_items_3.fk_logItemTypeId = 8 INNER JOIN dbo.log_items AS log_items_4 ON dbo.log_main.logId = log_items_4.fk_logId AND log_items_4.fk_logItemTypeId = 10 INNER JOIN dbo.description_orderstatus ON log_items_2.item = dbo.description_orderstatus.id INNER JOIN dbo.description_orderstatus AS description_orderstatus_1 ON log_items_4.item = description_orderstatus_1.id AND description_orderstatus_1.name ='Levererad' INNER JOIN dbo.order_main ON log_items_3.item = dbo.order_main.orderId AND order_main.deliveryDate <> 0 INNER JOIN customers_main cm ON order_main.fk_customerId = cm.customerId AND cm.customerId = 822 WHERE dbo.log_main.fk_logTypeId = 2 AND cast(dbo.log_items.item as datetime) > cast('Sep 18 2009 2:41PM' as datetime) ORDER BY dbo.order_main.orderId desc
the first date is 2009-10-22 10:00:00 (order_main.deliveryDate - datetime) and the second date is oct 22 2009 10:00AM (log_items.item - varchar)
??? Your dates are identical. The result would be 0 no matter what you do, right?
You, um, can't do a datediff on something that isn't a datetime. You seem to want to compare varchar. Compare things that are datetimes.
I'm not even sure if you're looking to use datediff so much as to determine equality.
Play arround with actual dates to see how the function works
e.g.
sql
select cast('2009-10-22 10:00:00' as datetime), cast('Sep 18 2009 2:41PM' as datetime), GetDate(), DateDiff(day, cast('2009-10-22 10:00:00' as datetime), GetDate())
Then, try it in a statement.
sql
SELECT cm.customerName, log_items_3.item AS Ordernr, order_main.customer_orderno, order_main.plannedDeliveryDate, order_main.deliveryDate, dbo.log_items.item AS [Date] FROM dbo.log_main INNER JOIN dbo.log_items ON dbo.log_main.logId = dbo.log_items.fk_logId AND dbo.log_items.fk_logItemTypeId = 1 INNER JOIN dbo.log_items AS log_items_2 ON dbo.log_main.logId = log_items_2.fk_logId AND log_items_2.fk_logItemTypeId = 9 INNER JOIN dbo.log_items AS log_items_3 ON dbo.log_main.logId = log_items_3.fk_logId AND log_items_3.fk_logItemTypeId = 8 INNER JOIN dbo.log_items AS log_items_4 ON dbo.log_main.logId = log_items_4.fk_logId AND log_items_4.fk_logItemTypeId = 10 INNER JOIN dbo.description_orderstatus ON log_items_2.item = dbo.description_orderstatus.id INNER JOIN dbo.description_orderstatus AS description_orderstatus_1 ON log_items_4.item = description_orderstatus_1.id AND description_orderstatus_1.name ='Levererad' INNER JOIN dbo.order_main ON log_items_3.item = dbo.order_main.orderId AND order_main.deliveryDate <> 0 INNER JOIN customers_main cm ON order_main.fk_customerId = cm.customerId AND cm.customerId = 822 WHERE dbo.log_main.fk_logTypeId = 2 AND cast(dbo.log_items.item as datetime) > cast('Sep 18 2009 2:41PM' as datetime) ORDER BY dbo.order_main.orderId desc
Hope this helps.
Still got the error : Conversion failed when converting date and/or time from character string. when i try your suggestions. Problem is that log_items.item is an varchar and not and datetime... This is an varchar becauce it contains more info then the date, but the sql above only takes out the date....
but i would like to return it as an datetime, since then the report server generats an excel fil, and you can't do nothing with this cell in excel since it is an text...
If you could show some examples of the text you're trying to cast as datetime, that might help.
QUOTE(June7 @ 27 Oct, 2009 - 12:07 PM)
AND #cast(dbo.log_items.item as datetime)# > #cast('Sep 18 2009 2:41PM' as datetime)#
The wonky pound syntax only works in Access. I don't know for sure that cast does. It think it's something like "CDate" or "#12/21/2012#", both of which also only work in Access.