Can anyone help me write a sql query - sql

jkdfhdjfhjh&name=ijkjkjkjkjkjk&id=kdjkjkjkjkjkjjjd&class=kdfjjfjdhfjhf
The above string has some characters starting with & and ending with =
for example we have &name= and I just need this from the above string.
similarly I need &id=, &class=
I need the output under a single column.
Final Extract
----------------------
&id=, &class=, &name=
can anyone help me out in writing a query for this.

You could try this :
select regexp_replace('jkdfhdjfhjh&name=ijkjkjkjkjkjk&id=kdjkjkjkjkjkjjjd&class=kdfjjfjdhfjhf', '\\w*?(&.*?=)\\w+((?=&)|$)', '\\1, ', 'g');
result:
regexp_replace
-------------------------
&name=, &id=, &class=,
Then it's up to you to remove the last ,.
The regexp_replace function is available in version 8.1 and after.

If you want the values along with each variable, I would implement this by splitting on "&" into an array and then taking a slice of the desired elements:
SELECT (string_to_array('jkdfhdjfhjh&name=ijkjkjkjkjkjk&id=kdjkjkjkjkjkjjjd&class=kdfjjfjdhfjhf','&'))[2:4];
Output in PostgreSQL 8.4 (array type):
{name=ijkjkjkjkjkjk,id=kdjkjkjkjkjkjjjd,class=kdfjjfjdhfjhf}
The example string is very wide so here's the general form to show the array slicing more clearly:
SELECT ((string_to_array(input_field,'&'))[2:4];
NOTE: You must have the extra parentheses around the string_to_array() call in order for the array slicing to work--you'll get an error otherwise.

Related

Using the function SPLIT_PART twice in PostgreSQL?

I have a TEXT column where each text is formatted as such:
/customers/{customer_id}/views/{id1}~{id2}/
I am trying to fetch the id2 only.
My idea is how to split the string by the / character first, where I will have:
customers, {customer_id}, views, {id1}~{id2}.
And then, get the last position:
{id1}~{id2}. And then split it again by the ~ character, and finally get the last position.
The issue is that I am new to SQL and I have no idea if this is even possible. How can I do that and end up with only one column?
SELECT
split_part(thetext, '/', 4) as temp
// how do I proceed from here?
FROM mytable
EDIT:
Some examples:
/customers/1231341/views/1312391293~3432491/
/customers/2213441/views/424131~231321341/
The IDs are of different lengths as well.
Use regexp_replace() to capture the part you want while matching the whole input, and replacing (the whole input) with the capture:
select regexp_replace(thetext, '.*~(.*)/', '\1') as temp
from mytable
See live demo.

Athena SPLIT_PART last element

I have no normalised fields in a no-sql json extract queried from ATHENA.
I would like to get the last value of my field.
fields examples:
Raleigh, NC, USA
Frankfurt, Germany
In the idea i would like something like this to select last element:
SPLIT_PART(city, ',' , last_element ) AS country
I don't know if i use the right function to perform this.
Bonus: how to select a field with like value.from without raise sql error ? :)
You can use regexp_extract():
select regexp_extract(city, '[^ ,]+$')
This returns the last set of strings in city that do not contain spaces or commas -- the last element you are looking for.
If instead of split_part(), you use split() and element_at() functions of AWS Athena combined together, you would get the desired result.
ELEMENT_AT(SPLIT(city, ','),-1) AS country
For further reference on element_at() function follow this doc.
For further reference on split() function follow this doc.

Postgres SQL regexp_replace replace all number

I need some help with the next. I have a field text in SQL, this record a list of times sepparates with '|'. For example
'14613|15474|3832|148|5236|5348|1055|524' Each value is a time in milliseconds. This field could any length, for example is perfect correct '3215|2654' or '4565' (only 1 value). I need get this field and replace all number with -1000 value.
So '14613|15474|3832|148|5236|5348|1055|524' will be '-1000|-1000|-1000|-1000|-1000|-1000|-1000|-1000'
Or '3215|2654' => '-1000|-1000' Or '4565' => '-1000'.
I try use regexp_replace(times_field,'[[:digit:]]','-1000','g') but it replace each digit, not the complete number, so in this example:
'3215|2654' than must be '-1000|-1000', i get:
'-1000-1000-1000-1000|-1000-1000-1000-1000', I try with other combinations and more options of regexp but i'm done.
Please need your help, thanks!!!.
We can try using REGEXP_REPLACE here:
UPDATE yourTable
SET times_field = REGEXP_REPLACE(times_field, '\y[0-9]+\y', '-1000', 'g');
If instead you don't really want to alter your data but rather just view your data this way, then use a select:
SELECT
times_field,
REGEXP_REPLACE(times_field, '\y[0-9]+\y', '-1000', 'g') AS times_field_replace
FROM yourTable;
Note that in either case we pass g as the fourtb parameter to REGEXP_REPLACE to do a global replacement of all pipe separated numbers.
[[:digit:]] - matches a digit [0-9]
+ Quantifier - matches between one and unlimited times, as many times as possible
your regexp must look like
regexp_replace(times_field,'[[:digit:]]+','-1000','g')

BigQuery LTRIM doesn't return desired result

I've got the following SQL code:
SELECT LTRIM("0039040123456","0039")
The result should be 040123456 but BigQuery returns 40123456.
Why is the 0 trimmed as well?
Bug or intended behavior?
Many thanks!
Try this:
SELECT LTRIM("0039p40123456","p039")
40123456
It removed the p too!
That's because:
If value2 contains more than one character or byte, the function removes all leading or trailing characters or bytes contained in value2.
https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators?hl=en#trim
(so it looks at the list of characters, not the sequence of them)
What you really want is:
SELECT REGEXP_REPLACE("0039040123456","^0039","")
The column type is STRING. After looking at the docs https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators?hl=de#ltrim I guess this behavior is intended. (Have a look at the fruits example.)

Select query that displays Joined words separately, not using a function

I require a select query that adds a space to the data based on the placement of the capital letters i.e. 'HelpMe' using this query would be displayed as 'Help Me' . Note i cannot use a stored function to do this the it must be done in the query itself. The Data is of variable length and query must be in SQL. Any Help will be appreciated.
Thanks
You need to use user defined function for this until MS give us support for regular expressions. Solution would be something like:
SELECT col1, dbo.RegExReplace(col1, '([A-Z])',' \1') FROM Table
Aldo this would produce leading space that you can remove with TRIM.
Replace regular expresion function:
http://connect.microsoft.com/SQLServer/feedback/details/378520
About dbo.RegexReplace you can read at:
TSQL Replace all non a-z/A-Z characters with an empty string
Assume if you are using Oracle RDBMS, you use the following,
REGEX_REPLACE
SELECT REGEXP_REPLACE('ILikeToWatchCSIMiami',
'([A-Z.])', ' \1')
AS RX_REPLACE
FROM dual
;
Managed to get this output: * SQLFIDDLE
But as you see it doesn't treat well on words such as CSI though.