I have the following query:
<cfset x = 0.125>
<cfquery name="MySummary" datasource="xyz">
SELECT
sum(mycount_int_int) * 2 AS MySummary
FROM
[MK].[dbo].[mytable]
WHERE
date_dt >= '#Start_dt# #Start_time#' AND date_dt < '#Stop_dt# #Stop_time#'
</cfquery>
And I am getting the following error :
The value of '' cannot be converted to a number.
The error occured in Line 241
Here is my line 241 which is written below the above cfquery in my code:
<cfset Voice1st = Numberformat(MySummary.MySummary * x, "0.00")>
Should I do something like the following?
<cfset Voice1st = IsNumeric(Numberformat(MySummary.MySummary * x, "0.00"))>
Please let me know
Change your SQL to:
SELECT
isNull(sum(mycount_int_int) * 2,0) AS MySummary
FROM
[MK].[dbo].[mytable]
WHERE
date_dt >= '#Start_dt# #Start_time#' AND
date_dt < '#Stop_dt# #Stop_time#'
Edit:
Use the isNull function depending upon your DB type. IsNull() for mssql, IfNull() for mySql (mySql has IsNUll() too, but that behaves differently), NVL for Oracle.
The error message is pretty self-explanatory.
Maybe there were 0 row? Check MySummary.recordCount
Related
I want perform below SQL query using Q models
SELECT * FROM motor_vehicle_collision.collision_data_collisiondetails
where (numberOfCyclistInjured > 0 or numberOfCyclistKilled > 0) and
(longitude != '' and latitude != '');
for that I have written below query
query = Q(numberOfCyclistInjured__gt=0)
query.add(Q(numberOfCyclistKilled__gt=0), Q.OR)
query.add(~Q(latitude=''), Q.AND)
query.add(~Q(longitude=''), Q.AND)
but still I am getting data having latitude/longitude empty, how should I rectify it?
You can work with:
query = (Q(numberOfCyclistInjured__gt=0) | Q(numberOfCyclistKilled__gt=0))
& ~Q(longitude='')
& ~Q(latitude=''))
This makes it also more explicit how you bind the conditions together.
There is a scenario where I receive a string to the bigquery function and need to use it as a column name.
here is the function
CREATE OR REPLACE FUNCTION METADATA.GET_VALUE(column STRING, row_number int64) AS (
(SELECT column from WORK.temp WHERE rownumber = row_number)
);
When I call this function as select METADATA.GET_VALUE("TXCAMP10",149); I get the value as TXCAMP10 so we can say that it is processed as SELECT "TXCAMP10" from WORK.temp WHERE rownumber = 149 but I need it as SELECT TXCAMP10 from WORK.temp WHERE rownumber = 149 which will return some value from temp table lets suppose the value as A
so ultimately I need value A instead of column name i.e. TXCAMP10.
I tried using execute immediate like execute immediate("SELECT" || column || "from WORK.temp WHERE rownumber =" ||row_number) from this stack overflow post to resolve this issue but turns out I can't use it in a function.
How do I achieve required result?
I don't think you can achieve this result with the help of UDF in standard SQL in BigQuery.
But it is possible to do this with stored procedures in BigQuery and EXECUTE IMMEDIATE statement. Consider this code, which simulates the situation you have:
create or replace table d1.temp(
c1 int64,
c2 int64
);
insert into d1.temp values (1, 1), (2, 2);
create or replace procedure d1.GET_VALUE(column STRING, row_number int64, out result int64)
BEGIN
EXECUTE IMMEDIATE 'SELECT ' || column || ' from d1.temp where c2 = ?' into result using row_number;
END;
BEGIN
DECLARE result_c1 INT64;
call d1.GET_VALUE("c1", 1, result_c1);
select result_c1;
END;
After some research and trial-error methods, I used this workaround to solve this issue. It may not be the best solution when you have too many columns but it surely works.
CREATE OR REPLACE FUNCTION METADATA.GET_VALUE(column STRING, row_number int64) AS (
(SELECT case
when column_name = 'a' then a
when column_name = 'b' then b
when column_name = 'c' then c
when column_name = 'd' then d
when column_name = 'e' then e
end from WORK.temp WHERE rownumber = row_number)
);
And this gives the required results.
Point to note: the number of columns you use in the case statement should be of the same datatype else it won't work
I don't understand whats wrong with the code, I have read a lot of times but I can't find the error
pstmt = con->prepareStatement("SELECT (?) FROM votos WHERE id = (?)");
pstmt->setString(1, eleccion);
pstmt->setInt(2, p->getId());
res = pstmt->executeQuery();
while(res->next())
{
p->setVoto(res->getInt(1));
}
When the eleccion and id variables are Provincial and 1 respectively the getInt(1) function should return 1, but it returns 0.
The command (in the mysql command line):
SELECT Provincial from Votos WHERE id=1
Returns a table with one row and one column with the value 1
Side notes:
Spelling was checked
The getId() function works correctly
The compiler doesn't give any error
You can't use a placeholder in a prepared query for a column name. It's returning the value of the string eleccion, not using it as the name of a column in the table. You need to do string concatenation to substitute the column name.
std::string sql = std::string("SELECT `") + eleccion + "` FROM votos WHERE id = ?";
pstmt = con->prepareStatement(sql.c_str());
pstmt->setInt(1, p->getId());
res = pstmt->executeQuery();
while(res->next())
{
p->setVoto(res->getInt(1));
}
If the value of eleccion is coming from the user or some other untrusted source, make sure you validate it before concatenating, to prevent SQL injection.
I'm trying to execute a raw sql string using my custom Connection class..
(The Execute method simply executes the given sql statement..)
MyConnection.Execute(
"DECLARE #i int "
"SET #i = 0 "
"WHILE (#i < 10000) BEGIN "
"INSERT INTO my_table VALUES (1, 2, 3) "
"SET #i = #i + 1");
Running this statement gives me a syntax error:
Code = 80040e14
Code meaning = I
Source = ASEOLEDB
Description = [42000]
[ASEOLEDB] Incorrect syntax near '1'.
As you can see, I'm connecting to Sybase Adaptive Server Enterprise, which accepts T-SQL..
I'm assuming there is something wrong with my sql string,, or with how it's formatted?
Thanks
PS I'm sure there's nothing wrong with my "Execute" method, as a single line delete statement works fine..
i found this example in the web
there is no END at your code and no ; but the ';', i think, you don't need.
DECLARE #MyCounter int;
SET #MyCounter = 0;
WHILE (#MyCounter < 26)
BEGIN;
INSERT INTO TestTable VALUES (#MyCounter, CHAR( ( #MyCounter + ASCII('a') ) ) );
SET #MyCounter = #MyCounter + 1;
END;
BR
Alex
I'm still new to ColdFusion. Basically I am dynamically creating a query for Oracle. I have used cfquery/cfparam in the past but I would really rather use cfscript to accomplish as that is more readable. It is intended to be a large 'INSERT ALL ... INTO.'
Here's a basic example of what I have so far:
<cfscript>
clinicNIL = structNew();
clinicNIL.ADDRESS1 = 'line 1';
clinicNIL.ADDRESS2 = 'line 2';
myFields = [
'ADDRESS1'
,'ADDRESS2'
];
query = queryNew("");
sql = "INSERT ALL";
for (i=1; i LTE ArrayLen(myFields); i=i+1) {
sql = sql & "INTO NOTINLIST (SOURCETABLE, SOURCECOLUMN, SOURCEPK, ENTEREDVALUE, INSERTDATE, UPDATEDDATE, INSERTEDBY, UPDATEDBY) VALUES(";
// [..]
// How to dynamically escape the value below?
sql = sql & EscapeTheParameterHere( clinicNIL[ myFields[i] ]);
// [..]
sql = sql & ")
";
}
WriteOutput( query );
</cfscript>
Where I have 'EscapeTheParameterHere' I want to be able to have that value escaped somehow. how can I escape the value?
while I'm here, is there any good resources or references for CF?
You can bind parameters using the addParam function of a cfscript query object just like cfqueryparam works. Had to convert your example a bit to work on my MSSQL box and a smaller version of your table but it should give you the general idea.
<cfscript>
clinicNIL = structNew();
clinicNIL.ADDRESS1 = 'line 1';
clinicNIL.ADDRESS2 = 'line 2';
myFields = [
'ADDRESS1'
,'ADDRESS2'
];
query = new query();
//you may need to use the query methods setDatasource, setUsername and setPassword to configure the query
//sql = "INSERT ALL" & chr(13) & chr(10);
sql = "";
for (i=1; i LTE ArrayLen(myFields); i=i+1) {
query.addParam(name="address"&i,value=clinicNIL[ myFields[i] ],cfsqltype="VARCHAR");
sql = sql & "INSERT INTO NOTINLIST (ADDRESS) VALUES(";
sql = sql & ":address" & i;
sql = sql & ")" & chr(13) & chr(10);
}
queryResult = query.execute(sql=sql);
</cfscript>
The magic is the :paramName in the sql string will have it's associated parameter replaced during the execute call with a properly escaped parameter.
here is the solution I came up with using cfquery/cfqueryparam. I didn't realize you could do a cfloop inside of a cfquery. By the way, I did find something called 'CF.Query' but apparently it only satisfies a subset of cfquery.
<cfscript>
clinicNIL = structNew();
clinicNIL.ADDRESS1 = 'line 1';
clinicNIL.ADDRESS2 = 'line 2';
myFields = [
'ADDRESS1'
,'ADDRESS2'
];
totalFields = ArrayLen(myFields);
</cfscript>
<cfquery name="insert" datasource="somedatasource">
INSERT ALL
<cfloop from="1" to="#totalFields#" index="i">
INTO NOTINLIST
(SOURCETABLE, SOURCEPK, SOURCECOLUMN, ENTEREDVALUE, INSERTDATE, UPDATEDATE, INSERTEDBY, UPDATEDBY)
VALUES(
'FACULTYADDRESSES'
, 123
, <cfqueryparam value = "#myFields[i]#" cfsqltype='CF_SQL_VARCHAR'>
, <cfqueryparam value = "#clinicNIL[ myFields[i] ]#" cfsqltype='CF_SQL_VARCHAR'>
, SYSDATE
, SYSDATE
, '123'
, '123'
)
</cfloop>
SELECT * FROM DUAL
</cfquery>