Extract a string without a standard start, end position - sql

I am trying to extract data from a string column. I need to get data similar to 'HFDD20200203300PM' from a string like : 'Atlas,WARM_HF,hotfix,web2020janHF,HFDD20200203300PM'
input2 : 'AEM_ARCH_REVIEW_DONE,Atlas,HFDD2020013000,hotfix,web2020janHF,WARM_HF' output2:'HFDD2020013000'
input3: 'Atlas,WARM_HF,hotfix,web2020janHF,HFDD20200203300PM' output3: 'HFDD20200203300PM'
For the above examples, the length and position of characters can't be standarardized. So I am unable to use SUBSTRING function.
I tried the following, but no results:
select
'Atlas,WARM_HF,hotfix,web2020janHF,HFDD20200203300PM'
where 'Atlas,WARM_HF,hotfix,web2020janHF,HFDD20200203300PM' like substring('''%HFDD%''',1,17)
Could someone please throw light on achieving the above output in SQL server?

Try this out if I understand your question right.
DECLARE #Desc VARCHAR(250) = 'Atlas,WARM_HF,hotfix,web2020janHF,HFDD20200203300PM'
SELECT SUBSTRING(#Desc, PATINDEX('%HFDD%', #Desc), 17)

DECLARE #STR VARCHAR(MAX)='Atlas,WARM_HF,hotfix,web2020janHF,HFDD20200203300PM'
SELECT SUBSTRING(#STR,CHARINDEX('HFDD',#STR),17)

Related

Find the position of first ' space lower case alphabet' in a string - Databricks SQL

I have an entry in a table as below
MasterCard recurrent=true renewal=true s
Visa initialCharge=true moneyBacks=82518
Visa paymentMethodFailed=true renewal=tr
Is there a way to find out the position of a pattern such as 'space first_lower_case_alphabet'
i.e
for string 'MasterCard recurrent=true renewal=true s' I want to find
the position of ' recurrent'
for string 'Visa initialCharge=true
moneyBacks=82518' I want to find the position of ' initialCharge'
I tried the following code but it does not work. The output is always 0
CHARINDEX('%[^ a-z]%', col_name)
Any help is appreciated, thank you!
You need another regular expression. Try the following with regexp_extract function:
SELECT regexp_extract(col_name, '\s[a-z]+', 1);
Then you can use this result in charindex function as following:
SELECT CHARINDEX(regexp_extract(col_name, '\s[a-z]+', 1), col_name)
See more in here: databricks documentation

SQL Server: Return a string in a specific format

In TSQL, I need to format a string in a predefined format.
For eg:
SNO
STRING
FORMAT
OUTPUT
1
A5233GFCOP
*XXXXX-XXXXX
*A5233-GFCOP
2
K92374
/X-000XXXXX
/K-00092374
3
H91543987
XXXXXXXXX
H91543987
I am trying with FORMATMESSAGE() built in function.
For ex:
FORMATMESSAGE('*%s-%s','A5233','GFCOP')
FORMATMESSAGE('/%s-000%s','K','92374')
FORMATMESSAGE('%s','H91543987')
I am able to get the first argument by replace function but issue is second/third/fourth/.. arguments.
I don't know how to count respective X's between the various delimiters, so that I can use substring to pass in second/third/.. arguments. If I can count the respective # of X's from the Format column, I feel using substring we can get it but not sure how to count the respective X's.
Please let me know how to get through it or if there is any other simple approach.
Appreciate your help.
Thanks!
It's in theory quite simple, could probably be done set-based using string_split however that's not ideal as the ordering is not guaranteed. As the strings are fairly short then a scalar function should suffice. I don't think it can use function in-lining.
The logic is very simple, create a counter for each string, loop 1 character at a time and pull a character from one or the other into the output depending on if the format string is an X or not.
create or alter function dbo.fnFormatString(#string varchar(20), #format varchar(20))
returns varchar(20)
as
begin
declare #scount int=1, #fcount int=1, #slen int=len(#string), #flen int=Len(#format), #output varchar(20)=''
while #scount<=#slen or #fcount<=#slen
begin
if Substring(#format,#fcount,1)='X'
begin
set #output+=Substring(#string,#scount,1)
select #scount+=1, #fcount +=1
end
else
begin
set #output+=Substring(#format,#fcount,1)
set #fcount +=1
end
end
return #output
end;
select *, dbo.fnFormatString(string, [format])
from t
See working Fiddle

SQL Server : query to get formatted string

Can anyone help me to extract string in a SQL Server query?
declare #txt = ' TOWER LAKES 60010 *Friday, 10/17/1952 CLOUTIER FABRICANTE .'
I want to get 60010 and 10/17/1952 from that string.
I tried
select #txt
where #txt like '[0-9]/[0-9]/[0-9]
Any other way to extract that string?
This is not well understood question on what exactly format you want to extract from the string values.
But, as your query suggests that also could achieve by using patindex() with substring() function to get fixed digits numeric values and date values
SELECT
substring(#txt, patindex('%[0-9][0-9][0-9][0-9][0-9]%', #txt), 5), Numeric_Values
substring(#txt, patindex('%[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]%', #txt), 10) Date_values

How to split a string using sybase query function

String str = 'ce765e1bc7:abc879:53:7011:2'
How to split the string using sybase query function to value 7011
I have working stored proc for this. But wanted to know if sybase provides any inbuilt function to do so.
If you know the position of the first character and length of the required pattern, you can use 'substring'
Syntax - substring(expression, start, length)
select substring('ce765e1bc7:abc879:53:7011:2',22,4)
If you only have string and the pattern to find but not sure about the length, you can additionally use 'charindex' and 'char_length' as shown in the example below:
BEGIN
DECLARE #stpos INT, #stlen INT
SELECT #stpos = charindex('7011', 'ce765e1bc7:abc879:53:7011:2')
SELECT #stlen = char_length('7011')
SELECT substring('ce765e1bc7:abc879:53:7011:2',#stpos, #stlen)
END

Syntax error when nesting STR inside custom formatting?

I'm trying to convert a number to a string with STR, and give it custom number formatting.
Currently, I tried to nest the two:
DECLARE
#Num float
SET
#Num = 222.33339
SELECT
FORMAT(STR(#Num,(8), 4), #,##0.00)
/*String(Original Number, Total Length of Number, Length of Decimal Places)*/
FROM
Sample
But I am receiving a syntax error in my Format function. I've tried debugging but I feel like I'm missing something obvious. Any suggestions?
Change your Format function to be:
SELECT FORMAT(#Num, N'#,##0.00')