PostgreSQL - JSONB in WHERE - postgresql

I have a field that is defined to be JSONB, though in reality it has just text (no, I can't change the type as a solution).
This query:
SELECT value FROM property WHERE id = 1;
returns this:
-[ RECORD 1 ]--
value | "IP"
Now, I want to query by this value e.g. SELECT value FROM property WHERE value = 'IP'. I tried several different casting (value::TEXT = 'IP', value::VARCHAR = 'IP') but they all return no results. What is the correct way to do this query?

SELECT value
FROM property
WHERE value = to_jsonb('IP')
If you prefer casting value to string then you have to fix your right side and pass "IP" instead of IP. However if the right side is actually not fixed (i.e. values other than IP are possible) then you would have to properly escape it, e.g. for A"B the actual value would be "A\"B". Thus I suggest my original solution (which takes care of that).

Converting a scalar JSON value to text, isn't unfortunately as straight-forward as one might wish. Using to_jsonb() on the right hand (as shown in frekish's answer) side is probably the best solution.
If you are looking for alternatives, you can convert the JSON value to text, using the #>> operator.
select value
from property
where value #>> '{}' = 'IP'
'{}' essentially means "the top level value"
The problem with casting such a value to text is that the double quotes are retained. Another option would therefor be:
select value
from property
where trim('"' from value::text) = 'IP'

Related

Default Value Expression for Optional String Input iReport

I'm using PostgreSQL as DBMS, in my current report I need an optional String parameter to get records by Id, which is a String field.
So I set the Default Value Expression to:
($P{Param} == null || $P{Param}.equals("")) ? "" : "AND id='" + $P{Param} + "'"
When the field is empty the report is created without issues, but when I enter a valid Id the compiler complains:
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "160E0"   Position: 3126
Just like if I was adding double quotes after and before the value I'm passing. Somebody know how to handle this problem when using String values?
I guess, you've created one Parameter, named Param and in your query you have $P!{Param}. Thing is, default value is set only when parameter stays NULL after prompting, so when you input your ID, ${Param} value is passed into query as it was inputed by user (without AND id=' part).
Try creating second parameter, lets name it $P{input}; set its default value to your expression, "Use as a prompt" value to false. Pass it to your query ($P!{input}). Now, you have your ${Param} for prompting and when it's value is set to the desired ID, $P{input} is set to your query condition.

How do you retrieve a date value from a buffer-field in Progress Openedge?

I've got a buffer which contains a mix of data, number and character fields. I am getting the displaying the values of the fields, but for some reason date fields return "?" when I try to add them to a string.
I still get ? even if I do
ASSIGN lvString = lvString + STRING( hField:BUFFER-VALUE ).
I've also tried assigning the BUFFER-VALUE to a local DATE variable, and converting that to a string, but that doesn't work either - still ?.
However if I use the STRING-VALUE attribute, it works fine.
How do I get the value out as a date field, rather than just a string?
There are two ways that you can use to achieve your needs. One is to use directly the table buffer and the other is to use an QUERY handle.
First example using a buffer directly from a table (or a TEMP-TABLE, doesn't matter):
DEF VAR dateVar AS DATE NO-UNDO.
FIND FIRST job NO-LOCK.
dateVar = DATE(BUFFER job:BUFFER-FIELD('dt-job'):BUFFER-VALUE).
MESSAGE dateVar
VIEW-AS ALERT-BOX INFO BUTTONS OK.
Second example using a query handle:
DEF VAR dateVar AS DATE NO-UNDO.
DEF QUERY qrJob FOR job.
OPEN QUERY qrJob FOR EACH job.
QUERY qrJob:GET-FIRST().
dateVar = DATE(QUERY qrJob:GET-BUFFER-HANDLE(1):BUFFER-FIELD('dt-job'):BUFFER-VALUE).
MESSAGE dateVar
VIEW-AS ALERT-BOX INFO BUTTONS OK.
As Tim Kuehn said you can substitute 'dt-job' by # of field in the query if you know its position inside the query. I could used BUFFER-FIELD(2) in substitution of BUFFER-FIELD('dt-job') because dt-job is the #2 field in my query. Keep in mind that use the FIELDS clause in a FOR EACH or in an OPEN QUERY statement changes the order of fields in query. Generally, for browsers only available the columns fields specified in FIELDS section, in order.
These might work for you. It's important to say that BUFFER-VALUE always returns a CHARACTER data type and because of this you need to use DATE statement for data conversion.
Hope it helps.
The standard form for getting a data of the field's data type is
buffer table-name:buffer-handle:buffer-field("field-name"):buffer-value.
for arrays it's:
buffer table-name:buffer-handle:buffer-field("field-name"):buffer-value[array-element].
You can also substitute a field # for "field-name" to get the buffer field handle.

DB2 how do I replace a double quote returned by an XML element

I am able to successfully retrieve the value of XML element using the following SQL
*SELECT XMLQUERY('$item/*:ItemSpec/*:AdditionalDescription/*:ABCDescription/text()' PASSING productval.value_xml as "item") AS H_DESCRIPTION FROM USER1.XMETA*
This returns a value that has double quotes (") in it. How do I replace it with a different value in the same select query. I tried something like this but it didn't work
Select REPLACE(XMLQUERY('$item/*:ItemSpec/*:AdditionalDescription/*:ABCDescription/text()' PASSING productval.value_xml as "item"),'"','QUOT') AS H_DESCRIPTION
The error is No authorized routine named "REPLACE" of type "FUNCTION" having compatible arguments was found...SQLCODE=-440,SQLSTATE==-42884.
XMLQUERY returnes an XML type, try casting it to varchar before applying REPLACE on it:
REPLACE(XMLCAST(XMLQUERY('$item/*:ItemSpec/*:Addition ...) AS VARCHAR(...)), '"','QUOT') AS ..

SQL Report Builder 3.0 Multi Value Parameter

I have a report that is required to accept number ranges and comma delimited values in a text field parameter. The parameter is for 'Account Type' and they want to be able to enter "1,2,5-9" and that will take integer values of 1,2,5,6,7,8,9. I know how to do this with a single value but never with a range.
The example code I would use for a single value is:
SELECT
arcu.vwARCUAccount.AccountType
,arcu.vwARCUAccount.ACCOUNTNUMBER
FROM
arcu.vwARCUAccount
WHERE
arcu.vwARCUAccount.AccountType = #AccountType
Any information would be extremely helpful. Someone on my team already estimated it and said it could be done without even realising that they wanted a range so now I am stuck figuring it out. I bet everyone here has been in my position so I thank everyone in advance.
There are several things you want to do.
A. Set up a parameter that is data type 'integer' and ensure you select the checkbox 'Allow multiple values'. Set it's value to be 'Ints'. Hit okay for now.
This essentially set up an array available list of a data set that you define for that type of data type that can be passed more than one dataset.
B. Create a simple dataset called 'values' that is like so
declare #Ints table ( id int);
insert into #Ints values (1),(2),(5),(6),(7),(8),(9)
C. Go back to your variable in step one and open up it's properties. Select 'Available Values' on the side pane. Choose radio button 'Get values from a query'. List your data set as 'values' and your value and label to be 'id'.
You have now bound your parameter array to values you specified. However a user DOES NOT have to just choose one or all of these but choose one or many of them.
D. You need to set up your main dataset(which I assume you already did before coming here). For the purpose of my example I will make a simple one up. I create a dataset called person:
declare #Table Table ( personID int identity, person varchar(8));
insert into #Table values ('Brett'),('Brett'),('Brett'),('John'),('John'),('Peter');
Select *
from #Table
where PersonID in (#Ints)
The important part is the predicate showing:
'where PersonID in (#Ints)'
This tells the dataset that it is dependent on the user to select a value in this array parameter.
I'm not completely proficient with tsql but what about using reg expressions?
See LIKE (Transact-SQL)
Such as:
arcu.vwARCUAccount.AccountType like '[' + replace(#AccountType, ',','') + ']'
This could work. As a broud brush try:
Remove the filter of the account type from the tsql.
Create a vb function that inputs a number, this being the account type, and tests to see if it is in the parameter string supplied by the user and outputs a 1 or 0. Vb Functions
Function test(byval myin as integer, byval mylimits as string) as integer
'results as 1 or 0
dim mysplit as string()= Split(mylimits, ",")
dim mysplit2 as string(1)
'look through all separated by a ","
For Each s As String In mysplit
'does there exists a range, i.e. "-"?
if s like "%-%" then
mysplit2 = split(s, "-")
'is the value between this range?
if myin >= mysplit(0) andalso myin <= mysplit(1) then
return 1
end if
'is the value equal to the number?
elseif s = myin then
return 1
end if
Next s
return 0
End
3. Create a filter on the dataset using the vb function with the account type as the input equal to 1.
=code.test(Fields!AccountType.Value, Paramaters!MyPar.Value)

Specifying a DB table dynamically? Is it possible?

I am writing a BSP and based on user-input I need to select data from different DB tables. These tables are in different packages. Is it possible to specify the table I want to use, based on its path, like this:
data: path1 type string value 'package1/DbTableName',
path2 type string value 'package2/OtherDbTableName',
table_to_use type string.
if some condition
table_to_use = path1.
elseif some condition
table_to_use = path2.
endif.
select *
from table_to_use
...
endselect
I am new to ABAP & Open SQL and am aware this could be an easy/silly question :) Any help at all would be very much appreciated!
You can define the name of the table to use in a variable, and then use the variable in the FROM close of your request :
data tableName type tabname.
if <some condition>.
tableName='PA0001'.
else.
tableName='PA0002'.
endif.
select * from (tableName) where ...
there are a few limitation to this method, as the stable can not contains fields of type RAWSTRING, STRING or SSTRING.
as for the fact that the table are in different package, i don't think it matters.
Regards,