Sql date variable delphi - sql

Im having trouble with my sql :
sSQL:= 'select * from tbldebit where transaction date like " '+ tdate+ ' " ' ;
Delphi 2010 keeps giving me missing operator error.
(TDate captures value from date time picker)
Look foward to your response
Regards

I have always used:
function DateTimeToSQLDate(const ADate: TDateTime): string;
var
MyYear, MyMonth, MyDay, MyHour, MyMinute, MySecond, MyMSecond: Word;
begin
DecodeDateTime(ADate, MyYear, MyMonth, MyDay, MyHour, MyMinute, MySecond, MyMSecond);
Result := Format('#%d %s %d#', [MyDay, FormatSettings.LongMonthNames[MyMonth], MyYear]);
end;
You would use this...
sSQL:= 'select * from tbldebit where [transaction date] = ' + DateTimeToSQLDate(TDate);

Correct code =
sSQL: ='Select * from tbldebit where [Transaction date] like " '+ datepicked+ ' " ' ;
The field name had to be in square brackets since their is a space in the name. I tested it and 'like' works.
Regards

Use wildcards like below:
query = "SELECT * FROM tbldebit WHERE date LIKE '%" + tdate+ "%'";

Related

SQL Date Query using parameters - Delphi

I have an SQL Query (MS Access), and I want to add the Date() function into a parameter, however I get the error: [ODBC Microsoft Access Driver]Data type mismatch in criteria expression.
Here is the code:
Qry.SQL.Text := 'SELECT Bookings.Date, Bookings.WeekDay, Bookings.Shift, Bookings.Start, Bookings.Finish,'
+ ' Bookings.DateFinish, Wards.WardName'
+ ' FROM Bookings'
+ ' INNER JOIN Wards ON Bookings.WardNo = Wards.WardNo'
+ ' WHERE (Bookings.NurseNo=:nurseID) AND (Bookings.Date BETWEEN :dateA AND :dateB)'
+ ' ORDER BY Bookings.Date ASC;';
Qry.Params.ParamByName('dateA').Value := 'Date()';
Qry.Params.ParamByName('dateB').Value := 'Date()+6';
I've also tried Qry.Params.ParamByName('dateA').AsString := 'Date()'; but no luck with that, is there a correct way to do this, or would it actually have to be in the query and not parameterised? The reason I want to do it like this, is I will have multiple different queries based on which button is pressed, but the only thing changing is those parameterised dates.
A parameter can't be a function, it has to be a value. You are assigning strings as those values, and those strings do not represent valid dates. That is why you are getting a mismatch error.
You can use Delphi Date() function, and pass the returned TDate as a parameter value:
Qry.Params.ParamByName('dateA').Value := Date();
Qry.Params.ParamByName('dateB').Value := Date()+6;
Or, you can use Access's Date() function in the SQL itself:
Qry.SQL.Text := 'SELECT Bookings.Date, Bookings.WeekDay, Bookings.Shift, Bookings.Start, Bookings.Finish,'
+ ' Bookings.DateFinish, Wards.WardName'
+ ' FROM Bookings'
+ ' INNER JOIN Wards ON Bookings.WardNo = Wards.WardNo'
+ ' WHERE (Bookings.NurseNo=:nurseID) AND (Bookings.Date BETWEEN Date() AND Date() + 6)'
+ ' ORDER BY Bookings.Date ASC;';

Combining current date code with stored procedure ti use in PowerBi

To get the required table I have to input the value "201801" into the stored procedure query. I want to place the following code:
SELECT CONVERT(nvarchar(6), GETDATE(), 112)
In the following Sp:
USE [MDAmanager]
GO
DECLARE #return_value int
EXEC #return_value = [dbo].[sqSupplierBalances]
#Period = 201801,
#SupplierString = 'select SupplierID from Suppliers ',
#SelectionString = 'select * from vSupplierBalances order by
ControlOfficeName, PortfolioName, OwnerEntityName, RegionName,
PropertyName, PropertyManagerName, Custom1Name, Custom2Name,
ServiceTypeDescription, AnalysisCode, SupplierName',
#WithCommitments = NULL,
#UserID = NULL,
#ExcludeInDispute = NULL,
#IncludeSupplierPropertyReference = NULL
SELECT 'Return Value' = #return_value
GO
Is it possible to assign a value to the first code example and replace the "201801" in the second code example with that variable. I have been trying this but not getting it right.
Update: So I realize M query functions and SQL server functions are different. I don't how to go about answering my own question but I figured I'd give the answer here anyway.
I replaced the initial date code with:
Perdiod0 = (Date.Year(DateTime.LocalNow()) * 100) +
Date.Month(DateTime.LocalNow())
And then just replaced the 201801 with:
'" & Text.From(Period0) & "'
Seems to work now

Calculate value per day

I have Oracle table where I insert data about network upload and download speed.
CREATE TABLE AGENT_HISTORY(
EVENT_DATE DATE,
NETWORK_UP NUMBER,
NETWORK_DOWN NUMBER
)
I want to generate Bar chart for last 30 days and display total upload traffic per day(24 hours).
select * from AGENT_HISTORY where EVENT_DATE >= SYSDATE - 30;
The problem which I don't know how to solve is how I can calculate the traffic for each day from the column NETWORK_UP. The result of the query should be 30 days with total upload traffic for each day. Is this possible without PL/SQL procedure?
You can do a query like this to aggregate totals data for both the network_up and network_down columns per day.
select trunc(event_day,'day') event_date
,sum(network_up) tot_network_up
,sum(network_down) tot_network_down
from agent_history
where event_day >= trunc(sysdate,'day') - interval '30' day
group by trunc(event_day,'day');
You can do it without embbeding the query in PL/SQL stored code depending on what you use for front end, in java you could you use something like this
Statement stmt = null;
String schema_name = 'abc';
String query = "select trunc(event_day,'day') event_date," +
"sum(network_up) tot_network_up," +
"sum(network_down) tot_network_down " +
"from " + schema_name +".agent_history " +
"where event_day >= trunc(sysdate,'day') " +
"- interval '30' day " +
"group by trunc(event_day,'day')"
try {
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String event_data = rs.getString("EVENT_DATE");
int tot_network_up = rs.getInt("TOT_NETWORK_UP");
int tot_network_down= rs.getInt("TOT_NETWORK_DOWN");
.....
}
catch (SQLException e) {
.......
} finally {
......
}
With something like this you just execute pure SQL.
My guess is that you just want to aggregate the data. Something like
SELECT trunc(event_date),
sum(network_up) total_up,
sum(network_down) total_down
FROM agent_history
WHERE event_date >= trunc(sysdate) - 30
GROUP BY trunc(event_date)
If that is not what you want, it would be very helpful to post some sample data, expected output, etc.

Why doesn't this GROUP BY query work?

I'm querying my Access table with this query:
SELECT (VIN&' '&Make&' '&Model&' '&CarYear&' '&ExColor&' '&InColor&' ')as CarDescript
FROM TestTable
WHERE (WorkOrderNumber='$workorder')
GROUP BY AssignedEmp;
But a similar type of query works just fine in this SQL Fiddle
Even if I replace the long (VIN&' '&....) with VIN it still doesn't work.
EDIT: Schema of the table is
WorkOrderNumber - Priority - JobStage - WorkItem - AssignedEmp - DueDate - VIN - Make - ... - InColor
In general use + instead of & for SQL. (Access will allow this however).
In a group by you need to pick which one in the group to use (if you are using mysql like your example it just picks a random one, see this fiddle) so to fix this in the general case for your example:
SELECT (max(VIN) + ' ' + max(Make) + ' ' + max(Model) + ' ' + max(CarYear) + ' ' + max(ExColor) + ' ' + max(InColor) + ' ')
as CarDescript
FROM TestTable
WHERE WorkOrderNumber='$workorder'
GROUP BY AssignedEmp;

ORA-00907: missing right parenthesis in string query

I am not familiar with Oracle syntax, but I am getting a missing right parenthesis error when I am passing this string to an .Net Oracle Command and then it tries to fill the data adapter.
string cT = "SELECT 'PRODUCT' AS ItemType, 'x' || CAST(LPROD.QUANTITY AS VARCHAR2(50)), " +
"PNAME.NAME || ' (' || CAST((PRODS.\"SIZE\" AS VARCHAR2(50))) || ' ' || PRODS.MEASURE || ')' AS Name, " +
"PRODS.PRODUCT_ID as ProductSizeID, PRODS.UPC_CODE as BarCode FROM ORDERS ORDR, LI_PROD LPROD, PRODUCT_NAME PNAME, " +
"PRODUCT PRODS WHERE ORDR.INV_NUM = :Invoice_Num AND ORDR.ORDER_NUM = LPROD.ORDER_NUM " +
"AND LPROD.PRODUCT_ID = PRODS.PRODUCT_ID AND PRODS.PRODUCT_NAME_ID = PNAME.ID";
Can anyone spot the error that may be causing this? Thanks for your help.
Try:
cast(prods."SIZE" as varchar2(50))
Instead of:
cast((prods."SIZE" as varchar2(50)))
I recently came up with the exact same issue and could not find a solution. What ended up happening was that I had the following code from SQLServer that I was attempting to convert to Oracle.
CAST(col AS NVARCHAR(250)) AS col_name,
I was able to fix it by changing it to
CAST(col AS NVARCHAR2(250)) AS col_name,