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,
Related
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]+')
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)', ''
), '*)--', ''
)
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
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')
I want to replace always the last node in the string-
root/node1/node2
If I pass node3 as the parameter it should do a replace like this -
root/node1/node3
Can anyone help me do this say the column name was lineage and I have the id. So, the query would be -
Update
tree
set
lineage= -- replace(lineage,node3) -- this is what i need ?
where
id=2
You could do some string manipulation to find the last occurrence of a /, and then strip everything after that point... and then append your new node parameter to that value
Update
tree
set
lineage = LEFT(Lineage, LEN(Lineage) - CHARINDEX('/', REVERSE(Lineage)) + 1) + #NewNode
where
id=2
User Defined Function. You have the option of T-SQL or .NET languages. I, myself, would chose .NET, as it give the ability to use Regex to more efficiently hone in on the last part of the string, but you can split and reassemble a string using T-SQL. This shows splitting, for example:
http://www.logiclabz.com/sql-server/split-function-in-sql-server-to-break-comma-separated-strings-into-table.aspx