Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 MAX()

Author  Topic 

Braddy
Starting Member

3 Posts

Posted - 2010-06-12 : 03:45:07
Hi

I'm doing an winform application using sql server 2008 and my sql statement doesn't seems to work for me.
I have information from two related tables and my sql statement looks like this:
"SELECT * FROM Car LEFT JOIN Invoice ON Car.carId=Invoice.carId WHERE Car.arrival <= '" & date & "' AND Invoice.invoiceDay IS NULL OR (SELECT MAX(Invoice.invoiceDay) FROM Invoice WHERE Invoice.carId=Car.carId) <= '" & date & "' "

First I need to check if there is an invoice date, if not then get arrival date instead (in case this car was never invoiced before).
I need to check if the date I have set my datetimepicker on is not smaller than arrival date, if true in both cases get arrival date. If not true, then get MAX() invoice date instead. I would assumed that it gets the in this case highest date = one record/car only. But it actually gives me all records of each car. Why is that? Please advise.

Kristen
Test

22859 Posts

Posted - 2010-06-12 : 05:04:07
What format is "date"?

It needs to be "yyyymmdd" - no hyphens.

Any other format and you will have to use SQL CONVERT function to correctly parse the date format you are providing.

Does the SELECT statement do what you want if you run it manually (e.g. in SSMS) with a suitable date, formatted in yyyymmdd!!, in place?
Go to Top of Page

Braddy
Starting Member

3 Posts

Posted - 2010-06-12 : 14:43:00
Ok, thank you very much for your reply. I will try that, cause now it's stated like this:yyyy-mm-dd
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-06-14 : 03:46:16
quote:
Originally posted by Braddy

Ok, thank you very much for your reply. I will try that, cause now it's stated like this:yyyy-mm-dd


That format is ambigous
Make sure to read this post where it is explained why only two formats are really unambigous
http://beyondrelational.com/blogs/madhivanan/archive/2010/06/03/understanding-datetime-column-part-ii.aspx


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -