string replace sql select - sql

Hi Ive tried to find an answer to this but cant find one.
Id like to remove some characters and prepend a pound sign to the result of an SQL query which looks as follows (its already using a replace command can I stack these)?:
select fundraiser.Company_Name,
replace(Just_Giving_Campaign,'"label":',''),
sum(fundraising_campaigns.Total_Collected) as donations
from fundraising_campaigns,
fundraiser
where Charity_Name = 'WaterAid'
and fundraising_campaigns.Campaigners_ID = fundraiser.id
group by fundraiser.Company_Name
Can anyone confirm how I would go about adding (£ sign) and remove several sets of characters from a select statement.Certainly dont appear to be able to stack replace statements (e.g.
replace(replace (string, what to match, what to replace it with), what to match, what to replace it with)
Appreciate any thoughts

I am not sure about your question. If I am correct you want to prepend £ and do some nested replace. Hope the below example helps.
select '£'+replace(replace('YourText','x','s'),'You','U')

Related

Regex match first number if it does not appear at the end

I am currently facing a Regex problem which apparently I cannot find an answer to.
My Regex is embedded in a teradata SQL of the form:
REGEXP_SUBSTR(column, 'regex_pattern')
I want to find the first appearance of any number except if it appears at the end of the string.
For Example:
"YEL2X30" -> "2"
"YEL19XYZ05" -> "19"
"YELLOW05" -> ""
I tried it with '[0-9]+(?!$)/' but this returns me a blank String always.
Thanks in Advance!
Shot in the dark here since I'm unfamiliar with teradata and the supported SQL-functionality. However, reading the docs on the REGEXP_SUBSTR() function it seems like you may want to use the 3rd and 4th possible argument along with a slightly different regular expression:
[0-9]+(?![0-9]|$)
Meaning: 1+ Digits that are not followed by either the end of the string or another digit.
I'd believe the following syntax may work now to retrieve the 1st appearance of any number from the matching results:
REGEXP_SUBSTR(column, '[0-9]+(?![0-9]|$)', 1, 1)
The 3rd parameter states from which position in the source-string we need to start searching whereas the 4th will return the 1st match from any possible multiple matches (is how I read the docs). For example: abc123def456ghi789 whould return 123.
Fiddling around in online IDE's gave me that:
CREATE TABLE TBL (TST varchar(100));
INSERT INTO TBL values ('YEL2X30'), ('YEL19XYZ05'), ('YELLOW05'), ('abc123def456ghi789');
SELECT REGEXP_SUBSTR(TST, '[0-9]+(?![0-9]|$)', 1, 1) as 'RESULTS' FROM TBL;
Resulted in:
RESULTS
2
19
NULL
123
NOTE: I also noticed that leaving out the 3rd and 4th parameter made no difference since they will default back to 1 without explicitly mentioning them. I tested this over here.
Possibly the simplest way is to look for digits followed by a non-digit. Then keep all the digits:
regexp_substr(regexp_substr(column, '[0-9]+[^0-9]'), '[0-9]+')

Regex matching everything except specific words

I have looked through the other questions asked on excluding regex, but I was unable to find the answer to my question.
I have the SQL statement
select --(* vendor(microsoft), product(odbc) guid'12345678-1234-1234-1234-123456789012' *)-- from TAB
With regex, I want to find every single character in that string, except
--(* vendor(microsoft), product(odbc)
and
*)--
The vendor and product names (microsoft and odbc) could be anything as well, I still want to exclude it.
I don't care what kind of characters there are, or if the SQL statement is even syntactically correct. The string could look like this, and I still want to find everything, including whitespaces, excluding what I mentioned above:
{Jane Doe?= --(* vendor(micro1macro2?), product(cdb!o) 123$% --(**) *)-- = ?
So far, I have this expression:
(--\(\* vendor\(.*\), product\(.*?\))|(\*\)--)
Which seems to work in finding what I want to exclude https://regex101.com/r/rMbYHz/204. However, I'm unable to negate it.
Does replace() do what you want?
select replace(replace(t.col, '--(* vendor(microsoft), product(odbc)', ''
), '*)--', ''
)

SQL Server : extracting the Midddle Characters without CHARINDEX

To start, I have seen the CHARINDEX results on here but none of them seem to be working for my case. The reasons are either a, CHARINDEX can't help me, or b, I am not understanding how CHARINDEX works. That being said, I would like to ask my question here in hopes that I can get some clarification on both how to solve my issue and CHARINDEX if that so happens to be the way this question is answered.
The variable that I am trying to extract from has varying length. However, two things are always constant.
There is always a '/' as the 16th character in the string
The last character in the string is always '0' OR '1'
What I am trying to do is extract the name from between '/' and '0' or '1'. In short, I want to chop off the first 16 characters and the last character of every string. So far, this is what I have:
SELECT
SUBSTRING([string_name], 17, LEN([string_name]) - 1) AS 'username'
FROM
[table_name]
The results I get still contain the 0 OR 1 at the end. What I need to do is somehow remove that 0 from the string. It is important to note that the number of characters between '/' and '0' are always different.
Current results:
gordon0
grant0
greg0
guy1
hanying0
Desired results:
gordon
grant
greg
guy
hanying
Any advice here would be wonderful.
Please let me know if you need any additional information from me. If possible, would like to maintain using either SUBSTRING, LEFT or RIGHT.
Thanks
Adjusting the length would seem to address your problem:
SELECT SUBSTRING([string_name], 17, LEN([string_name])-17) AS username
FROM [table_name]

SQL remove spaces between specific character in a string?

I want to update a database table field using another field in the same table. Currently I have this table called sources.
Name Code
In the name column I have values like this example :
' Deals On Wheels '
'Homesru - Abu Dhabi - Madinat Zayed Gold Centre'
And I am having this update statement :
UPDATE Sources
SET Code = REPLACE((LTRIM(RTRIM(Name))),' ','-')
the result is :
Deals-On-Wheels-Al-Aweer
which is fine.
but for second one I have this :
Homesru---Abu-Dhabi---Madinat-Zayed-Gold-Centre
I want it to be like this :
Homesru-Abu-Dhabi-Madinat-Zayed-Gold-Centre
How can I Achieve this ? Any Help is appreciated.
As suggested by #DanielE. my answer will point to a more global solution, in case you ever need to replace duplicated/triplicated/quadriplicated/... occurrences of a character on a string.
I'll not create a full solution for this issue, is a recurring question and there are really good solutions around already. Check these links:
SQL Server Central: remove spaces between specific character in a string?. This forum post will point to the next link I'm posting here. But is good to know what they are asking and answering.
Replace multiple spaces with new one but you can slightly modify it to replace any character you want.
You can also rely on this answer Find and remove repeated strings from Aaron Bertrand.
try
REPLACE((LTRIM(RTRIM(REPLACE((LTRIM(RTRIM(Name))),' - ','-')))),' ','-')
this will first replace ' - ' with just '-'
You might want to look into using a UDF to do a regular expression search and replace. See https://launchpad.net/mysql-udf-regexp

Search for string with conditions

I have table with a name filed which is string. I need to create a sql statement that searches for children to a node without finding the children to the children. Is it possible to use LIKE and some wildcards to accomplish this? You can see some examples below of the results I need to get based on my search string.
Search string is /home
Then the follwing entries should be returned
/home/something
/home/somethingElse
but not
/home/something/foo
/home/something/bar
/home/somethingElse/foo
but if the search string is /home/something
These should be returned
/home/something/foo
/home/something/bar
SELECT name FROM table
WHERE name LIKE '/home/%' AND name NOT LIKE '/home/%/%'
should filter out anything with second level node under it.
I would probably search on the number of slashes in addition to the actual keywords. So the first one would be searching for /home with 1-2 /'s
The second one would be /home/something wtih 2-3 slashes.
I don't have sql up infront of me, but I'll work on some sample code for you.
Edit:
CREATE FUNCTION [dbo].[ufn_CountChar] ( #pInput VARCHAR(1000), #pSearchChar CHAR(1) )
RETURNS INT
BEGIN
RETURN (LEN(#pInput) - LEN(REPLACE(#pInput, #pSearchChar, '')))
END
GO
This little function will act nicely to count the number of slashes in your strings.
Enjoy
Hope this helps,
Cheers,