SELECT query using collate binary_ci with arg mapped to multiple params - spring-data-jpa

I am trying to create a spring data jpa custom query that takes 2 args and uses collate binary_ci. One arg is compared to a string using '=', the other is compared to a string using LIKE.
Example that works without collate binary_ci:
SELECT * FROM MYTABLE WHERE ID = :id AND ((MODEL LIKE %:in%) OR (DESCR LIKE %:in%)) ORDER BY ...
The LIKE arg in is mapped to multiple parameters. I cannot get this query to work. I have tried multiple things, but in the end, my attempt to include collate binary_ci is the issue. Here is what I've tried:
WHERE ID = :id AND ((MODEL LIKE %:in%) OR (DESCR LIKE %:in%)) collate binary_ci ORDER BY ...
WHERE ID = :id AND (MODEL LIKE %:in% OR DESCR LIKE %:in%) collate binary_ci ORDER BY ...
WHERE ID = :id AND ((MODEL LIKE %:in%) collate binary_ci OR (DESCR LIKE %:in%) collate binary_ci) ORDER BY
Running these queries gets me either Could not locate named parameter [in], expecting one of [in%, id] or sql statement was not ended properly or missing right parentheses
How can I make this work?
Version: Spring-Boot: (v2.4.3)
Here are version values from my sqldeveloper:
org.openide.specification.version 6.2
org.osgi.framework.os.version 10.0.0
org.osgi.framework.version 1.7.0
os.version 10.0
osgi.framework.version 3.9.1.v20140110-1610

Figured it out:
WHERE ID = :id AND ((MODEL LIKE %:in% collate binary_ci) OR (DESCR LIKE %:in% collate binary_ci)) ORDER BY...

Related

How can set collate en_us.UTF-8 in order by query on Sequelize

On postgres, order by with collate and lower writing like below.
ORDER BY convert_to(lower(column COLLATE "en_US"), 'UTF8')
But in sequelize, where should I put the "collate" query and how I write it.
// This is omitted "collate" query
sequelize.fn('convert_to', sequelize.fn('lower', sequelize.col(column)), '\'UTF8\''), 'ASC')
Try passing the argument to lower() as a sequelize.literal() You also do not need to include the single quotes in "UTF8", just passing the string will do.
order: [
sequelize.fn(
'convert_to',
sequelize.fn(
'lower',
sequelize.literal('column COLLATE "en_US"')
),
'UTF8'),
'ASC')
]

What is the correct syntax to write "merge statement" in PostgreSQL 9.6.2

code :It is merge query which is running on Postgres 9.6.2 and giving syntax error.
<<!--It is giving syntax error--->
MERGE INTO timesheets.timesheet_report AS tgt USING timesheets.tmp_timesheet_report AS src ON src.FMNo = tgt.FMNo
AND src.ts_start_dt = tgt.ts_start_dt
AND src.charge_code = tgt.charge_code WHEN NOT MATCHED
INSERT (tgt.FIRST_NAME,
tgt.LAST_NAME)
VALUES(src.FIRST_NAME,
src.LAST_NAME) WHEN MATCHED
UPDATE
SET tgt.FIRST_NAME = src.FIRST_NAME,
tgt.LAST_NAME = src.LAST_NAME;
It is ON CONFLICT
INSERT INTO table_name [your usual insert syntax here]
ON CONFLICT [some conflict definition]
DO UPDATE SET column1 = EXCLUDED.value1
So I guess your query would look like this:
INSERT INTO timesheets.timesheet_report (FMNo, ts_start_dt, charge_code, FIRST_NAME, LAST_NAME)
SELECT src.FMNo, src.ts_start_dt, src.charge_code, src.FIRST_NAME, src.LAST_NAME FROM timesheets.tmp_timesheet_report AS src
ON CONFLICT (FMNo, ts_start_dt, charge_code)
DO UPDATE
SET FIRST_NAME = EXCLUDED.FIRST_NAME,
LAST_NAME = EXCLUDED.LAST_NAME;
If you don't have primary key or unique index, then you need to create unique index on timesheets.timesheet_report using btree (FMNo, ts_start_dt, charge_code);

Column is of type timestamp without time zone but expression is of type character

I'm trying to insert records on my trying to implement an SCD2 on Redshift
but get an error.
The target table's DDL is
CREATE TABLE ditemp.ts_scd2_test (
id INT
,md5 CHAR(32)
,record_id BIGINT IDENTITY
,from_timestamp TIMESTAMP
,to_timestamp TIMESTAMP
,file_id BIGINT
,party_id BIGINT
)
This is the insert statement:
INSERT
INTO ditemp.TS_SCD2_TEST(id, md5, from_timestamp, to_timestamp)
SELECT TS_SCD2_TEST_STAGING.id
,TS_SCD2_TEST_STAGING.md5
,from_timestamp
,to_timestamp
FROM (
SELECT '20150901 16:34:02' AS from_timestamp
,CASE
WHEN last_record IS NULL
THEN '20150901 16:34:02'
ELSE '39991231 11:11:11.000'
END AS to_timestamp
,CASE
WHEN rownum != 1
AND atom.id IS NOT NULL
THEN 1
WHEN atom.id IS NULL
THEN 1
ELSE 0
END AS transfer
,stage.*
FROM (
SELECT id
FROM ditemp.TS_SCD2_TEST_STAGING
WHERE file_id = 2
GROUP BY id
HAVING count(*) > 1
) AS scd2_count_ge_1
INNER JOIN (
SELECT row_number() OVER (
PARTITION BY id ORDER BY record_id
) AS rownum
,stage.*
FROM ditemp.TS_SCD2_TEST_STAGING AS stage
WHERE file_id IN (2)
) AS stage
ON (scd2_count_ge_1.id = stage.id)
LEFT JOIN (
SELECT max(rownum) AS last_record
,id
FROM (
SELECT row_number() OVER (
PARTITION BY id ORDER BY record_id
) AS rownum
,stage.*
FROM ditemp.TS_SCD2_TEST_STAGING AS stage
)
GROUP BY id
) AS last_record
ON (
stage.id = last_record.id
AND stage.rownum = last_record.last_record
)
LEFT JOIN ditemp.TS_SCD2_TEST AS atom
ON (
stage.id = atom.id
AND stage.md5 = atom.md5
AND atom.to_timestamp > '20150901 16:34:02'
)
) AS TS_SCD2_TEST_STAGING
WHERE transfer = 1
and to short things up, I am trying to insert 20150901 16:34:02 to from_timestamp and 39991231 11:11:11.000 to to_timestamp.
and get
ERROR: 42804: column "from_timestamp" is of type timestamp without time zone but expression is of type character varying
Can anyone please suggest how to solve this issue?
Postgres isn't recognizing 20150901 16:34:02 (your input) as a valid time/date format, so it assumes it's a string.
Use a standard date format instead, preferably ISO-8601. 2015-09-01T16:34:02
SQLFiddle example
Just in case someone ends up here trying to insert into a postgresql a timestamp or a timestampz from a variable in groovy or Java from a prepared statement and getting the same error (as I did), I managed to do it by setting the property stringtype to "unspecified". According to the documentation:
Specify the type to use when binding PreparedStatement parameters set
via setString(). If stringtype is set to VARCHAR (the default), such
parameters will be sent to the server as varchar parameters. If
stringtype is set to unspecified, parameters will be sent to the
server as untyped values, and the server will attempt to infer an
appropriate type. This is useful if you have an existing application
that uses setString() to set parameters that are actually some other
type, such as integers, and you are unable to change the application
to use an appropriate method such as setInt().
Properties props = [user : "user", password: "password",
driver:"org.postgresql.Driver", stringtype:"unspecified"]
def sql = Sql.newInstance("url", props)
With this property set, you can insert a timestamp as a string variable without the error raised in the question title. For instance:
String myTimestamp= Instant.now().toString()
sql.execute("""INSERT INTO MyTable (MyTimestamp) VALUES (?)""",
[myTimestamp.toString()]
This way, the type of the timestamp (from a String) is inferred correctly by postgresql. I hope this helps.
Inside apache-tomcat-9.0.7/conf/server.xml
Add "?stringtype=unspecified" to the end of url address.
For example:
<GlobalNamingResources>
<Resource name="jdbc/??" auth="Container" type="javax.sql.DataSource"
...
url="jdbc:postgresql://127.0.0.1:5432/Local_DB?stringtype=unspecified"/>
</GlobalNamingResources>

How to CAST a value in PostgreSQL for use in WHERE with LIKE statement?

I'm trying to fix a SQL query for later convert it to Doctrine2 DQL since it's part of a Symfony2 project. This is what my DDL has:
CREATE TABLE "nomencladores"."norma" (
"id" int4 NOT NULL,
"comite_tecnico_id" int4,
"numero" VARCHAR (10) COLLATE "default" NOT NULL,
"anno" int4 NOT NULL,
"nombre" VARCHAR (255) COLLATE "default" NOT NULL,
"activo" bool,
CONSTRAINT "norma_pkey" PRIMARY KEY ("id"),
CONSTRAINT "fk_f00cbe8e84edad75" FOREIGN KEY ("comite_tecnico_id") REFERENCES "nomencladores"."comite_tecnico" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION
) WITH (OIDS = FALSE);
And I'm trying to execute a LIKE query to find %45% and I've tried all this queries without success:
The one generated by Doctrine2 in DQL
SELECT
n0_.numero AS numero0,
n0_.anno AS anno1,
n0_. ID AS id2,
n0_.nombre AS nombre3,
n0_.activo AS activo4,
n0_.comite_tecnico_id AS comite_tecnico_id5
FROM
nomencladores.norma n0_
WHERE
n0_.anno LIKE %45%;
Trying to cast the values
SELECT
n0_.numero AS numero0,
n0_.anno AS anno1,
n0_. ID AS id2,
n0_.nombre AS nombre3,
n0_.activo AS activo4,
n0_.comite_tecnico_id AS comite_tecnico_id5
FROM
nomencladores.norma n0_
WHERE
CAST (n0_.anno AS CHAR) LIKE %45%;
SELECT
n0_.numero AS numero0,
n0_.anno AS anno1,
n0_. ID AS id2,
n0_.nombre AS nombre3,
n0_.activo AS activo4,
n0_.comite_tecnico_id AS comite_tecnico_id5
FROM
nomencladores.norma n0_
WHERE
CAST (n0_.anno, "FM9999") LIKE %45%
SELECT
n0_.numero AS numero0,
n0_.anno AS anno1,
n0_. ID AS id2,
n0_.nombre AS nombre3,
n0_.activo AS activo4,
n0_.comite_tecnico_id AS comite_tecnico_id5
FROM
nomencladores.norma n0_
WHERE
to_char(n0_.anno, "FM9999") LIKE %45%
SELECT
n0_.numero AS numero0,
n0_.anno AS anno1,
n0_. ID AS id2,
n0_.nombre AS nombre3,
n0_.activo AS activo4,
n0_.comite_tecnico_id AS comite_tecnico_id5
FROM
nomencladores.norma n0_
WHERE
n0_.anno::text LIKE "%45%"
And none works, what is the right way to achieve this on PostgreSQL?
The syntax could be:
WHERE n0_.anno::text LIKE '%45%';
You need to cast the number to text (or varchar) before you can use it with the LIKE operator.
The right hand argument for LIKE is a text value. Your input is a string literal to be precise. You need single quotes for values, double quotes are for identifiers.
If anno is supposed to hold a year and you are just interested in the last two digits, make that:
WHERE n0_.anno::text LIKE '%45';
Or better, yet:
WHERE n0_.anno % 100 = 45;
% being the modulo operator. (Not related to the % symbol in LIKE patterns!)
45 (without quotes) being a numeric constant.

DB2 Case Sensitivity

I'm having great difficultly making my DB2 (AS/400) queries case insensitive.
For example:
SELECT *
FROM NameTable
WHERE LastName = 'smith'
Will return no results, but the following returns 1000's of results:
SELECT *
FROM NameTable
WHERE LastName = 'Smith'
I've read of putting SortSequence/SortType into your connection string but have had no luck... anyone have exepierence with this?
Edit:
Here's the stored procedure:
BEGIN
DECLARE CR CURSOR FOR
SELECT T . ID ,
T . LASTNAME ,
T . FIRSTNAME ,
T . MIDDLENAME ,
T . STREETNAME || ' ' || T . ADDRESS2 || ' ' || T . CITY || ' ' || T . STATE || ' ' || T . ZIPCODE AS ADDRESS ,
T . GENDER ,
T . DOB ,
T . SSN ,
T . OTHERINFO ,
T . APPLICATION
FROM
( SELECT R . * , ROW_NUMBER ( ) OVER ( ) AS ROW_NUM
FROM CPSAB32.VW_MYVIEW
WHERE R . LASTNAME = IFNULL ( #LASTNAME , LASTNAME )
AND R . FIRSTNAME = IFNULL ( #FIRSTNAME , FIRSTNAME )
AND R . MIDDLENAME = IFNULL ( #MIDDLENAME , MIDDLENAME )
AND R . DOB = IFNULL ( #DOB , DOB )
AND R . STREETNAME = IFNULL ( #STREETNAME , STREETNAME )
AND R . CITY = IFNULL ( #CITY , CITY )
AND R . STATE = IFNULL ( #STATE , STATE )
AND R . ZIPCODE = IFNULL ( #ZIPCODE , ZIPCODE )
AND R . SSN = IFNULL ( #SSN , SSN )
FETCH FIRST 500 ROWS ONLY )
AS T
WHERE ROW_NUM <= #MAXRECORDS
OPTIMIZE FOR 500 ROW ;
OPEN CR ;
RETURN ;
Why not do this:
WHERE lower(LastName) = 'smith'
If you're worried about performance (i.e. the query not using an index), keep in mind that DB2 has function indexes, which you can read about here. So essentially, you can create an index on upper(LastName).
EDIT
To do the debugging technique I discussed in the comments, you could do something like this:
create table log (msg varchar(100, dt date);
Then in your SP, you can insert messages to this table for debugging purposes:
insert into log (msg, dt) select 'inside the SP', current_date from sysibm.sysdummy1;
Then after the SP runs, you can select from this log table to see what happened.
If you want case-insensitive in your procedure, try using this option in it:
SET OPTION SRTSEQ = *LANGIDSHR ;
You should also create an index to support it for performance. Create the index when you have *LANGIDSHR as a connection attribute, and the shared-weight index should then be available to later jobs. (There are various ways to get the appropriate setting into effect.)
*LANGIDSHR relates to the language-ID for your jobs. Characters in the character set that might be considered as "equals", such as 'A' and 'a' or 'ü' and 'u', should be given equal weights (shared) and so select together.
I did something similar when I wanted a case insensitive search. I used UPPER(mtfield) = 'SEARCHSTRING'. I know this works.
See: https://stackoverflow.com/a/47181640/5507619
Database setting
There is a database config setting you can set at database creation. It's based on unicode, though.
CREATE DATABASE yourDB USING COLLATE UCA500R1_S1
The default Unicode Collation Algorithm is implemented by the UCA500R1 keyword without any attributes. Since the default UCA cannot simultaneously encompass the collating sequence of every language supported by Unicode, optional attributes can be specified to customize the UCA ordering. The attributes are separated by the underscore (_) character. The UCA500R1 keyword and any attributes form a UCA collation name.
The Strength attribute determines whether accent or case is taken into account when collating or comparing text strings. In writing systems without case or accent, the Strength attribute controls similarly important features.
The possible values are: primary (1), secondary (2), tertiary (3), quaternary (4), and identity (I). To ignore:
accent and case, use the primary strength level
case only, use the secondary strength level
neither accent nor case, use the tertiary strength level
Almost all characters can be distinguished by the first three strength levels, therefore in most locales the default Strength attribute is set at the tertiary level. However if the Alternate attribute (described below) is set to shifted, then the quaternary strength level can be used to break ties among white space characters, punctuation marks, and symbols that would otherwise be ignored. The identity strength level is used to distinguish among similar characters, such as the MATHEMATICAL BOLD SMALL A character (U+1D41A) and the MATHEMATICAL ITALIC SMALL A character (U+1D44E).
Setting the Strength attribute to higher level will slow down text string comparisons and increase the length of the sort keys.
Examples:
UCA500R1_S1 will collate "role" = "Role" = "rôle"
UCA500R1_S2 will collate "role" = "Role" < "rôle"
UCA500R1_S3 will collate "role" < "Role" < "rôle"
This worked for me. As you can see, ..._S2 ignores case, too.
Using a newer standard version, it should look like this:
CREATE DATABASE yourDB USING COLLATE CLDR181_S1
Collation keywords:
UCA400R1 = Unicode Standard 4.0 = CLDR version 1.2
UCA500R1 = Unicode Standard 5.0 = CLDR version 1.5.1
CLDR181 = Unicode Standard 5.2 = CLDR version 1.8.1
If your database is already created, there is supposed to be a way to change the setting.
CALL SYSPROC.ADMIN_CMD( 'UPDATE DB CFG USING DB_COLLNAME UCA500R1_S1 ' );
I do have problems executing this, but for all I know it is supposed to work.
Generated table row
Other options are e.g. generating a upper case row:
CREATE TABLE t (
id INTEGER NOT NULL PRIMARY KEY,
str VARCHAR(500),
ucase_str VARCHAR(500) GENERATED ALWAYS AS ( UPPER(str) )
)#
INSERT INTO t(id, str)
VALUES ( 1, 'Some String' )#
SELECT * FROM t#
ID STR UCASE_STR
----------- ------------------------------------ ------------------------------------
1 Some String SOME STRING
1 record(s) selected.