What does ^1 mean in SQL call? - java

SELECT LOWER(pla_lan_code) as locale,
pla_auto_translate_opt_out_flag^1 as autoTranslationEnabled,
pte_manual_edit_flag^1 as autoTranslated,
ftr_created_date as queuedDate,
ftr_translation_date as translationDate,
ftr_engine as translationEngine,
ftr_id as translationId,
pla_auto_translate_opt_out_flag as translationOptOut
SELECT * FROM property_languages (nolock)
LEFT OUTER JOIN properties_text_live (nolock)
This query is embedded in Java code. I am trying to convert it into a stored proc. I want to know what ^1 equates to in SQL.

This isn't standard SQL. In Transact-SQL (used in MS SQL Server and Sybase) ^ is the bitwise exclusive-OR operator.
1 ^ 1 is 0, and 0 ^ 1 is 1.
If the original int stores 0 for false and 1 for true, XORing by 1 would reverse the sense of the original flags.
Guessing that pla_auto_translate_opt_out_flag is an int with 1 for opting out and 0 for enabling autotranslation, using the operator returns 1 for enabling and 0 for opting out.

Related

Oracle order by via JPA with numeric first

I have column data like this in my db
data
-----
1
2
A
3
4
B
I have below nls settings in place
SELECT * From NLS_SESSION_PARAMETERS;
NLS_LANGUAGE AMERICAN
NLS_TERRITORY AMERICA
NLS_CURRENCY $
NLS_ISO_CURRENCY AMERICA
NLS_ISO_CURRENCY AMERICA
NLS_NUMERIC_CHARACTERS .,
NLS_CALENDAR GREGORIAN
NLS_DATE_FORMAT DD-MON-RR
NLS_DATE_LANGUAGE AMERICAN
NLS_SORT BINARY
NLS_COMP BINARY
When I query the db for **select data from <mytable> order by data ** I am getting the result as below
data
-----
1
2
3
4
A
B
The same query via hibernate is giving the results with alphabets first and numeric later
data
-----
A
B
1
2
3
4
But, I want column to be displayed with numbers first and alphabets later via hibernate
data
------
1
2
3
4
A
B
Can someone help me with this.
You can use regularexpressions to sort in such a way,
select data from <mytable> order by REGEXP_REPLACE(data ,'[^0-9]'), REGEXP_REPLACE(data ,'[0-9]')
That depends on NLS_SORT parameter's value. For example, in my database it is set to CROATIAN and the result is what you wanted:
SQL> select * From nls_session_parameters where parameter = 'NLS_SORT';
PARAMETER VALUE
-------------------- --------------------
NLS_SORT CROATIAN
SQL> with test (data) as
2 (select '1' from dual union all
3 select '2' from dual union all
4 select 'A' from dual union all
5 select '3' from dual union all
6 select '4' from dual union all
7 select 'B' from dual
8 )
9 select data
10 from test
11 order by data;
D
-
A
B
1
2
3
4
6 rows selected.
However, in your database, NLS_SORT = BINARY. Let's try it:
SQL> alter session set nls_sort = 'BINARY';
Session altered.
SQL> with test (data) as
2 (select '1' from dual union all
3 select '2' from dual union all
4 select 'A' from dual union all
5 select '3' from dual union all
6 select '4' from dual union all
7 select 'B' from dual
8 )
9 select data
10 from test
11 order by data;
D
-
1
2
3
4
A
B
6 rows selected.
SQL>
Right; the wrong result.
Therefore, modify NLS_SORT, if that's an option. See valid values by
SQL> select * From v$nls_valid_values where parameter = 'SORT';
PARAMETER VALUE ISDEP
-------------------- -------------------- -----
SORT BINARY FALSE
SORT WEST_EUROPEAN FALSE
SORT XWEST_EUROPEAN FALSE
SORT GERMAN FALSE
<snip>
I believe one of the two options below should work,
Use NLSSORT function in the order by clause explicitly.
select * from my_table order by NLSSORT(data,'NLS_SORT=BINARY');
Create a function based index on the column,
create index mytable_nlssort_index on my_table(nlssort(data, 'nls_sort=''BINARY'''))
Use the query as below when the column is indexed.
select data from my_table order by data;

How to match number "start with" through query sqlite

Is there any way to match the query, for example, I want to search my number against the rules (table name) through query.
I want to match the number which starts with "333" rules .........
1)3322323
Here is my query
SELECT * FROM demo where rules like '33322323';
I want above query return true. because it matches with my rule.
Below is my table.
id | rules
...............
1 | 333
2 | 22
3 | 442
I have sample data 1) 33331235, 2) 2354545 3) 4424545454 4) 22343434
Case 1 (matching data 1) 33331235)
Suppose I want to check my 33331235 with my rules table so my sample data match with my
row 1 which is 333, because of my data start with 333..... it should return true because of it matched.
Case 2 (matching data 2) 2354545)
Suppose I want to check my 2354545 with my rules table so my sample data does not match with my
Any row because my no rule applies on it..... it should return true because of it matched.
Case 3 (matching data 1) 4424545454)
Suppose I want to check my 4424545454 with my rules table so my sample data match with my
row 1 which is 442, because of my data start with 442..... it should return true because of it matched.
Solved.
I solved this with the help of Forpas I used this query to match number start with
SELECT * FROM demo where '333434334 like rules ||'%';
The number at the end of the string when I use this query.
SELECT * FROM demo where '333434334' like '%' || rules;
The number anywhere in the string then I use this query.
SELECT * FROM demo where '333434334' like '%' || rules ||'%';
You need to use the operator LIKE.
You have tagged your question with both MySQL and SQLite.
For MySQL:
SELECT * FROM demo where '33322323' like concat(rules, '%');
For SQLite:
SELECT * FROM demo where '33322323' like rules || '%';
The above code will return all rows where the rules column value is the starting chars of 33322323.

MySQL update with and operator

I had written some wrong syntax for an SQL query. But still, it outputted no error with a java tomcat server. Running on Debian 9.
MySQL Version:
mysql Ver 14.14 Distrib 5.7.24, for Linux (x86_64)
The query was as follows, I had misplaced the comma ',' with 'and' after the set operator
UPDATE table_pod_print set print_status = 1 and operator_id = 2091 where id = 1
I tried running it on the console, which gave me the following output:
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
Please help me in understanding why the query worked in the first place.
In MySQL SELECT 1 AND 0; produces 0 because the AND operator evaluates operands as a logical AND. Looking at your query the SET print_status was evaluated as (adding additional brackets for clarity):
print_status = (1 AND (operator_id = 2091))
which mean it would be 1 AND 1 if operator_id = 2091 for updated id = 1 row was true and 1 AND 0 if not.

Regex in a multilingual query not working for application, but in SQL developer and unit test

I have a strange behaviour for the following query containing a regular expression:
SELECT COALESCE(lang.TITLE, ids.message_id) AS TEXT,
ids.message_id
FROM
(SELECT
CASE
WHEN regexp_instr(messages.NR, '[a-z]{2}[[:space:],_-]\d+[-_]\d{2,6}') > 0
THEN regexp_substr(messages.NR, '\d+')
ELSE messages.NR
END AS message_id
FROM
( SELECT 'GB 28647854-04' AS NR FROM dual
UNION
SELECT 'GB 5310031-05' AS NR FROM dual
UNION
SELECT '9184' AS NR FROM dual
) messages
) ids,
LOCAL_TITLES lang
WHERE ids.message_id = '' || lang.NUMBER_NO(+);
The LOCAL_TITLES contains the following entries:
5310031 | Some localized Text
9184 | Another Text
So the expected query result should be:
28647854 | 28647854
Some localized Text | 5310031
Another Text | 9184
This works well, when the query runs via SQL Developer. Also I have a (Unit-/Integration-) Test for my DAO, which runs this query returning the expected result.
My problem: when the query is executed by the running web application, then the regex does not find the numeric id. Instead, the actual query result is
GB 28647854-04 | GB 28647854-04
GB 5310031-05 | GB 5310031-05
Another Text | 9184
Do you have an idea, why the regular expression behaves differently, when it's coming from the web application?
Your regular expression is looking for lower-case characters with the [a-z] pattern. Your dual-generated data has upper-case GB, so they don't match, with default case-sensitive settings, at least in my locale:
alter session set nls_sort = 'BINARY';
SELECT
CASE
WHEN regexp_instr(messages.NR, '[a-z]{2}[[:space:],_-]\d+[-_]\d{2,6}') > 0
THEN regexp_substr(messages.NR, '\d+')
ELSE messages.NR
END AS message_id
FROM
( SELECT 'GB 28647854-04' AS NR FROM dual
UNION
SELECT 'GB 5310031-05' AS NR FROM dual
UNION
SELECT '9184' AS NR FROM dual
) messages;
MESSAGE_ID
--------------
9184
GB 28647854-04
GB 5310031-05
If you make the session case-insensitive they do:
alter session set nls_sort = 'BINARY_CI';
SELECT
CASE
WHEN regexp_instr(messages.NR, '[a-z]{2}[[:space:],_-]\d+[-_]\d{2,6}') > 0
THEN regexp_substr(messages.NR, '\d+')
ELSE messages.NR
END AS message_id
FROM
( SELECT 'GB 28647854-04' AS NR FROM dual
UNION
SELECT 'GB 5310031-05' AS NR FROM dual
UNION
SELECT '9184' AS NR FROM dual
) messages;
MESSAGE_ID
--------------
9184
28647854
5310031
You can also make it case-insensitive just within each regex call:
SELECT
CASE
WHEN regexp_instr(messages.NR, '[a-z]{2}[[:space:],_-]\d+[-_]\d{2,6}', 1, 1, 0, 'i') > 0
THEN regexp_substr(messages.NR, '\d+', 1, 1, 'i')
ELSE messages.NR
END AS message_id
...
Or just expand the character class:
WHEN regexp_instr(messages.NR, '[a-zA-Z]{2}[[:space:],_-]\d+[-_]\d{2,6}') > 0
or
WHEN regexp_instr(messages.NR, '[[:alpha:]]{2}[[:space:],_-]\d+[-_]\d{2,6}') > 0
Some of your sessions are being created with BINARY_CI (or some other case-insensitive) linguistic comparison settings, but your 'web application' setting is not. That may be down to the locale being used for each, so changing the application locale could also fix the discrepancy; but making the pattern more logical is probably better.
More specifically in your case (having looked at your profile), if your locale is Germany then your NLS_SORT with be German, which behaves the same as BINARY_CI does for me with a UK locale. Presumably your SQL Developer and unit test is being run with German settings, and your web app is not, either because of its own defaults or by design.
Read more about "SQL Regular Expressions in a Multilingual Environment" in the documentation.

PreparedStatement throwing index is out of range when it is not out of range

I am generating a table and inserting the data into that table. The table is created from data that has complex relationships. We created this so users of our BI tool could easily deal with the data.
We have a bunch of these tables that get created each one is different based on the user generated data. In one case we are getting index out of range exception. When inspecting the meta data it is not out of range.
The exact exception message is:
com.microsoft.sqlserver.jdbc.SQLServerException: The index 13 is out of range.
The generated insert statement is:
INSERT INTO [F22_AF] ([frcId],[eId],[aId],[dateCreated],[DateofThing],[Wasthisaninjury],[Whowainjured],[WhowainjuredFNAME],[WhowainjuredLNAME],[Whatwasinvolved],[WhatwasinvolvedDATA],[WhatOptionsWereAvailable],[AccidentWitnessed]) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)
When the value for accident witnessed is set the exception is thrown. When the value is about to be set I printed the column index and the value:
13 : AccidentWitnessed = false
When it fails I printed out the meta data from the insert statement and got this:
parameterMetaData.getParameterCount()=13
ColIndex - Type || Class
1 - int || java.lang.Integer
2 - int || java.lang.Integer
3 - int || java.lang.Integer
4 - datetime || java.sql.Timestamp
5 - datetime || java.sql.Timestamp
6 - nvarchar || java.lang.String
7 - int || java.lang.Integer
8 - nvarchar || java.lang.String
9 - nvarchar || java.lang.String
10 - int || java.lang.Integer
11 - nvarchar || java.lang.String
12 - int || java.lang.Integer
13 - bit || java.lang.Boolean
When I try to set col index 13 I get Index Out Of Range, however according to the meta data it exists! If it didn't exist I would get another out of range exception.
The same code works for a different set of data.
Can anyone explain why this error would occur when the column index exists?
My advice? Believe the JVM. It doesn't matter what you think.
You're not looking at the right spot to understand what's going wrong. Write a small, self-contained example and run it in a debugger. You'll be able to see where the code is going awry that way.
Same error also occured for me when I used Hibernate. But the exception does not depend to hibernate. This exception is depend to JDBC or Database version.In mine database is oracle and got same exception.Reason was the ojdbc bug which I used. In Prepared Statement if I passed more than 7 parameters I got exception.This is a bug. The solution is changing ojdbc.

Categories