joshfly
asked on
SQL for DATE Value
Hello, im encounter a SQL problem, i want to create a report's filter that allow user to select a Start Date and End Date to display all the sales between the date.
By looking at the SQL statement below is actually nothing but when i select the SAME date for both start date and end date, the report display nothing, just blank.
Quate of my SQL Statement
-------------------------- -------
SELECT *
FROM SalesMaster
WHERE SALESDATE >= :MyStartSalesDate AND SALESDATE <= :MyEndSalesDate
how can i solve this problem...
Thanks
josh
By looking at the SQL statement below is actually nothing but when i select the SAME date for both start date and end date, the report display nothing, just blank.
Quate of my SQL Statement
--------------------------
SELECT *
FROM SalesMaster
WHERE SALESDATE >= :MyStartSalesDate AND SALESDATE <= :MyEndSalesDate
how can i solve this problem...
Thanks
josh
1. Is your date format correct for the SQL server?
This works on an Oracle database and solves any date format issue
'TO_DATE('+ Enquote( FormatDateTime('dd-mm-yyyy ', td))+',''DD-MM-YYYY'')';
2. Are you using dbExpress?
In that case you have supply your date parameter as AsSQLTimeStamp instead of AsDate
Info on http://community.borland.com/article/0,1410,30078,00.html
This works on an Oracle database and solves any date format issue
'TO_DATE('+ Enquote( FormatDateTime('dd-mm-yyyy
2. Are you using dbExpress?
In that case you have supply your date parameter as AsSQLTimeStamp instead of AsDate
Info on http://community.borland.com/article/0,1410,30078,00.html
use BETWEEN like below
SELECT *
FROM SalesMaster
WHERE SALESDATE BETWEEN :MyStartSalesDate AND :MyEndSalesDate
SELECT *
FROM SalesMaster
WHERE SALESDATE BETWEEN :MyStartSalesDate AND :MyEndSalesDate
if you supply the same value for MyStartSalesDate and MyEndSalesDate in
WHERE SALESDATE >= :MyStartSalesDate AND SALESDATE <= :MyEndSalesDate
this is the same as saying
WHERE SALESDATE = :MyStartSalesDate
which means SALESDATE must be equal to MyStartSalesDate down to milliseconds.
Its quite normal that this query returns an empty resultset.
WHERE SALESDATE >= :MyStartSalesDate AND SALESDATE <= :MyEndSalesDate
this is the same as saying
WHERE SALESDATE = :MyStartSalesDate
which means SALESDATE must be equal to MyStartSalesDate down to milliseconds.
Its quite normal that this query returns an empty resultset.
procedure TForm1.SelectSalesByPeriod (MyStartSa lesDate, MyEndSalesDate: TDateTime);
var
S: string;
begin
S := ''
+ 'SELECT * FROM SalesMaster WHERE SALESDATE >='
+ ''''
+ FormatDateTime('yyyy/mm/dd ', MyStartSalesDate)
+ ''''
+ ' AND SALESDATE <='
+ ''''
+ FormatDateTime('yyyy/mm/dd ', MyEndSalesDate)
+ ''''
+ '';
ADOQuery1.SQL.Text := S;
end;
var
S: string;
begin
S := ''
+ 'SELECT * FROM SalesMaster WHERE SALESDATE >='
+ ''''
+ FormatDateTime('yyyy/mm/dd
+ ''''
+ ' AND SALESDATE <='
+ ''''
+ FormatDateTime('yyyy/mm/dd
+ ''''
+ '';
ADOQuery1.SQL.Text := S;
end;
procedure TForm1.SelectSalesByPeriod (MyStartSa lesDate, MyEndSalesDate: TDateTime);
var
S: string;
begin
S := ''
+ 'SELECT * FROM SalesMaster WHERE SALESDATE >='
+ ''''
+ FormatDateTime('yyyy/mm/dd ', MyStartSalesDate)
+ ''''
+ ' AND SALESDATE <='
+ ''''
+ FormatDateTime('yyyy/mm/dd ', MyEndSalesDate)
+ ''''
+ '';
ADOQuery1.Active := False;
ADOQuery1.SQL.Text := S;
ADOQuery1.Active := True;
end;
var
S: string;
begin
S := ''
+ 'SELECT * FROM SalesMaster WHERE SALESDATE >='
+ ''''
+ FormatDateTime('yyyy/mm/dd
+ ''''
+ ' AND SALESDATE <='
+ ''''
+ FormatDateTime('yyyy/mm/dd
+ ''''
+ '';
ADOQuery1.Active := False;
ADOQuery1.SQL.Text := S;
ADOQuery1.Active := True;
end;
If you use Access database then you can
use the TimeSerial and DateSerial.
SELECT *
FROM MyTable
WHERE (MyDate>=DateSerial(2000,1 ,1)+TimeSe rial(0,0,0 )) and
(MyDate<=DateSerial(2001,1 2,31)+Time Serial(23, 59,59));
If you use MS SQL Server then you can
use the Convert function.
SELECT *
FROM MyTable
WHERE (MyDate>=CONVERT(datetime, '01/01/2000 00:00:00')) AND
(MyDate<=CONVERT(datetime, '31/12/2001 23:59:59'));
I preffer using this conversion function to avoid different kinds of date format.
DateSerial is always DateSerial(yyyy,mm,dd) and Convert is always Convert(datetime, 'dd/mm/yyyy hh:mm:ss').
regards,
huferry
use the TimeSerial and DateSerial.
SELECT *
FROM MyTable
WHERE (MyDate>=DateSerial(2000,1
(MyDate<=DateSerial(2001,1
If you use MS SQL Server then you can
use the Convert function.
SELECT *
FROM MyTable
WHERE (MyDate>=CONVERT(datetime,
(MyDate<=CONVERT(datetime,
I preffer using this conversion function to avoid different kinds of date format.
DateSerial is always DateSerial(yyyy,mm,dd) and Convert is always Convert(datetime, 'dd/mm/yyyy hh:mm:ss').
regards,
huferry
Don't use FormatDateTime routines. They depend on the regional settings. Instead you can convert the date as Float, because the DateTime is real type - the integer part contains the date, the fraction - time.
So put your query in some DataSet:
SELECT * FROM SalesMaster
WHERE SALESDATE >= :MyStartSalesDate
AND SALESDATE <= :MyEndSalesDate
set Params property with 2 parameters and then:
ADOQuery1.Parameters.Param ByName('DA TE_START') .Value := DateTimePicker1.DateTime;
ADOQuery1.Parameters.Param ByName('DA TE_END').V alue := DateTimePicker2.DateTime;
So put your query in some DataSet:
SELECT * FROM SalesMaster
WHERE SALESDATE >= :MyStartSalesDate
AND SALESDATE <= :MyEndSalesDate
set Params property with 2 parameters and then:
ADOQuery1.Parameters.Param
ADOQuery1.Parameters.Param
> Don't use FormatDateTime routines. They depend on the regional settings.
Ivanov_G is right:
FormatDateTime('yyyy/mm/dd ', 38204.7370250926);
returns on my computer:
2004.08.05
but I expected:
2004/08/05
So please ignore my above posts....
//........
if you use is MS SQL Server database:
uses
...., DateUtils;
procedure TForm1.SelectSalesByPeriod (MyStartSa lesDate, MyEndSalesDate: TDateTime);
var
S: string;
T: string;
R: string;
begin
T := IntToStr(YearOf(MyStartSal esDate)) + '/'
+ IntToStr(MonthOf(MyStartSa lesDate)) + '/'
+ IntToStr(DayOf(MyStartSale sDate));
R := IntToStr(YearOf(MyEndSales Date)) + '/'
+ IntToStr(MonthOf(MyEndSale sDate)) + '/'
+ IntToStr(DayOf(MyEndSalesD ate));
S := ''
+ 'SELECT * FROM SalesMaster WHERE SALESDATE >='
+ '''' + T + ''''
+ ' AND SALESDATE <='
+ '''' + R + ''''
+ '';
ADOQuery1.Active := False;
ADOQuery1.SQL.Text := S;
ADOQuery1.Active := True;
end;
Ivanov_G is right:
FormatDateTime('yyyy/mm/dd
returns on my computer:
2004.08.05
but I expected:
2004/08/05
So please ignore my above posts....
//........
if you use is MS SQL Server database:
uses
...., DateUtils;
procedure TForm1.SelectSalesByPeriod
var
S: string;
T: string;
R: string;
begin
T := IntToStr(YearOf(MyStartSal
+ IntToStr(MonthOf(MyStartSa
+ IntToStr(DayOf(MyStartSale
R := IntToStr(YearOf(MyEndSales
+ IntToStr(MonthOf(MyEndSale
+ IntToStr(DayOf(MyEndSalesD
S := ''
+ 'SELECT * FROM SalesMaster WHERE SALESDATE >='
+ '''' + T + ''''
+ ' AND SALESDATE <='
+ '''' + R + ''''
+ '';
ADOQuery1.Active := False;
ADOQuery1.SQL.Text := S;
ADOQuery1.Active := True;
end;
> Don't use FormatDateTime routines.
That is a bit harsh. Yes they do rely on Regional settings but you can make them work as you want using date format variables.
Eg:
DateSeparator:='/';
FormatDateTime('yyyy/mm/dd ', 38204.7370250926);
returns 2004/08/05
DateSeparator:='.';
FormatDateTime('yyyy/mm/dd ', 38204.7370250926);
returns 2004.08.05
That is a bit harsh. Yes they do rely on Regional settings but you can make them work as you want using date format variables.
Eg:
DateSeparator:='/';
FormatDateTime('yyyy/mm/dd
returns 2004/08/05
DateSeparator:='.';
FormatDateTime('yyyy/mm/dd
returns 2004.08.05
ASKER
im using MS SQL SERVER...
and also, if only specific a date it still can not display the record
SQL statement (Which i had tried)
-----------------
(1) SELECT *
FROM SalesMaster
WHERE DATE = GetDate() // not work
(2) SELECT *
FROM SalesMaster
WHERE DATE = CONVERT(DATETIME,'04/08/20 04)
(3) SELECT *
FROM SalesMaster
WHERE DATE = 04/08/2004
headache....T_T, why can not display the record, anyone know?
thanks for all who reply my post..
josh
and also, if only specific a date it still can not display the record
SQL statement (Which i had tried)
-----------------
(1) SELECT *
FROM SalesMaster
WHERE DATE = GetDate() // not work
(2) SELECT *
FROM SalesMaster
WHERE DATE = CONVERT(DATETIME,'04/08/20
(3) SELECT *
FROM SalesMaster
WHERE DATE = 04/08/2004
headache....T_T, why can not display the record, anyone know?
thanks for all who reply my post..
josh
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
(3) SELECT *
FROM SalesMaster
WHERE DATE = 2004/08/04
FROM SalesMaster
WHERE DATE = 2004/08/04
For more information on the CONVERT function
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ca-co_2f3o.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ca-co_2f3o.asp
ASKER
Thanks jpedef, im using your example to solve my problem, but i made some modification.
the start and end date is select by the user from a date combo box, so, the HH:NN will always be 00:00, since te user only select the date not the time.
Then i change replace the both HH:NN to this :-
StartDate := FormatDatetime('YYYYMMDD 00:00, StartOfTheDay.Date);
EndDate := FormatDatetime('YYYYMMDD 23:00, EndOfTheDay.Date);
this code giving a range of the date....thanks alot
other experts also is GREAT!! i will award points to you all, once i post anoter questions. thanks alot
josh ;)
the start and end date is select by the user from a date combo box, so, the HH:NN will always be 00:00, since te user only select the date not the time.
Then i change replace the both HH:NN to this :-
StartDate := FormatDatetime('YYYYMMDD 00:00, StartOfTheDay.Date);
EndDate := FormatDatetime('YYYYMMDD 23:00, EndOfTheDay.Date);
this code giving a range of the date....thanks alot
other experts also is GREAT!! i will award points to you all, once i post anoter questions. thanks alot
josh ;)
2.