Escape single quote in redshift sql in a column value - sql

As per the attached image the column tlist in a table 'c' has values separated by a comma such as 'HCC19','HCC18'.
I am trying to used the column values in a query condition on redshift ..
where a.risk_factor in (c.tlist)
.. ..but its not giving the expected result possibly because its taking the value a single string as '
where a.risk_factor in( ' 'HCC19','HCC18' ') and not as required in the expression where a.risk_factor in('HCC19','HCC18')
Is there any workaround possible for this situation ?

You may try using a LIKE comparison here:
WHERE c.tlist LIKE '%''' || a.risk_factor || '''%';
The above LIKE expression compares 'HCC19','HCC18' searching for the single quoted risk factor. If the risk factor already comes with single quotes, then just use:
WHERE c.tlist LIKE '%' || a.risk_factor || '%';

Related

Differene between 2 where conditions which looks similar

My mind is blind when I see those 2 where conditions and I can't understand what's the difference.
Can someone explain me? First returns 273 records and the second one returns 93. I thought they should return the same records.
1st
s.value LIKE '%One Travel%'
OR s.value LIKE '%One RSA%'
OR s.value LIKE '%One Other%'
OR s.value LIKE '%GP&U%'
OR s.value = ' '
2nd
s.value LIKE (
'%One Travel%'
,'%One RSA%'
,'%One Other%'
,'%GP&U%'
, ' ')
I don't know H2 at all, but I assume the second query evaluates to:
s.value LIKE '%One Travel%'
OR s.value LIKE '%One RSA%'
OR s.value LIKE '%One Other%'
OR s.value LIKE '%GP&U%'
OR s.value LIKE ' '
So you're left with the difference between s.value = ' ' and s.value LIKE ' '. Apparently H2 follows ANSI SQL, and the ANSI standard ignores trailing spaces in comparisons (actually the strings to be compared are padded with spaces till they are the same length). So s.value = ' ' returns TRUE for ' ', but also for '' and ' '. LIKE doesn't pad spaces to compare, since it is used for pattern matching rather than equality tests. Hence, LIKE ' ' only matches a single space.
In short; your first query returns more rows because rows where value is an empty string or multiple spaces, are also returned. Your second query returns only rows where value is a single space (ignoring the other conditions for simplicity).
LIKE in the second command is not a function, it is actually the same LIKE predicate. But its right-hand argument is specified as a row value.
I guess many DBMS throw an error, but H2 tries to convert all values here to a character string instead, in some rare cases it is useful, but in this case it isn't.
something LIKE (
'%One Travel%'
,'%One RSA%'
,'%One Other%'
,'%GP&U%'
, ' ')`
becomes
something LIKE 'ROW (%One Travel%, %One RSA%, %One Other%, %GP&U%, )'
in recent versions of H2 or
something LIKE '(%One Travel%, %One RSA%, %One Other%, %GP&U%, )'
in older versions (they don't support row values, but they treat such expressions as non-standard arrays).
Both expressions are meaningless. You need to use the first command with multiple OR conditions. Alternatively you can use a complex regular expression with non-standard REGEXP operator (with similar to LIKE syntax) or with also non-standard REGEXP_LIKE function.
Actually VALUE is a keyword, so both these commands cannot be executed by H2 (at least by its recent versions), but I assume it was just a replacement of a real column name.

operation LIKE in PL/SQL with string that contain "_" symbol

I have to do a simple select query with the LIKE operator but the problem is that the string to be compared with the LIKE operator contains the symbol "_", for example:
TRY_1245
POSTM_A422
PREP_1000X
And in the table on the DB i have:
TRY_
POSTM_
PREP_
How can I make the query work like this? because if I do the simple LIKE "TRY_1235"
the query returns nothing.
Thanks a lot in advance everyone!
I think you want:
WHERE 'TRY_1235' LIKE col || '%'
The column can be the pattern.
Note that this will match 'TRYST' because _ is a wildcard character. Perhaps the simplest solution is REGEXP_LIKE() -- assuming there are no other unusual characters:
WHERE REGEXP_LIKE('TRY_12345', '^' || col)
Or just escape the _:
WHERE 'TRY_12345' LIKE REPLACE(col, '_', '\_') || '%'

using wildcard and 'like' to compare contents of two columns in pgSQL

i have columns in two separate tables that i'm using in a join and subsequent update. i want to be able to see if one column has all of its content captured in a second column.
for instance, here is a sample of contents from each column:
city_table1 | city_table2
Portsmouth Portsmouth, New Hampshire, USA
i want to be able to have a where clause in a select statement that will match the two columns based on the contents in city_table1 (but i can't just do a left or right trim based on content variance), so i'm envisioning something like
where city_table1 like ('%' + city_table2 '%')
is my logic off here? should i referse the two fields in this where clause? i've tried this in postgres and got no results when i know it should work if i have the syntax right.
thank you!
Postgres uses || to concatenate strings. So:
where city_table2 like ('%' || city_table1 || '%')
Also, you have the comparison backwards. The shorter string is surrounded by the '%'.
And, this would be simpler with regular expressions:
where city_table2 ~ city_table1

Full Text Search Using Multiple Partial Words

I have a sql server database that has medical descriptions in it. I've created a full text index on it, but I'm still figuring out how this works.
The easiest example to give is if there is a description of Hypertensive heart disease
Now they would like to be able to type hyp hea as a search term and have it return that.
So from what I've read it seems like my query needs to be something like
DECLARE #Term VARCHAR(100)
SET #Term = 'NEAR(''Hyper*'',''hea*'')'
SELECT * FROM Icd10Codes WHERE CONTAINS(Description, #Term)
If I take the wild card out for Hypertensive and heart, and type out the full words it works, but adding the wild card in returns nothing.
If it makes any difference I'm using Sql Server 2017
So it was a weird syntax issue that didn't cause an error, but stopped the search from working.
I changed it to
SELECT * FROM Icd10Codes where CONTAINS(description, '"hyper*" NEAR "hea*"')
The key here being I needed double quotes " and not to single quotes. I assumed it was two single quotes, the first to escape the second, but it was actually double quotes. The above query returns the results exactly as expected.
this will work:
SELECT * FROM Icd10Codes where SOUNDEX(description)=soundex('Hyp');
SELECT * FROM Icd10Codes where DIFFERENCE(description,'hyp hea')>=2;
You could try a like statement. You can find a thorough explanation here.
Like so:
SELECT * FROM Icd10Codes WHERE Icd10Codes LIKE '%hyp hea%';
And then instead of putting the String in there just use a variable.
If you need to search for separated partial words, as in an array of search terms, it gets a bit tricky, since you need to dynamically build the SQL statement.
MSSQL provides a few features for full text search. You can find those here. One of them is the CONTAINS keyword:
SELECT column FROM table WHERE CONTAINS (column , 'string1 string2 string3');
For me - this had more mileage.
create a calculated row with fields as full text search.
fullname / company / lastname all searchable.
ALTER TABLE profiles ADD COLUMN fts tsvector generated always as (to_tsvector('english', coalesce(profiles.company, '') || ' ' || coalesce(profiles.broker, '') || ' ' || coalesce(profiles.firstname, '') || ' ' || coalesce(profiles.lastname, '') || ' ' )) stored;
let { data, error } = await supabase.from('profiles')
.select()
.textSearch('fts',str)

Oracle SQL - Is there a better way to concatenate multiple strings with a given delimiter?

First question, so apologies in advance if this is stupid or unoriginal, but I've searched for about 30 mins now without finding any mention anywhere of my exact question:
Is there a way to concatenate a series of strings, to be separated by a given delimiter, without manually putting the delimiter between each column being concatenated?
To give a concrete example, I currently have this:
SELECT member_no as Member#,
(member_gname
|| ' '
|| member_fname) as Name,
(member_street
|| ' '
|| member_city
|| ' '
|| member_state
|| ' '
|| member_postcode) AS Address,
member_phone AS Phone,
TO_CHAR(member_joindate, 'dd-Mon-yyyy') as Joined
FROM MEMBER;
It works fine, and produces exactly the output I wanted, but as this is for study I'm less concerned about the output and more concerned with the readability and 'best practise' factors of the .sql file itself. I understand that CONCAT() only takes two arguments, so that won't work without nesting them (which is even uglier and less readable). I'm coming in totally naively here, but I was hoping there'd be some kind of magical AWESOMECONCAT() type of function that would take all the columns i need, as well as allowing me to specify what character I want separating them (in this case, a space). Any ideas?
Also, this is a separate question not worthy of posting by itself, but is there any way to select a column 'AS' and give it a name including whitespace? E.g 'Member #' would look better imo, and 'Join Date' would be clearer, but I've tried both brackets and single quotes after the AS and neither seems to fly with SQL developer.
We can still write our own AWESOMECONCAT(). Unfortunately, Oracle has no in built function. As the concatenate operator does the basic thing.
Using double quotes in the alias, you can make the column references case sensitive and even accept blanks. But note that, any more references to that column/expression needs double quotes with same text.
SELECT member_no as "Member #",
(member_gname
|| ' '
|| member_fname) as Name,
(member_street
|| ' '
|| member_city
|| ' '
|| member_state
|| ' '
|| member_postcode) AS Address,
member_phone AS Phone,
TO_CHAR(member_joindate, 'dd-Mon-yyyy') as "Join Date"
FROM MEMBER;
Is there a way to concatenate a series of strings, to be separated by a given delimiter, without manually putting the delimiter between each column being concatenated?
The best way to do concatenation from 11g onwards is the new string literal technique q'[]'.
For example :
select q'[This is a string, 'this is also a string'.]' from dual