Field Description:
User_id Unique identifier of every user following these creators
Creator_id List of creator ids separated by ‘&’
User_id,Creator_IDs
U100,A300&A301&A302
U101,A301&A302
U102,A302
U103,A303&A301&A302
U104,A304&A301
U105,A305&A301&A302
U106,A301&A302
U107,A302
Note: I have to remove U and A before the values, I though I could use substring for U but what can I do for A since it is varying.
Moreover going forward I have to use this data to have distinct creator_id and subsequent user following them.
You could try using regexp_replace eg:
select regexp_replace(User_id, "^U", "")
, regexp_replace(regexp_replace(Creator_IDs, "A", ""), '&', ',')
You can use REPLACE function to remove A from the string. The function would be something like this -
SELECT REPLACE('A300&A301&A302', 'A','') AS NewString;
For the entire query -
select concat (REPLACE('U100', 'U',''),',',REPLACE('A300&A301&A302', 'A',''));
You can use this to see how it works. For your query of course you have to use the column names -
select concat (REPLACE(user_id, 'U',''),',',REPLACE(Creator_Id, 'A',''));
Related
Newbie here. Been searching for hours now but I can seem to find the correct answer or properly phrase my search.
I have thousands of rows (orderids) that I want to put on an IN function, I have to run a LIKE at the same time on these values since the columns contains json and there's no dedicated table that only has the order_id value. I am running the query in BigQuery.
Sample Input:
ORD12345
ORD54376
Table I'm trying to Query: transactions_table
Query:
SELECT order_id, transaction_uuid,client_name
FROM transactions_table
WHERE JSON_VALUE(transactions_table,'$.ordernum') LIKE IN ('%ORD12345%','%ORD54376%')
Just doesn't work especially if I have thousands of rows.
Also, how do I add the order id that I am querying so that it appears under an order_id column in the query result?
Desired Output:
Option one
WITH transf as (Select order_id, transaction_uuid,client_name , JSON_VALUE(transactions_table,'$.ordernum') as o_num from transactions_table)
Select * from transf where o_num like '%ORD12345%' or o_num like '%ORD54376%'
Option two
split o_num by "-" as separator , create table of orders like (select 'ORD12345' as num
Union
Select 'ORD54376' aa num) and inner join it with transf.o_num
One method uses OR:
WHERE JSON_VALUE(transactions_table, '$.ordernum') LIKE IN '%ORD12345%' OR
JSON_VALUE(transactions_table, '$.ordernum') LIKE '%ORD54376%'
An alternative method uses regular expressions:
WHERE REGEXP_CONTAINS(JSON_VALUE(transactions_table, '$.ordernum'), 'ORD12345|ORD54376')
According to the documentation, here, the LIKE operator works as described:
Checks if the STRING in the first operand X matches a pattern
specified by the second operand Y. Expressions can contain these
characters:
A percent sign "%" matches any number of characters or
bytes.
An underscore "_" matches a single character or byte.
You can escape "\", "_", or "%" using two backslashes. For example, "\%". If
you are using raw strings, only a single backslash is required. For
example, r"\%".
Thus , the syntax would be like the following:
SELECT
order_id,
transaction_uuid,
client_name
FROM
transactions_table
WHERE
JSON_VALUE(transactions_table,
'$.ordernum') LIKE '%ORD12345%'
OR JSON_VALUE(transactions_table,
'$.ordernum') LIKE '%ORD54376%
Notice that we specify two conditions connected with the OR logical operator.
As a bonus information, when querying large datasets it is a good pratice to select only the columns you desire in your out output ( either in a Temp Table or final view) instead of using *, because BigQuery is columnar, one of the reasons it is faster.
As an alternative for using LIKE, you can use REGEXP_CONTAINS, according to the documentation:
Returns TRUE if value is a partial match for the regular expression, regex.
Using the following syntax:
REGEXP_CONTAINS(value, regex)
However, it will also work if instead of a regex expression you use a STRING between single/double quotes. In addition, you can use the pipe operator (|) to allow the searched components to be logically ordered, when you have more than expression to search, as follows:
where regexp_contains(email,"gary|test")
I hope if helps.
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]+')
I have 10 columns and their values could be either null, or a name of a fruit.
I would like to add another column with all the fruits that every row has. I have used Concat(column1 , column2,..., column10) as name.
Issue : There are no commas coming on the result and if I add the comma before concatenating, we are having them together, the last word is also a comma.
Any ideas?
Thanks!
You can use the standard concatenation (||) in conjunciton with COALESCE function, which returns the value of the first non-null argument.
Example:
select coalesce(column1||',', '')||coalesce(column2||',', '')|| ... ||coalesce(column10||, '');
I have a database table that has prefix and phone number fields they are varchar2 so user can enter a phone number as +1999 999-9999 now i need to select the numerical values only and leave out the - in a select statement. So the + sign is selected but the - sign is not selected how can I do this? I am reading up on substr but this is getting confusing
Try this:
select replace(phonenumber,'-','') from yourtable
This will remove the - sign from your select results, while keeping + intact. If you want to remove the + sign as well, just nest the replace function calls.
I have an SQL question. Everything works fine in the below SELECT statement except the portion I have highlighted in bold. What I'm trying to do is allow the user to search for a specific Rule within the database. Unfortunately, I do not actually have a Rule column, and so I need to concatenate certain field values to create a string with which to compare to the user's searchtext.
Any idea why the part in bold does not work? In theory, I would like this statement to check for whether the string "Rule " + part_num (where part_num is the value contained in the part_num field) equals the value of searchtext (the value of searchtext is obtained from my PHP script).
I did some research on concatenating strings for SQL purposes, but none seem to fit the bill. Does someone out there have any suggestions?
SELECT id,
part_num,
part_title,
rule_num,
rule_title,
sub_heading_num,
sub_heading,
contents
FROM rules
WHERE part_title LIKE "%'.$searchtext.'%"
OR rule_title LIKE "%'.$searchtext.'%"
OR sub_heading LIKE "%'.$searchtext.'%"
OR contents LIKE "%'.$searchtext.'%"
OR "rule" + part_num LIKE "%'.$searchtext.'%" --RULE PLUS PART_NUM DOESN'T WORK
ORDER BY id;
Since you didn't specify which DB your using, I'm going to assume SQL Sever.
Strings are specified in SQL Server with single quotes 'I'm a string', not double quotes.
See + (String Concatenation) on MSDN for examples.
Another possibility is that part_num is a numeric. If so, cast the number to a string (varchar) before concatenating.