extract characters using delimiters - sql

I am having string called '200_CFL_2010'. I wish to get the characters between _'s. I want the output as 'CFL'. I have to achieve it through SQL.

I think this may be dependent on your Database (MySQL, Oracle, whatever..)
Do a search for "string functions" and your database name. For e.g MySQL, you can find it here: http://dev.mysql.com/doc/refman/5.1/en/string-functions.html.
The function INSTR and SUBSTR are the ones you are looking for. Use then like
SELECT SUBSTR(field, INSTR(field, "_", 1), INSTR(field, "_", 2) - INSTR(field, "_", 1)) FROM ...
Note: INSTR does only have two parameters in MySQL... youd need to cascade SUBSTRs a little there.

select substring(col, 0, LEN(col) - charindex('_', col)) as col
from
(
select substring(col, charindex('_', col) + 1, LEN(col)) as col
from
(
select '200_CFL_2010' as col
) as subq
) as subq

Related

How can I use SQL Substring to extract characters from this filename?

I'm attempting to use the SUBSTRING function to extract a name out of a filename.
An example filename would be: "73186_RHIMagnesita_PHI_StopLoss_TruncSSN_NonRedact_Inc_to_Apr2022_Paid_to_Apr2022_EDIT"
I'm attempting to extract the "RHIMagnesita" from this filename.
The substring I used was:
SUBSTRING(DFH.FileName, CHARINDEX('_', DFH.FileName) + 1, CHARINDEX('_PHI', DFH.FileName) - 1)
The results it gave were: "RHIMagnesita_PHI_S"
How do I extract only "RHIMagnesita" using the Substring function?
The third parameter in SUBSTRING is length not position, so you would need to substract the length of the beginning string.
SUBSTRING(DFH.FileName, CHARINDEX('_', DFH.FileName) + 1, CHARINDEX('_PHI', DFH.FileName) - CHARINDEX('_', DFH.FileName))
You might need to add or substract 1, but that's the idea.
You were close. You need to use CHARINDEX to also find the position of the second underscore.
SELECT SUBSTRING(FileName,
CHARINDEX('_', FileName) + 1,
CHARINDEX('_', FileName, CHARINDEX('_', FileName) + 1) -
CHARINDEX('_', FileName) - 1) AS FilePart
FROM yourTable;
Here's a way using STRING_SPLIT and FETCH, rather than SUBSTRING We split the string and only return the second row
SELECT
value
FROM STRING_SPLIT('73186_RHIMagnesita_PHI_StopLoss_TruncSSN_NonRedact_Inc_to_Apr2022_Paid_to_Apr2022_EDIT','_')
ORDER BY (SELECT NULL)
OFFSET 1 ROWS
FETCH NEXT 1 ROWS ONLY;
Note: On Azure Sql Server STRING_SPLIT has an ordinal parameter, so you could write this
SELECT
value
FROM
STRING_SPLIT('73186_RHIMagnesita_PHI_StopLoss_TruncSSN_NonRedact_Inc_to_Apr2022_Paid_to_Apr2022_EDIT','_', 1)
WHERE ordinal = 2

equals letter start and end SQL

how to select a cells with contents (varchar) starting and ending with equals letter in SQL??
You can use string functions. Assuming a table called mytable with a column called col:
select *
from mytable
where left(col, 1) = right(col, 1)
Note that this also allows values that are made of just one character - you did not tell which behavior you want in that case. You can easily avoid that, if that's what you want, with another condition on the length of the string:
select *
from mytable
where left(col, 1) = right(col, 1) and len(col) > 1

Get text after second occurrence of a specific character

I have a column with data like this:
firstNameLetter_surname_numericCode
For example:
m_johnson_1234
I need to extract the numeric code. I've tried with SUBSTRING function but I just get:
surname_numericCode
This is the code I've used:
SET number = substring(t2.code, charindex('_', t2.code, 2) + 1, len(t2.code))
How can I get just the numeric code?
Heres a one-liner for you
select right('m_johnson_1234', charindex('_', reverse('m_johnson_1234') + '_') - 1)
Call the CHARINDEX function twice:
SELECT SUBSTRING(
code,
NULLIF(CHARINDEX('_', code, NULLIF(CHARINDEX('_', code), 0) + 1), 0) + 1,
LEN(code)
)
FROM (VALUES
('a_b_c'),
('a_b')
) x(code)
One method is to look for the first _ in the reversed string:
select col,
stuff(col, 1, len(col) - charindex('_', reverse(col)) + 1, '') as numericCode
from (values ('firstNameLetter_surname_numericCode')) v(col);
If the numeric code is really a number -- and no other numbers start the preceding values -- then you can use patindex():
select col,
stuff(col, 1, patindex('%[_][0-9]%', col), '') as numericCode
from (values ('firstNameLetter_surname_0000')) v(col);
The SUBSTR and INSTR functions can be combined to get you the numeric code.
SELECT SUBSTR('m_johnson_1234', INSTR('m_johnson_1234', '_', 1, 2)+1) FROM TABLE;
For the start_pos argument use INSTR to start at the beginning of the string, and find the index of the second instance of the '_' character. Then use that to start one position after and read to the end of the string.
If you need the result to be numeric instead of still a string, then wrap the SUBSTR() in a TO_NUMBER() function.
Late answer, and just because I didn't see PARSENAME() mentioned.
Example
Select parsename(replace(t2.code,'_','.'),1)
From YourTable

Split string to array and order arr[1] ? - MS SQL

I want to order a value like 1234_1 (id, index) in correct way. Remove 123_ and then just order the index. How can i do it in right way in Microsoft SQL... Cheers
Try using Substring and charindex :
To order by it :
SELECT ...
FROM ...
ORDER BY SUBSTRING(YourColumn, CHARINDEX('_', YourColumn) + 1, LEN(YourColumn))
Presumably, you want to order by the index numerically (so _10 is after _9 rather than _1).
You can extract the value and convert to a number:
order by try_convert(int, stuff(col, 1, charindex('_', col), ''))

SQL Server Query Output with sql scenario replace values with 'x' [duplicate]

This question already has answers here:
Replacing certain character in email addresses with '*' in an SQL query
(3 answers)
Closed 6 years ago.
I have one col in my table like:
COL
Kiyara#ymail.com
Akira#zmail.com
I want my output something like this:
COL
Kxxxxx#ymail.com
Axxxx#zmail.com
iyara replaced with 5x's(xxxxx) and 'kira' with 4x's(xxxx)
There's probably a better way to do it that this, but this achieves what you're after:
DECLARE #email NVARCHAR(50) = 'Kiyara#ymail.com';
SELECT LEFT(#email, 1) + REPLICATE('X',
LEN(SUBSTRING(#email, 2,
CHARINDEX('#', #email) - 1)))
+ SUBSTRING(#email, CHARINDEX('#', #email), LEN(#email));
-- Result: KXXXXXX#ymail.com
This uses the REPLICATE() method, which according to the docs online starts with SQL Server 2008.
REPLICATE (Transact-SQL)
Repeats a string value a specified number of times.
REPLICATE ( string_expression ,integer_expression )
SELECT LEFT(col, 1) + RIGHT('xxxxxxxxxxxxxxxx', CHARINDEX('#', col) - 2) +
SUBSTRING(col, CHARINDEX('#', col), LEN(col) - CHARINDEX('#', col) + 1);
Explanation:
Take Kiyara#ymail.com as an example, and each piece of my query is shown here.
K LEFT(col, 1)
xxxxx RIGHT('xxxxxxxxxxxxxxxx', CHARINDEX('#', col) - 2)
#ymail.com SUBSTRING(col, CHARINDEX('#', col), LEN(col) - CHARINDEX('#', col) + 1)
Note that you can replace the call to RIGHT() with a string of x's long enough to match your longest expected email name.
Try it's
select substring('Akira#zmail.com',1,1) + REPLICATE('x',charindex('#','Akira#zmail.com')) + SUBSTRING('Akira#zmail.com',charindex('#','Akira#zmail.com'),200)
Change the email by column or whatever you want
declare #n nvarchar(max)
set #n='Kiyara#ymail.com'
select concat(substring(#n,1,1),replicate('X',len( substring(#n,2,charindex('#',#n,1)-2))),SUBSTRING(#n,charindex('#',#n),200))
In this with the help o substring you have found first letter ans then rest of he letter til # i.e iyara is searched using charindex ans substring,which will be converted into 'x' using replicate function and then rest id printed.