JPA Named query gives Error as The right expression is not a valid expression and The query contains a malformed ending - jpa

I have the Named query as below and passing dynamic parameter
select count(table1) from Table1 table1 where table1.nameId in
(select table2.nameId from Table2 table2 where table2.hicNumber =:value0)
and table1.indicator = :value1
and table1.startDate = :value2
and NVL(table1.endDate,'31-DEC-2999')=NVL(:value3,'31-DEC-2999')
It gives Error as
[235, 255] The right expression is not a valid expression.
[256, 297] The query contains a malformed ending.
Can you please help me for what i am doing wrong?

Related

Redshift - ERROR: Invalid input syntax for type numeric

I use the aws - redshift now,but i got a Error like below when i run this query
<Query>
select
row_number() over(partition by nb.roadnumbercode
order by sqrt(power((1.0*cast(ra.latitude as decimal(38,10)))-coalesce(nb.buildingcenterlatitude,0),2)
+power((1.0*cast(ra.longitude as decimal(38,10)))-coalesce(nb.buildingcenterlongitude,0),2)
)) rnum
from dmart.addresses ra join dmart.buildings nb on nb.roadnumbercode = ra.roadnamecode and nb.buildingcenterlatitude is not null and nb.buildingcenterlongitude is not null
limit 30;
Why this error cause ? How can I fix it ?
It looks like you are casting the empty string (“”) to a numeric which isn’t valid. You need to handle the case where the string is empty, nut just NULL.

T-SQL Error converting data type on join, on non-joining field

A query is written to join data from two tables and group it. If the two lines defining the join are commented out, the query returns correctly. Here is the query (with the join commented out):
SELECT tblTurbineLocations.TurbineLayoutProjectID as ProjectID
,TurbineLayoutNumber
,Count(HubHeight) as NumTurbines
,tblTurbineLocations.WTCode
FROM [TurbineLayout].[dbo].[tblTurbineLocations]
--LEFT OUTER JOIN [TurbineModel].[dbo].[tblTurbineModels]
--ON str(tblTurbineLocations.WTCode) = str(tblTurbineModels.WTCode) --Need to force string conversion to avoid data type conflict.
WHERE tblTurbineLocations.TurbineLayoutProjectID = 2255
AND tblTurbineLocations.TurbineLayoutNumber IN (406, 407)
GROUP BY tblTurbineLocations.TurbineLayoutProjectID ,tblTurbineLocations.TurbineLayoutNumber ,tblTurbineLocations.WTCode
Removing the two commented join lines then attempts a join on the WTCode field. This returns the following error:
Msg 8114, Level 16, State 5, Line 2
Error converting data type nvarchar to float.
The error points to line 2, rather than the line containing the join. The error raises that nvarchar cannot be converted to float. However the column in line 2, tblTurbineLocations.TurbineLayoutProjectID, is not an nvarchar; it is an int:
Reviewing the other columns in the query, none are of type nvarchar save for the joining column, WTCode (nvarchar(11) in one table, nvarchar(5) in the other). Both are cast as strings to avoid a different error (that is resolved by casting as str):
Cannot resolve the collation conflict between "Latin1_General_CI_AS" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation.
WTCode is not being cast as a float in the query.
What is the error in my code or my approach?
As indicated by Larnu in the comments:
The issue is that the "str" function is expecting a float, thus is returning the error when it is fed an nvarchar. To solve the collation conflict, COLLATE can be used after the join to confirm what collation should be used for the nvarchar fields. Thus the following works:
SELECT tblTurbineLocations.TurbineLayoutProjectID as ProjectID
,TurbineLayoutNumber
,Count(HubHeight) as NumTurbines
,tblTurbineLocations.WTCode
FROM [TurbineLayout].[dbo].[tblTurbineLocations]
LEFT OUTER JOIN [TurbineModel].[dbo].[tblTurbineModels]
ON tblTurbineLocations.WTCode = tblTurbineModels.WTCode
COLLATE Latin1_General_CS_AS_KS_WS
WHERE tblTurbineLocations.TurbineLayoutProjectID = 2255
AND tblTurbineLocations.TurbineLayoutNumber IN (406, 407)
GROUP BY tblTurbineLocations.TurbineLayoutProjectID ,tblTurbineLocations.TurbineLayoutNumber ,tblTurbineLocations.WTCode

Literal SQL works: Array value must start with "{" or dimension information

I am trying to add an ARRAY to an existing jsonb ARRAY. This array will be added to the ARRAY[0] of the existing array. When I hardcode the details it's working but when I try to do it dynamically it fails with the above error. what am I doing wrong?
Postgresql 13 db server version
with whatposition as (select position pos from users cross join lateral
jsonb_array_elements(user_details->'Profile') with ordinality arr(elem,position)
where display_ok=false)
update users set user_details=jsonb_set(
user_details,concat('ARRAY[''userProfile'',''',(select pos-1 from whatposition)::text,'''',',''DocumentDetails'']')::text[],
'[{"y":"supernewValue"}]')
where display_ok=false;
SQL Error [22P02]: ERROR: malformed array literal:
"ARRAY['userProfile','0','DocumentDetails']" Detail: Array value
must start with "{" or dimension information.
This is the with subquery output.
with whatposition as (select position pos from users cross join lateral
jsonb_array_elements(user_details->'userProfile') with ordinality arr(elem,position)
where display_ok=false)
select concat('ARRAY[''userProfile'',''',(select pos-1 from whatposition)::text,'''',',''DocumentDetails'']');
OUTPUT OF THE ABOVE SQL
ARRAY['userProfile','0','DocumentDetails']
But when I pass the value as a literal to the above SQL it works just fine.
with whatposition as (select position pos from users cross join lateral
jsonb_array_elements(user_details->'userProfile') with ordinality arr(elem,position)
where display_ok=false)
update users set user_details=jsonb_set(
user_details,ARRAY['userProfile','0','DocumentDetails'],'[{"y":"cccValue"}]')
where display_ok=false;
You shouldn't put the ARRAY[…] syntax in a literal value.
with whatposition as (
select position pos
from users
cross join lateral jsonb_array_elements(user_details->'Profile') with ordinality arr(elem,position)
where display_ok=false
)
update users
set user_details=jsonb_set(
user_details,
ARRAY['userProfile', (select pos-1 from whatposition)::text, 'DocumentDetails'],
'[{"y":"supernewValue"}]'
)
where display_ok=false;
The query you are trying is broken beyond the superficial syntax error (which is addressed by Bergi).
If the CTE returns multiple rows (as expected), the ARRAY constructor will fail because the nested subselect is only allowed to return a single value in this place.
To "upsert" (insert or update) the property "DocumentDetails": [{"y": "cccValue"}]} to the first element (the one with subscript 0) of the nested JSON array user_details->'userProfile':
Postgres 14 or later
Make use of JSONB subscripting:
UPDATE users
SET user_details['userProfile'][0]['DocumentDetails'] = '[{"y":"cccValue"}]'
WHERE display_ok = FALSE;
Postgres 13
Use jsonb_set() - exactly like you already have in your last code example, only without the unneeded CTE:
UPDATE users
SET user_details = jsonb_set(user_details, '{userProfile, 0, DocumentDetails}', '[{"y":"cccValue"}]')
WHERE display_ok = FALSE;
db<>fiddle here

Postgresql pass function parameter with casting

I created two functions. The first function, getProductOrder, is getting token whose type is character varying. The second function is getOrderTechnicalDetails whose function parameter is character varying.
For example When I call getProductOrder with quotation marks that is working nothing problem the function
Worked example
select
(select row_to_json(getOrderTechnicalDetails('991964b80d4f41a416b48ac99bdc07ad')) as techDetails),
o.*
from
(select * from getProductOrder(null, '54e91016-46d0-11e7-912d-001dd8b72237', null, 9181, 1::smallint)) o
But I have to use dynamic parameter here problem is casting problem function can't determine casting.
When I execute this query I get syntax error. How can I solve this problem?
Query
select
(select row_to_json(getOrderTechnicalDetails(o.token::character varying)) as techDetails),
o.*
from
(select * from getProductOrder(null, '54e6341-46d0-11e7-912d-001dd8b72237', null, 9181, 1::smallint)) o
Error
ERROR: invalid input syntax for integer: "-"
SQL state: 22P02
Context: SQL function "getordertechnicaldetails" statement 1

Have to find a count of rows from my postgresql query

Hi I tried to get a count of rows from my below query:
select count(substring(wsresult_question FROM '[0-9]+') as pumporder) AS totals,
job_id,
job_siteid,
job_completed
from webserviceresults w, jobs s
where job_siteid = '1401'
and job_id = wsresult_jobid
and job_completed is not null
and wsresult_question LIKE 'job.job_site_data.site_meters.pump.%'
and wsresult_category = 'Job'
group by pumporder,job_id,job_siteid,job_completed order by job_completed desc
I tried this and i got the error like
There was an SQL error:
ERROR: syntax error at or near "as" LINE 1: ... count(substring(wsresult_question FROM '[0-9]+') as pumpord... ^
In this line substring(wsresult_question FROM '[0-9]+') as pumporder I just tired to get only a number from some concatenate strings. The concatenate string is being like
1.job.job_site_data.site_meters.pump.0.meter_calibration_record.meter_adjustedtofast
2.job.job_site_data.site_meters.pump.0.meter_calibration_record.meter_adjustedtoslow
3.job.job_site_data.site_meters.pump.1.meter_calibration_record.meter_adjustedtofast
So substring(wsresult_question FROM '[0-9]+') as pumporder is return the numbers like 0,1 in array. I need to total the count of rows now. So Kindly help me on this.
Please let me know if you have any queries.
Thanks in advance!
your error means you should not create an alias for the function - only for the column, so if you remove as pumporder from count(substring(wsresult_question FROM '[0-9]+') as pumporder) , error will go away
Your approach though is very doubtful. If you want to count number of rows with substring(wsresult_question FROM '[0-9]+'), you better instead:
select count(1) AS totals,
job_id,
job_siteid,
job_completed
from webserviceresults w, jobs s
where job_siteid = '1401'
and job_id = wsresult_jobid
and job_completed is not null
and wsresult_question ~ '^(job.job_site_data.site_meters.pump.)[0-9]'
and wsresult_category = 'Job'
group by pumporder,job_id,job_siteid,job_completed order by job_completed desc
and lastly the string job.job_site_data.site_meters.pump.0 looks like json path, so it would be more appropriate using json array length function, not count on rows