Text to List in SQL - sql

Is there any way on how to convert a comma separated text value to a list so that I can use it with 'IN' in SQL? I used PostgreSQL for this one.
Ex.:
select location from tbl where
location in (replace(replace(replace('[Location].[SG],[Location].[PH]', ',[Location].[', ','''), '[Location].[', ''''), ']',''''))
This query:
select (replace(replace(replace('[Location].[SG],[Location].[PH]', ',[Location].[', ','''), '[Location].[', ''''), ']',''''))
produces 'SG','PH'
I wanted to produce this query:
select location from tbl where location in ('SG','PH')
Nothing returned when I executed the first query. The table has been filled with location values 'SG' and 'PH'.
Can anyone help me on how to make this work without using PL/pgSQL?

So you're faced with a friendly and easy to use tool that won't let you get any work done, I feel your pain.
A slight modification of what you have combined with string_to_array should be able to get the job done.
First we'll replace your nested replace calls with slightly nicer replace calls:
=> select replace(replace(replace('[Location].[SG],[Location].[PH]', '[Location].', ''), '[', ''), ']', '');
replace
---------
SG,PH
So we strip out the [Location]. noise and then strip out the leftover brackets to get a comma delimited list of the two-character location codes you're after. There are other ways to get the SG,PH using PostgreSQL's other string and regex functions but replace(replace(replace(... will do fine for strings with your specific structure.
Then we can split that CSV into an array using string_to_array:
=> select string_to_array(replace(replace(replace('[Location].[SG],[Location].[PH]', '[Location].', ''), '[', ''), ']', ''), ',');
string_to_array
-----------------
{SG,PH}
to give us an array of location codes. Now that we have an array, we can use = ANY instead of IN to look inside an array:
=> select 'SG' = any (string_to_array(replace(replace(replace('[Location].[SG],[Location].[PH]', '[Location].', ''), '[', ''), ']', ''), ','));
?column?
----------
t
That t is a boolean TRUE BTW; if you said 'XX' = any (...) you'd get an f (i.e. FALSE) instead.
Putting all that together gives you a final query structured like this:
select location
from tbl
where location = any (string_to_array(...))
You can fill in the ... with the nested replace nastiness on your own.

Assuming we are dealing with a comma-separated list of elements in the form [Location].[XX],
I would expect this construct to perform best:
SELECT location
FROM tbl
JOIN (
SELECT substring(unnest(string_to_array('[Location].[SG],[Location].[PH]'::text, ',')), 13, 2) AS location
) t USING (location);
Step-by-step
Transform the comma-separated list into an array and split it to a table with unnest(string_to_array()).
You could do the same with regexp_split_to_table(). Slightly shorter but more expensive.
Extract the XX part with substring(). Very simple and fast.
JOIN to tbl instead of the IN expression. That's faster - and equivalent while there are no duplicates on either side.
I assign the same column alias location to enable an equijoin with USING.

Directly using location in ('something') works
I have create a fiddle that uses IN clause on a VARCHAR column
http://sqlfiddle.com/#!12/cdf915/1

Related

Rearranging HEX string in SQL

I have this Blob person_profile ID that looks like this.
5FAABE4197CBA344A6C0735C33866BF6
I got that output using this query
SELECT HEX(person_profile_id) FROM person_profile
Now I need a query that rearranges it so that it will look like this
5FAABE41-97CB-A344-A6C0-735C33866BF6
Is this possible?
SUBSTRING lets your pull out parts of your hex string, while CONCAT allows you to put it back together, with some dashes thrown in.
SELECT CONCAT(
SUBSTRING(HEX(person_profile_id),1,8), '-', SUBSTRING(HEX(person_profile_id),9,4), '-',
SUBSTRING(HEX(person_profile_id),13,4), '-',
SUBSTRING(HEX(person_profile_id),17,4), '-',
SUBSTRING(HEX(person_profile_id),21)
) AS id_with_dashes;

SQL remove curly bracket

When i select a column i get the below results
{"ac92b9f2-c7c7-4d2a-982b-c06bb218a5ed"}
Is there a way to remove the {""} from the results?
This should work on most rdbms (though you should specify your rdbms anyway):
select substr(your_column, 3, length(your_column) - 4)
from your_table
Try this
Select
replace(replace(replace(col_name, '{', ''), '}', ''),
'"',") from table;
Or as per your data you can simply use
Select
Substr(col_name, 3,LENGTH(col_name)-4)
from table;
Check this for reference demo:
http://sqlfiddle.com/#!4/e24574/3
When you get the GUID value in your application from the database use this in your SQL query
SELECT CONVERT(VARCHAR(300), MYGUID)
instead of
SELECT MYGUID
Where MYGUID is the name of a field in your table.
This conversion CONVERT(VARCHAR(300), MYGUID) will remove bracket inside your application and you will get the value "ac92b9f2-c7c7-4d2a-982b-c06bb218a5ed" instead {"ac92b9f2-c7c7-4d2a-982b-c06bb218a5ed"}.

How to grab certain value found in a string column?

I have a column that contains several different values. This is not in JSON format. It is a string that is separated into different sections. I need to grab everything that is found under ID only.
In the examples below, I only want to grab the word: "syntax" and "village"
select value.id
from TBL_A
The above does not work since this is not a json.
Does anyone know how to grab the full word that is found under the "id" section in that string column?
Even though it's a string, since it's in properly formatted JSON you can convert the string to a JSON variant like this:
select parse_json(VALUE);
You can then access its properties using standard colon and dot notations:
select parse_json(VALUE):id::string
I would go with Greg's option of treat it as JSON because it sure looks like JSON, but if you know under some situations it most definitely is not JSON like, you could use SPLIT_TO_TABLE, and TRIM, if you know , is not inside any of the strings
SELECT t.value,
TRIM(t.value,'{}') as trim_value,
s.value as s_value,
split_part(s_value, ':', 1) as token,
split_part(s_value, ':', 2) as val,
FROM table t
,LATERAL SPLIT_TO_TABLE(trim_value, ',') s
Which can be compacted up, filtered with QUALIFY to get just the rows you want:
SELECT
SPLIT_PART(s.value, ':', 2) AS val,
FROM table t
,LATERAL SPLIT_TO_TABLE(TRIM(t.value,'{}'), ',') s
QUALIFTY SPLIT_PART(s.value, ':', 1) = 'id'

Extracting values between two delimiters for an entire column in SQL

I am looking for a way to extract all substrings between two delimiters over an entire column.I have found ways to do this for each string separately, but I need something I can apply over the entire column.
For example if I have a column called "NAMES" that contains the below values:
1235_brandon_098410090
1242353_sam_1920420101222
134214_kristein_39402384
I want my output to be
brandon
sam
kristein
how do I do this?
I've tried this:
regex_substr(names,'_(.*?)_'
Query Error: error: function regex_substr(character varying, unknown) does not exist
In MySQL, I think you can use substring_index():
select substring_index(substring_index(names, '_', 2), '_', -1)
This extracts the second value delimited by underscores, which is what all the sample data suggests is needed.
EDIT:
Your error message looks like Postgres. This is the equivalent in Postgres:
select v.*, split_part(names, '_', 2)
from (values ('1242353_sam_1920420101222')) v(names);
In Postgres, you can use substring() with a pattern as well:
select v.*, substring(names from '[A-Za-z]+')

remove comma within values in comma-delimited table column

I'm trying to remove all the commas in a column, for example:
column 1 contains:
1,232, 3,123, 123, 32,223
How do I remove the commas so that it updates the rows to:
1232, 3123, 123, 32223
I've tried the following:
SELECT REPLACE(col1,',','');
but I get the following error:
Error Code: 1054. Unknown column 'col1' in 'field list'
Don't store numbers in strings! That is a bad representation and Oracle offers much better solutions such as nested tables or JSON.
Sometimes we are stuck with other people's bad design decisions. I think the simplest method in this place is a multistep replace:
select replace(replace(replace(col1, ', ', '~ '), ',', ''), '~ ', ', ')
This assumes that ~ does not appear in the string.
Usecase : Your table have a column with varchar type, and you want to remove all the commas from the values stored in this column.
initially I wasn't sure how to apply the above mentioned solutions. This is how I achieved what I wanted.
update book set title = replace(title,'.','') where bookid<>0
update CityNames set City = Replace(cn.City,',','') from CityNames cn
This worked for me, I have replaced all the commas with blankspace with this and updated the table