I'm having problems getting Oracle's LIKE clause working with numeric bind variables.
In the examples below the TABLEID column is numeric.
In SQLDeveloper I can write SELECT * FROM TABLEX WHERE TABLEID LIKE ('201%'); which works fine. However, when I try the same query in code using a bind variable: SELECT * FROM TABLEX WHERE TABLEID LIKE :bindVar
I get an ORA-01722: invalid number error.
I've tried surrounding the bind var with () and have tried adding the % symbol to the end of the bind value with no luck.
I'm using NodeJS to make the database calls.
Any ideas as to what I might be doing wrong here?
Datatypes should match.
If TABLEID column's datatype is NUMBER and you're comparing it to a string ( (in like '201%', '201%' is a string), then you'd apply TO_CHAR function to that column:
select * from tablex
where to_char(tableid) like :bindVar
So as it turns out the problem ended up being not with my code but the data within the table itself. Sorry to waste yall's time!
Related
As community member called Saideep help with an coding issue I had here
I have attempted to modify the code to search for values with without www (after removing either http or https) and prepending it to a field value.
So, at the moment, my code does the following:
You can see from the image, that code successfully removes https:// and http, but it fails to prepend the values with www in the field newwebsite_url when doesn't exists in the homepage_url field
For example movingahead.com should appear as www.movingahead.com in newwebsite_url
My code is as follows:
SELECT tt.homepage_url
,concat(iff(left(v1.RightString, 4)='www.', null, 'www.')) as addwww , LEFT(v1.RightString,COALESCE(NULLIF(CHARINDEX('/',v1.RightString)-1,-1),150)) as newwebsite_url
FROM basecrmcbreport.organizations tt
inner join (select (SUBSTRING(homepage_url,CHARINDEX('//',homepage_url)+2,150)) from basecrmcbreport.organizations)v1(RightString) on tt.homepage_url like concat('%',v1.RightString,'%') escape '|';
I know its a concatenation issue, but not where to fix it.
Any thoughts?
As you suspected, the issue here is with concatenation. When using concat in the query, we have just given the part where taking www. if it is absent and null if it is present.
But we are not concatenating this with the extracted part from URL (instead using it to create new column). To fix this, we have to place the above function value inside concat.
The following is the demonstration of the same using my sample data.
Using the following updated query, I was able to achieve your requirement.
%sql
SELECT tt.homepage_url
,concat(iff(left(v1.RightString, 4)='www.', '', 'www.'),LEFT(v1.RightString,COALESCE(NULLIF(CHARINDEX('/',replace(v1.RightString,'\\','/'))-1,-1),150))) as newwebsite_url
FROM demo tt
inner join (select (SUBSTRING(homepage_url,CHARINDEX('//',homepage_url)+2,150)) from demo)v1(RightString) on tt.homepage_url like concat('%',v1.RightString,'%') escape '|';
NOTE: I have also replaced \ with /. Also, I have used '' (empty string) instead of using null in concat.
Can you tell me, please?
Why does the mysql2 value substitution not work inside the IN operator?
I do this, but nothing works.
Only the first character of the array is being substituted (number 6)
"select * from products_categories WHERE category_id IN (?)", [6,3]);
You can do it like this, of course:
IN(?,?,?,?,?,?,?,?,?,?) [6,3,1,1,1,1,1,1,1,1,1]
But that's not right, I thought that the IN should be automatically substituted from an array =(
I haven't used this, but my gut feeling tells that array items map to question marks based on indexes, so in your case 6 binds to first ? and 3 looks for another one, but doesn't find.
If I were you, I'd try to make sure that my first array item is then actually array, so I'd rewrite it:
"select * from products_categories WHERE category_id IN (?)", [[6,3]]);
I suspect you are using this with .execute(), which is short for prepared statements "prepare first if never executed before"+execute. While api is very similar to .query() one biggest difference is that in case of prepared statement only parameters are sent at execution time, unlike .query() where whole query text is interpolated with all parameters on the client. As a result, you need to send exactly the number of parameters as you have number of placeholders in original query text ( in you example - one ?). The whole [6,3,1,1,1,1,1,1,1,1,1] is treated as one parameter and sent to server as "6,3,1,1,1,1,1,1,1,1,1" string ( because during prepare step that parameter was likely reported by server as VAR_CHAR )
The solution is 1) use .query() and interpolate on the client or 2) build enough ?s dynamically and prepare different PS for different number of IN parameters
I'm currently trying to add some dbeaver parameters to my script.
The simple SQL Looks like this:
SELECT * FROM CONTRACT WHERE CONTRACT_NUMER = :CONTRACTNUMBER;
Now, when I run the Statement, dbeaver prompts me for the contract number which I type in. (The column is a varchar) Unfortunately it only works, if I put my Input Parameter in single Apostrophe.
When I just type in the string the following error message appears:
LE123990123 IS NOT VALID IN THE CONTEXT WHERE IT IS USED. SQLCODE=-206, SQLSTATE=42703, DRIVER=4.19.49
How do I make this Parameter being treated as a String? DB2 11 is in use here.
You MUST put quote for CHAR and VARCHAR variable.
If you look bottom in BIND PARAMETERS WINDOWS, you can read:
"Use Tab to switch. String values must be quoted. You can use expression in values"
I have an SSIS package that obtains a list of new GUIDs from a SQL table. I then shred the GUIDs into a string variable so that I have them separated out by comma. An example of how they appear in the variable is:
'5f661168-aed2-4659-86ba-fd864ca341bc','f5ba6d28-7283-4bed-9f11-e8f6bef225c5'
The problem is in the data flow task. I use the variable as a parameter in a SQL query to get my source data and I cannot get my results. When the WHERE clause looks like:
WHERE [GUID] IN (?)
I get an invalid character error so I found out the implicit conversion doesn't work with the GUIDs like I thought they would. I could resolve this by putting {} around the GUID if this were a single GUID but there are a potential 4 or 5 different GUIDs this will need to retrieve at runtime.
Figuring I could get around it with this:
WHERE CAST([GUID] AS VARCHAR(50)) IN (?)
But this simply produces no results and there should be two in my current test.
I figure there must be a way to accomplish this... What am I missing?
You can't, at least not using the mechanics you have provided.
You cannot concatenate values and make that work with a parameter.
I'm open to being proven wrong on this point but I'll be damned if I can make it work.
How can I make it work?
The trick is to just go old school and make your query via string building/concatenation.
In my package, I defined two variables, filter and query. filter will be the concatenation you are already performing.
query will be an expression (right click, properties: set EvaluateAsExpression to True, Expression would be something like "SELECT * FROM dbo.RefData R WHERE R.refkey IN (" + #[User::filter] + ")"
In your data flow, then change your source to SQL Command from variable. No mapping required there.
Basic look and feel would be like
OLE Source query
I want to write query something like :-
Select u from bookmaster u order by Year(b.createDate),Month(b.createDate),day(b.createDate)
How do i do this in JPQL? Are there any date parsing functions available?
Well Brandizzi, theese function are in your list, but checked as 'not standard'
I've tried use them without success.
Here is an alternative:
JPA Query MONTH/YEAR functions