sql query to remove prefix from field - sql

Given the following
Products_Joined.ProductName AS [stripHTML-TITLE],
How would I remove the first 5 characters when the field contain any of the following. Basically I want to strip out the characters between < and > and including <> as well. The returned field's length can vary.
<!01>AMSSSS
<!02>SSS
<!03>CMSS
<!04>DMSS
<!05>EMSDDDDD etc...
This gives me just the first characters after the > but I don't know how to get all characters after >
SUBSTRING(Products_Joined.ProductName, 6,1) AS [stripHTML-TITLE],
Was going to use Replace function for all the possible prefixes but that can get rather messy.

SUBSTRING(Products_Joined.ProductName, 6,LEN(Products_Joined.ProductName))

You can use STUFF.
select stuff('<!01>AMSSSS', 1, 5, '')
http://msdn.microsoft.com/en-us/library/ms188043.aspx

Related

How to extract first 5 characters from filename in LogicApps

I have an issue, could you help me to understand how to extract first 5 characters from the filename in LogicApps. I'm able to create a variable and append it, so I have the list with file names, but then struggling to extract specific characters...
You can also use the take expression:
take(variables('my_string'),5)
This also work for arrays if you want to return the 5 first array items.
Use the substring expression ...
https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#substring
substring('<text>', <startIndex>, <length>)
The expression within the First 5 Characters variable for the below result looks like this ...
substring(variables('String Variable'), 0, 5)
Result

Sqlite - How to remove substring in a field using wildcards

I would like to remove all occurrences of a certain string pattern across all fields in a table.
For example, find all occurrences of the pattern "<XY>*<Xy>" where * represents any possible configuration of characters. I want to remove just those substrings and leave the remainder of the string intact.
This is an example of what I would like to use as my SQL command, but of course this doesn't work:
UPDATE Table SET Field = replace(Field, '<XY>*<Xy>', '');
What is the solution?
Here is an option which attempts to splice around the <XY>...</XY> tags:
UPDATE yourTable
SET Field = SUBSTR(Field, 1, INSTR(Field, '<XY>') - 1) ||
SUBSTR(Field, INSTR(Field, '</XY>') + 5)
WHERE Field LIKE '%<XY>%</XY>%'
It updates fields containing this pattern with the concatenation of everything coming before the first <XY> and everything coming after the second </XY>.
Note that I used <XY>...</XY> rather than what you had originally, because INSTR() is not case sensitive, and both tags would appear as being the same thing.
Demo
The demo is for MySQL but the sytnax is almost identical to SQLite.

how to get regexp_substr for a string

In my table for the rows containing values like
sample>test Y10,
Sample> y21
I want to get a substring like y10,y21 from all rows. May I pls know how to get it. I tried with regexp_substr,Instr but not able to find the solution.
I am supposing that your string from column is devided by a single space .
It will give you last occurances which will be splited by ' ' a space
substr(your_string, 1, instr(yourString,' ') - 1)
OR you can achive this using regexp_substr
regexp_substr(your_String, '[^[:space:]]+', 1, -1 )
Assuming that yxx is always preceded by a space, it should be as easy as doing this:
TRIM(REGEXP_SUBSTR(mycolumn, ' y\d+', 1, 1, 'i'))
The above regular expression will grab y (note that it is case-insensitive, so it will grab Y as well) followed by an indefinite number (one or more) of digits. If you want to grab just two digits, replace \d+ with \d{2}.
Also, please note that it will get the first occurrence only. Getting multiple occurrences is a bit more complicated, but it can still be done.

Matching exactly 2 characters in string - SQL

How can i query a column with Names of people to get only the names those contain exactly 2 “a” ?
I am familiar with % symbol that's used with LIKE but that finds all names even with 1 a , when i write %a , but i need to find only those have exactly 2 characters.
Please explain - Thanks in advance
Table Name: "People"
Column Names: "Names, Age, Gender"
Assuming you're asking for two a characters search for a string with two a's but not with three.
select *
from people
where names like '%a%a%'
and name not like '%a%a%a%'
Use '_a'. '_' is a single character wildcard where '%' matches 0 or more characters.
If you need more advanced matches, use regular expressions, using REGEXP_LIKE. See Using Regular Expressions With Oracle Database.
And of course you can use other tricks as well. For instance, you can compare the length of the string with the length of the same string but with 'a's removed from it. If the difference is 2 then the string contained two 'a's. But as you can see things get ugly real soon, since length returns 'null' when a string is empty, so you have to make an exception for that, if you want to check for names that are exactly 'aa'.
select * from People
where
length(Names) - 2 = nvl(length(replace(Names, 'a', '')), 0)
Another solution is to replace everything that is not an a with nothing and check if the resulting String is exactly two characters long:
select names
from people
where length(regexp_replace(names, '[^a]', '')) = 2;
This can also be extended to deal with uppercase As:
select names
from people
where length(regexp_replace(names, '[^aA]', '')) = 2;
SQLFiddle example: http://sqlfiddle.com/#!4/09bc6
select * from People where names like '__'; also ll work

Regular expression to validate whether the data contains numeric ( or empty is also valid)

i have to validate the data contains numeric or not and if it is not numeric return 0 and if it is numeric or empty return 1.
Below is my query i tried in SQL.
SELECT dbo.Regex('^[0-9]','123') -- This is returning 1.
SELECT dbo.Regex('^[0-9]','') -- this is not returning 1 but i want to return as 1 and i try to put space in "pattern" also it is not working...
please can any one help....
Thanks in advance
Try:
SELECT dbo.Regex('^[0-9]*$','123')
or better yet:
SELECT dbo.Regex('^\d*$','123')
\d is standard shorthand for [0-9]. Note that when you use [0-9] it means "match exactly one digit. The * wildcard means match zero or more so \d* means match zero or more digits, which includes an empty result.
Your Regex is not quite right: "^[0-9]" checks only, if the first character in the string is a number. Your regex would return 1 on "3abcde".
Use
"^\d*$"
to check if the field contains only numbers (or nothing). If you want to allow whitespace, use
"^\s*\d*\s*$"
If you want to allow signs and decimals, try
"^\s*(+|-)?\d*\.?\d*\s*$"
Try ^[0-9]*$, which will match 0 or more digits only:
SELECT dbo.Regex('^[0-9]*$','')
should return 1, as should...
SELECT dbo.Regex('^[0-9]*$','123')