PostgreSQL: How to extract text from a particular letter? - sql

I'm practicing exercises with SQL and I've got a problem I couldn't resolve yet.
I have a table with a column named: **'email' ** and I want to extract just the Domain of each mail. Then I was thinking to extract since '#' to get that information.
But idk how to do it, was trying with SUBSTRING, but that didn't work because that's about position, and each mail has different size.
I attach a screenshot about the table's composition (does not contain real information). Thank u so much :)
I tried with SUBSTRING method but that didn't work
Example email: example_email#outlook.com
Output expected: #outlook.com

We can use SPLIT_PART to fetch everything after the # and then append the #:
SELECT CONCAT('#',SPLIT_PART(email, '#', 2)) AS mailDomain
FROM people_practice;
Here the documentation about this and other useful string functions.

Related

Replace value in multiple strings without charindex

so This is a sample of my DB Values
UPDATE page p SET p.content = REPLACE(p.content, SUBSTRING(p.content, CHARINDEX('php?id=', p.content) +1), CONCAT('php?id=',p.course)) WHERE p.content LIKE '%php?id=%' AND p.content NOT LIKE CONCAT('%','php?id=',p.course,'%');
I'm Trying to only get the text after php?id= which is a number and change it to the correct number which is the course column
so for example the first value should be changed to
4|Enjoy your training!<a href="*/view.php?id=4">
Unfortunately charindex keeps giving me an Error
Any Help on how to Make this thing work without charindex as i Believe our server does not support this function
UPDATE: Charindex didnt exist in mysql so i had to use LOCATE fuction instead. Also i wasnt using the root password for the whole server, just the root password for the db
Thanks for the heads up!
Now just need the query to correctly update... as it removes just everything after <a href etc etc and doesnt replace

Big Query - extract json field which contains emoji?

In my BQ database table I have a column called payload which contains raw facebook webhooks JSON payloads as string. One of them contains a text with an emoji like Sample 🏦. In big query it look like
{"object":"page","entry":[{"id":"xxxx","time":1602757469275,"messaging":[{"sender":{"id":"xxxx"},"recipient":{"id":"xxxx"},"timestamp":1602757469062,"message":{"mid":"m_xxxx","text":"Sample \ud83c\udfe6","quick_reply":{"payload":"{\"key\": \"value\"}"},"tags":{"source":"source"}}}]}]}
I would like to create a view with a column text with extracted text field value from the raw json. I created an sql like
SELECT
JSON_EXTRACT_SCALAR(payload, '$.entry[0].messaging[0].message.text') as text,
FROM `my_table.facebook.webhook_received`
Sadly the result I get looks like that Sample ��
Does anyone know how to make big query decode the emoji properly or at least just not change it to those � signs ?
Those characters you have embedded are not for a bank icon which is your issue I believe.
Run the following in BQ and it returns the desired emoji:
select " Sample \U0001f3e6"
Ref:https://emojipedia.org/bank/
The two you have provided seem to default to the '?', invalid character
http://unicode.scarfboy.com/?s=U%2Bdfe6
edit: what ever is handling the message maybe throwing the encodings you're seeing in your message which may be the actual problem.
If you are using BigQuery Python client and its load_table_from_json method, there is a Unicode bug (especially its byte is over 0xFFFF, like 🏦) in the previous version, and I have submitted this bug fix which is already included in the latest release include it, https://github.com/googleapis/python-bigquery/releases/tag/v2.24.0. By the way, you should use \U0001F3E6, not \ud83c\udfe6 (UTF-16 hex type) to present 🏦 in your Python code with BigQuery.
Unicode Character 'BANK': https://www.fileformat.info/info/unicode/char/1f3e6/index.htm,
https://charbase.com/1f3e6-unicode-bank

#Dblookup and formatting on web

I have been developing a web application using domino, therein I have dblookup-ing the field from notes client; Now, this is working fine but the format of value is missing while using on web.
For example in lotus notes client the field value format is as above
I am one, I am two, I am one , I am two, labbblallalalalalalalalalalalalalalalalalalaallllal
Labbbaalalalallalalalalalaalallaal
Hello there, labblalalallalalalllaalalalalalalalalalalalalalalalalalalalalalalala
Now when I retrieve the value of the field on web it seems it takes 2 immediate after 1. and so forth, I was expecting line feed here which is not happening.
The field above is multi valued field. Also on web I have used computed text which does db lookup from notes client.
Please help me what else could/alternate solution for this case.
Thanks
HD
Your multi-valued field has display options associated with it and the Notes client honors those. Obviously, your options are set up to display entries separated by newlines.
The computed text that you are using for the web does not have options like that and the field options are irrelevant because you aren't displaying the field. Your code has to insert the #Newlines. That's pretty easy because #DbLookup returns a list, and if you concatenate a list and a scalar, the scalar will be appended to each element of the list. (Look at the third example under "concatenation, pairwise" here to see what I mean.
The way you've worded your question is a little unclear to me, but what you need in your computed text formula is either something like this:
list := #DbLookup(etc,. etc.);
list + #Newline;
Or something like this:
multiValueFieldContainingListWithDbLookupResult + #NewLine;
I used #implode(Dblookupreturnedvalue;"");
thanks All :)

Trim string up to certain character in sql (oracle)

Looking for some help here if anyone can offer some. I am working with an oracle database and I would like to trim a string up until a certain character '/'. These fields are paths of a URL and they are all different sizes so I need to make sure it's getting to the very last '/' in the URL and removing everything up until that point. Additionally, there is a session ID that is associated with some of these URLs that is located at the end of the string and has a semi-colon before it starts, so I would want to remove everything that contains a semi-colon up to the semi_colon and on. So essentially I want to remove content from the beginning of the URL and from the end of the URL if applicable. Examples of these URL's (string) are as follows:
Current URLS
/ingaccess/jsp/mslogon.jsp
/ingaccess/help/helpie_term_cash_surrender_value.html
/eportal/logout.do;jsessionid=xr8co1kyebrve47xsjwmzw--.p704
/eportal/logout.do;jsessionid=gdh_e_e1m1hna0z9ednklg--.p705
/ingaccess/help/helpie_term_northern_unit_value.html
/ingaccess/help/helpie_scheduled_rebalance.html
/eportal/home.action;jsessionid=9vhfbkhunkvtcm5g1dtgsa--.p704
/ingaccess/help/helpie_catch_up_50.html
/ingaccess/piechartmaker
/ingaccess/help/helpie_term_fund_balance.html
Desired URLS
mslogon.jsp
helpie_term_cash_surrender_value.html
logout.do
logout.do
helpie_term_northern_unit_value.html
helpie_scheduled_rebalance.html
home.action
helpie_catch_up_50.html
piechartmaker
helpie_term_fund_balance.html
Anyone know of an easy fix here? I've tried working with SUBSTR and REPLACE a bit but can't seem to get them to work.
Thanks a bunch in advance,
Ryan
Try this
SELECT CASE WHEN INSTR (surname,';')>0 THEN SUBSTR(surname,1
,INSTR(surname,';',1,1)-1) ELSE surname END FROM
(SELECT SUBSTR(column,INSTR(column,'/',-1)+1) AS surname
FROM tableName)
Tested on Apex

SQL Server find and replace in TEXT field

I have a database in SQL Server 2005 that was brought up from SQL Server 2000 and is still using TEXT type fields instead of varchar(max).
I need to find and replace a string of characters in the text field but all of the examples of how to do this that I have found don't seem like they would work for me. It seems the UPDATETEXT command requires that the two parameters "insert_offset" and "delete_length" be set explicitly but the string i am searching for could show up in the text at any point or even at several points in the same cell. My understanding of these two parameters is that the string im searching for will always be in the same place, so that insert_offset is the number of spaces into the text that the UPDATETEXT command will start replacing text.
Example: Need to find: <u> and Replace it with: <u>
Text field example:
*Everyone in the room was <b><u>tired</u>.</b><br>Then they woke <b><u>up</u>.
Can anyone help me out with this? THANKS!
I finally figured it out. It was buried in the comments to the article jfrobishow published. Thank you SO much.
Here is the whole response that led me to the solution:
quote:Originally posted by fredclown
If you use SQL 2005 you can use replace with a text type. All you have
to do is the below ...
field = replace(cast(field as varchar(max)),'string' ,'replacement')
Easy as pie.
Two thumbs up to Fredclown!!! command
work like a charm for me as well. This
is what I wrote my Update statement to
Find and Replace in a Text field in
SQL server 2005 database
UPDATE TableName SET DBTextField = REPLACE(CAST(DBTextField AS varchar(MAX))
,'SearchText', 'ReplaceText')
FROM TableName
WHERE CHARINDEX('SearchText',CAST(DBTextField as varchar(MAX)))>0
Note:that this may truncate the size of you dbfield , but if is a long text column make it nvarchar(max) and you should not get none truncation!