Pulling variable length substring from middle of string - tsql

I am trying to grab variable length string from a primary string.
Example:
ABC*12*1*name name****XX*123456789~
ABC*12*1*diffname diffname****XX*234567890~
ABC*12*1*diffname2 diffname2***XX*345678901~
I need to pull out the 'name name', 'diffname diffname', 'diffname2 diffname2'
etc from the string. And then replace the ' ' between the names with an asterisk - but, I cant just insert in the first space in the string, there could be multiple names, and so I would want to insert the '*' into the second, or third space, depending on the length of the name string.
SELECT
CHARINDEX('*1*',data)+3 AS startpos,
CHARINDEX('***',data) AS Endpos,
data
from #t
where data like '%ABC*12*1*%'
This gives me a start point and end point for the variable length string. So I try:
SELECT SUBSTRING(data,CHARINDEX('*1*',data)+3,CHARINDEX('***',data) -CHARINDEX('*1*',data)+3)
FROM #t
WHERE data like '%ABC*12*1*name%'
But this gives me
name n name aa*****X
as a result set, basically starting at the start point and then running well past the end point.
What am I doing wrong?

This part is the problem :
SELECT .....-CHARINDEX('*1*',data)+3
FROM .....
WHERE .....
You want to substract with Endpos so it supposed to be written in brackets like so :
-(CHARINDEX('*1*',data)+3)
and if the brackets are removed the last part should become -3 :
-CHARINDEX('*1*',data)-3

Related

How To Take Left Side String From A Particular position using PostgreSql

I have a table in that table there is a column called eq1.sources in that column, entries are like mentioned below. Now I would like to extract the string from the left side to till card slot number only.
Example:
fdn:realm:pam:network:55.150.40.841:shelf-1:cardSlot-1:card:daughterCardSlot-1:daughterCard
for this entry I need only
fdn:realm:pam:network:55.150.40.841:shelf-1:cardSlot-1
similarly
fdn:realm:sam:network:35.250.40.834:shelf-1:cardSlot-1:card
for this entry I need
fdn:realm:sam:network:35.250.40.834:shelf-1:cardSlot-1
I have tried substring(eq1.sources,0,position (':card:daughter' in eq1.sources)). this is working only for row numbers 1,2,4,5,6,7,9,10 but row number 3,8,11 not working as the entries not continued with ':card:daughter'.
The column name for the below entries is eq1.sources.
1.fdn:realm:pam:network:55.150.40.841:shelf-1:cardSlot-1:card:daughterCardSlot-1:daughterCard
2.fdn:realm:pam:network:35.250.40.824:shelf-1:cardSlot-1:card:daughterCardSlot-1:daughterCard
3.fdn:realm:sam:network:35.250.40.834:shelf-1:cardSlot-1:card
4.fdn:realm:pam:network:55.159.40.994:shelf-1:cardSlot-2:card:daughterCardSlot-1:daughterCard
5.fdn:realm:pam:network:35.250.140.104:shelf-1:cardSlot-2:card:daughterCardSlot-1:daughterCard
6.fdn:realm:pam:network:55.170.40.1:shelf-1:cardSlot-2:card:daughterCardSlot-1:daughterCard
7.fdn:realm:pam:network:35.450.40.24:shelf-1:cardSlot-3:card:daughterCardSlot-1:daughterCard
8.fdn:realm:sam:network:35.250.40.14:shelf-1:cardSlot-3:card
9.fdn:realm:pam:network:55.150.40.854:shelf-1:cardSlot-4:card:daughterCardSlot-1:daughterCard
10.fdn:realm:pam:network:35.250.40.84:shelf-1:cardSlot-5:card:daughterCardSlot-1:daughterCard
11.fdn:realm:sam:network:35.250.40.84:shelf-1:cardSlot-6:card
Expecting a PostgreSQL query to extract left side substring from a particular position in a row.
Expected output is
1.fdn:realm:sam:network:35.250.40.834:shelf-1:cardSlot-1
2.fdn:realm:sam:network:35.250.40.14:shelf-1:cardSlot-3:card
from
1.fdn:realm:sam:network:35.250.40.834:shelf-1:cardSlot-1:card:daughterCardSlot-1:daughterCard
2.fdn:realm:sam:network:35.250.40.14:shelf-1:cardSlot-3:card
First split the string into an array with : as a delimiter (this is the t subquery) and then pick the first 7 array elements and join them again into a string with : delimiter.
select array_to_string(arr[1:7], ':') as sources
from
(
select string_to_array(sources, ':') as arr
from the_table
) as t;
See demo.

RIGHT Function in UPDATE Statement w/ Integer Field

I am attempting to run a simple UPDATE script on an integer field, whereby the trailing 2 numbers are "kept", and the leading numbers are removed. For example, "0440" would be updated as "40." I can get the desired data in a SELECT statement, such as
SELECT RIGHT(field_name::varchar, 2)
FROM table_name;
However, I run into an error when I try to use this same functionality in an UPDATE script, such as:
UPDATE schema_name.table_name
SET field_name = RIGHT(field_name::varchar, 2);
The error I receive reads:
column . . . is of type integer but expression is of type text . . .
HINT: You will need to rewrite or cast the expression
You're casting the integer to varchar but you're not casting the result back to integer.
UPDATE schema_name.table_name
SET field_name = RIGHT(field_name::TEXT, 2)::INTEGER;
The error is quite straight forward - right returns textual data, which you cannot assign to an integer column. You could, however, explicitly cast it back:
UPDATE schema_name.table_name
SET field_name = RIGHT(field_name::varchar, 2)::int;
1 is a digit (or a number - or a string), '123' is a number (or a string).
Your example 0440 does not make sense for an integer value, since leading (insignificant) 0 are not stored.
Strictly speaking data type integer is no good to store the "trailing 2 numbers" - meaning digits - since 00 and 0 both result in the same integer value 0. But I don't think that's what you meant.
For operating on the numeric value, don't use string functions (which requires casting back and forth. The modulo operator % does what you need, exactly: field_name%100. So:
UPDATE schema_name.table_name
SET field_name = field_name%100
WHERE field_name > 99; -- to avoid empty updates

how to get a substring from right with T-sql

Suppose I have a string like:
abc.efg.hijk.lmnop.leaf
I want the substring: abc.efg.hijk.lmnop.
Means: Find out the first comma . from right, then get the substring from left to this comma
How to use t-sql string function return the substring with one expresssion?
First your'll need to reverse the string and find the character index of the first period, then subtract this number from the length of the entire string. This value needs to be used at the length parameter of the sub-string function.
Try this:
DECLARE #S VARCHAR(55) = 'abc.efg.hijk.lmnop.leaf'
SELECT SUBSTRING(#S, 1, LEN(#S) - CHARINDEX('.', REVERSE(#S)))

tsql comma delimited testing for value

I've been given a table with a few fields that hold comma-separated values (either blank or Y/N) like so (and the field name where this data is stored is People_Notified):
Y,,N,
,Y,,N
,,N,Y
Each 'slot' relates to a particular field value and I need to now include that particular field name in the string as well (in this case Parent, Admin, Police and Medical) but inserting a "N" if the current value is blank but leaving the existing Y's and N's in place. So for the above example, where there are four known slots, I would want a tsql statement to end up with:
Parent=Y,Admin=N,Police=N,Medical=N
Parent=N,Admin=Y,Police=N,Medical=N
Parent=N,Admin=N,Police=N,Medical=Y
I tried to use a combination of CHARINDEX and CASE but haven't figured a way to make this work.
js
Although a bit messy, in theory can be done in one statement:
select
'Parent=' +stuff((stuff((stuff(
substring((replace(
(','+(replace((replace(#People_Notified,',,,',',N,N,')),',,',',N,'))+','),',,',',N,')),2,7),7,0,
'Medical=')),5,0,'Police=')),3,0,'Admin=')
broken down is easier to follow:
declare #People_Notified varchar(100)=',,Y,Y' -- test variable
-- Insert Ns
set #People_Notified= (select replace(#People_Notified,',,,',',N,N,')) -- case two consecutive missing
set #People_Notified= (select replace(#People_Notified,',,',',N,')) -- case one missing
set #People_Notified= (select replace((','+#People_Notified+','),',,',',N,')) -- case start or end missing
set #People_Notified= substring(#People_Notified,2,7) -- remove extra commas added previously
-- Stuff the labels
select 'Parent=' +stuff((stuff((stuff(#People_Notified,7,0,'Medical=')),5,0,'Police=')),3,0,'Admin=')
If you're able to use XQuery in SQL Server, I don't think you need to get too complex. You could do something like this:
SELECT CONVERT(XML, REPLACE('<pn>' + REPLACE(People_Notified, ',', '</pn><pn>') + '</pn>', '<pn></pn>', '<pn>N</pn>')).query('
concat("Parent=", data(/pn[1])[1], ",Admin=", data(/pn[2])[1], ",Police=", data(/pn[3])[1], ",Medical=", data(/pn[4])[1])
')
FROM ...
Explanation: Construct an XML-like string out of the original delimited string by replacing commas with closing and opening tags. Add an opening tag to the start and a closing tag to the end. Replace each empty element with one containing "N". Convert the XML-like string into actual XML data so that you can use XQuery. Then just concatenate what you need using concat() and the right indexes for the elements' data.
Here's one way to do it:
;WITH cteXML (Id, Notified)
AS
(
SELECT Id,
CONVERT(XML,'<Notified><YN>'
+ REPLACE([notified],',', '</YN><YN>')
+ '</YN></Notified>') AS Notified
FROM People_Notified
)
select id,
'Parent=' + case Notified.value('/Notified[1]/YN[1]','varchar(1)') when '' then 'N' else Notified.value('/Notified[1]/YN[1]','varchar(1)') end + ',' +
'Admin=' + case Notified.value('/Notified[1]/YN[2]','varchar(1)') when '' then 'N' else Notified.value('/Notified[1]/YN[2]','varchar(1)') end + ',' +
'Police=' + case Notified.value('/Notified[1]/YN[3]','varchar(1)') when '' then 'N' else Notified.value('/Notified[1]/YN[3]','varchar(1)') end + ',' +
'Medical=' + case Notified.value('/Notified[1]/YN[4]','varchar(1)') when '' then 'N' else Notified.value('/Notified[1]/YN[4]','varchar(1)') end Notified
from cteXML
SQL Fiddle
Check this page out for an explanation of what the XML stuff is doing.
This page has a pretty thorough look at the various ways you can split a delimited string into rows.

Test for numeric value?

The vendor data we load in our staging table is rather dirty. One column in particular captures number data but 40% of the time has garbage characters or random strings.
I have to create a report that filters out value ranges in that column. So, I tried playing with a combination of replace/translate like so
select replace(translate(upper(str),' ','all possible char'),' ','')
from table
but it fails whenever it encounters a char I did not code. Therefore, the report can never be automated.
Javascript has the isNaN() function to determine whether a value is an illegal number (True if it is and false if not).
How can I do the same thing with DB2?? Do you have any idea?
Thanks in advance.
A fairly reliable (but somewhat hackish) way is to compare the string to its upper- and lower-case self (numbers don't have different cases). As long as your data that is bringing in characters only includes Latin characters, you should be fine:
SELECT input, CASE
WHEN UPPER(input) = LOWER(input) THEN TO_NUMBER(input)
ELSE 0
END AS output
FROM source
Another option would be to use the TRANSLATE function:
SELECT input,
CASE
WHEN TRANSLATE(CAST(input as CHAR(10)), '~~~~~~~~~~~~~', '0123456789-. ') = '~~~~~~~~~~' THEN CAST(input AS DECIMAL(12, 2))
ELSE 0
END AS num
FROM x
WITH x (stringval) AS
(
VALUES ('x2'),(''),('2.2.'),('5-'),('-5-'),('--5'),('.5'),('2 2'),('0.5-'),(' 1 '),('2 '),('3.'),('-4.0')
)
SELECT stringval,
CASE WHEN (
-- Whitespace must not appear in the middle of a number
-- (but trailing and/or leading whitespace is permitted)
RTRIM(LTRIM( stringval )) NOT LIKE '% %'
-- A number cannot start with a decimal point
AND LTRIM( stringval ) NOT LIKE '.%'
-- A negative decimal number must contain at least one digit between
-- the negative sign and the decimal point
AND LTRIM( stringval ) NOT LIKE '-.%'
-- The negative sign may only appear at the beginning of the number
AND LOCATE( '-', LTRIM(stringval)) IN ( 0, 1 )
-- A number must contain at least one digit
AND TRANSLATE( stringval, '0000000000', '123456789') LIKE '%0%'
-- Allow up to one negative sign, followed by up to one decimal point
AND REPLACE(
TRANSLATE( RTRIM(LTRIM(stringval)), '000000000', '123456789'),
'0', '') IN ('','-','.','-.')
)
THEN 'VALID'
ELSE 'INVALID'
END AS stringisvalidnumber
FROM x
;
Check this out:
SELECT Mobile,
TRANSLATE(Mobile, '~~~~~~~~~~', '0123456789') AS FirstPass,
TRANSLATE(TRANSLATE(Mobile, '~~~~~~~~~~', '0123456789'), '', '~') AS Erroneous,
REPLACE(TRANSLATE(Mobile, '', TRANSLATE(TRANSLATE(Mobile, '~~~~~~~~~~', '0123456789'), '', '~')), ' ', '') AS Corrected
FROM Person WHERE Mobile <> '' FETCH FIRST 100 ROWS ONLY
The table is "Person" and the field that you want to check is "Mobile".
If you work a little bit more on this, you can build an UPDATE to fix the entire table