Get a particular value from String - sql

I want to remove particular value from my string. My string can be of any length. E.g.
string 1:
{"ABC":1,"ABC_DT":-1,"ABC_DBQty":0,"ABC_DSQty":0,"ABC_LMT":1,"ABC_DT":-1,"CTSD":"TEST","SD":1,"TE":23}
string 2:
{"ABC":1,"ABC_DT":-1,"ABC_DBQty":0,"ABC_DSQty":0,"ABC_LMT":1,"ABC_DT":-1,"CTSD":"TEST","TE":23}
I want this 23 as a result. How can I get this in SQL Server 2014?

DECLARE #string VARCHAR(500) = '{"ABC":1,"ABC_DT":-1,"ABC_DBQty":0,"ABC_DSQty":0,"ABC_LMT":1,"ABC_DT":-1,"CTSD":"TEST","TE":23}'
SET #string = REVERSE(#string)
SELECT REVERSE(
SUBSTRING(#string,2,CHARINDEX(':',#string)-1-1)
) --return: 23

SOLUTION:
DECLARE #str VARCHAR(MAX) = '{"ABC":1,"ABC_DT":-1,"ABC_DBQty":0,"ABC_DSQty":0,"ABC_LMT":1,"ABC_DT":-1,"CTSD":"TEST","TE":23}'
,#posA INT = 0
,#posB INT = 0
,#res VARCHAR(MAX) = ''
SET #posA = PATINDEX('%"TE":%}', #str) +5;
SET #posB = CHARINDEX('}', #str, #posA+1);
SET #res = SUBSTRING (#str ,#posA, #posB-#posA )
SELECT #res AS 'STR_res', CAST(#res AS INT) AS 'INT_res'

use the JSON_VALUE function:
https://learn.microsoft.com/en-us/sql/t-sql/functions/json-value-transact-sql?view=sql-server-2017&viewFallbackFrom=sql-server-2014
(unfortunately available since 2016)

Related

How to replace string in SQL from index to index

Hi guys can someone help me to write this?
DECLARE #STR VARCHAR(55) = 'Name';
SELECT // SOME LOGIC
RESULT => 'NXXE'
I would like this logic to be generalized to replace every string except the first and last character.
if dynamic data masking is not an answer you can use query below :
DECLARE #str VARCHAR(100) = 'yourstring'
SELECT UPPER(LEFT(#str,1) + REPLICATE('x',LEN(#str) -2)+ RIGHT(#str,1))
This is what you want
DECLARE #STR VARCHAR(55) = 'Name';
declare #i int
Set #i = 0
while #i <= len(#STR )
begin
select #i = #i + 1
if(#i>1 AND #i<len(#STR ))
select #STR = STUFF(#STR , #i, 1, 'X')
END
SELECT #STR

Extract characters after character '_' in string

I have the following string in sql server where the length is variable
Declare #string Varchar(max) = 'abjrc_chdc_hyku_abv_ytr_DCD_HTR_TBF'
How could I select the characters after the 4th '_' in this string? (expected result: ytr_DCD_HTR_TBF)
Also how to select only the 4th part of the string (result: abv)
Any Ideas?
Thanks
If you know exactly how many characters are in the string at any time, then you can use SUBSTRING, so you can call the first string like this
Declare #string Varchar(max) = 'abjrc_chdc_hyku_abv_ytr_DCD_HTR_TBF';
Declare #last4 Varchar(max) = SELECT SUBSTRING(#string, 21, 35);
Declare #before4 Varchar(max) = SELECT SUBSTRING(#string, 17, 19);
If y ou really need to split the string for each '_' then you can use this as well
hope i helped.
If you will do it for multiple line creating function would be better. I have prepared sample for your two question :
Declare #string Varchar(max) = 'abjrc_chdc_hyku_abv_ytr_DCD_HTR_TBF'
Declare #NewString Varchar(max) = #string
Declare #Find4thPart Varchar(max) =''
DECLARE #Sep VARCHAR(10)='_'
DECLARE #Pos INT=4
WHILE #Pos>0
BEGIN
SET #NewString = SUBSTRING(#NewString, CHARINDEX(#Sep,#NewString)+1,100)
IF #Pos=2
SET #Find4thPart = SUBSTRING(#NewString,1, CHARINDEX(#Sep,#NewString)-1)
SET #Pos=#Pos-1
--SELECT #NewString
END
seLECT #NewString ,#Find4thPart

SQL Query to retrieve 2 digit numbers from the below string

SQL Query to retrieve 2 digit numbers from the below string
String --> 'Partial:[64][95]'
The output should be in the below format.
64,95
You can use the replace() function several times to replace parts of the string.
In the example below I've set a declared variable to your example string:
DECLARE #mystring varchar(100) = 'Partial:[64][95]'
SELECT REPLACE(REPLACE(REPLACE(#mystring, 'Partial:[', '')
, '][', ',')
, ']', '') AS [answer]
Produces output:
answer
------
64,95
Use replace() :
select *, replace(replace(replace(Partial, '][', ','), '[', ''), ']', '')
from table t;
However, this would reduce your nested replace via TRANSLATE() but available from SQL Server 2017.
I was able to get the result with the below query.
DECLARE #string VARCHAR(100);
SET #string = 'Partial:[64][95]';
WHILE PATINDEX('%[^0-9]%', #string) <> 0
SET #string = STUFF(#string, PATINDEX('%[^0-9]%', #string), 1, '');
DECLARE #splitstring NVARCHAR(20) = #string;
DECLARE #i INT = 3;
WHILE #i < LEN(#splitstring)
BEGIN
SELECT #splitstring = STUFF(#splitstring, #i, 0, ',');
SET #i = #i +3;
END;
SELECT #splitstring;
Thanks everyone for the help.

SQL substring of URL

I am trying to select a part of a url which is after a wildcard expression and before another expression using SQL Server 2008 R2.
I have a url like:
https%www.msn.com%2ftokenauth%mapserver%ftoken%aogvgkooo%json
or:
https%www.msn.com%2ftokenauth%mapserver%token=aogvgkooo%json.
How do I write a sql query to only show aogvgkooo? The url is stored in column called url.
Here is my select statement:
select REPLACE(REPLACE(url, 'token=', ''),'%json', '')
have you looked at SUBSTRING, CHARINDEX or PATINDEX functions? this site gives a good explanation of them:
http://social.technet.microsoft.com/wiki/contents/articles/17948.t-sql-right-left-substring-and-charindex-functions.aspx
and this one gives an example as well
http://sqlzoo.net/howto/source/u.cgi/tip238311/sqlserver
DECLARE #url varchar(100)
SET #url ='https%www.msn.com%2ftokenauth%mapserver%token=aogvgkooo%json'
SELECT Replace(Replace(Replace(#url,substring(#url,1,Patindex('%token=%',#url)-1),''),'token=',''),'%json','')
sql fiddler
A user-defined function might be a good choice.
create function findToken (
#url nvarchar(max),
#tag nvarchar(100),
#after nvarchar(100)
) returns nvarchar(max) as begin
declare #token nvarchar(max);
declare #start int = charindex(#tag,#url) + len(#tag);
declare #justPast int = charindex(#after,#url,#start);
if #start = len(#tag) or #justPast = 0
return NULL;
return substring(#url,#start,#justPast-#start)
end;
go
declare #url varchar(100);
set #url ='https%www.msn.com%2ftokenauth%mapserver%token=aogvgkooo%json';
select
dbo.findToken(#url,'token=','%json');
DECLARE #url nvarchar(255),
#startPos nvarchar(100),
#endPos nvarchar(100)
SET #url ='https%www.msn.com%2ftokenauth%mapserver%token=aogvgkooo%json'
SET #startPos = 'token%'
SET #endPos = '%json'
SELECT SUBSTRING(#url,CHARINDEX(#startPos,REPLACE (#url,'=','%'),0)+ LEN(#startPos) ,CHARINDEX(#endPos,REPLACE (#url,'=','%'),0)-(CHARINDEX(#startPos,REPLACE (#url,'=','%'),0)+ LEN(#startPos)))
More general approach:
DECLARE #s VARCHAR(100) = 'https%www.msn.com%2ftokenauth%mapserver%ftoken%aogvgkooo%json'
SELECT REVERSE(SUBSTRING(
REVERSE(#s),
CHARINDEX('%', REVERSE(#s)) + 1,
CHARINDEX('%', REVERSE(#s), CHARINDEX('%', REVERSE(#s)) + 1) -
CHARINDEX('%', REVERSE(#s)) - 1))
Reversing string, then take substring from first occurence of % till second occurence, then reversing again.

How to replace all nonnumeric values?

This is TERADATA (not SQL Server, not Oracle )
I have a column of phone numbers:
(312)9879878
(298)989-9878
430-394-2934
394s9048ds987
..........
I need to clean this column into
3129879878
2989899878
4303942934
3949048987
..........
So that only numbers should stay. All other letters, special characters, hyphens ... should be removed. How can I do this?
Which release of TD is running at your site?
If it's 14 or you got the oTranslate UDF installed you can simply do an old trick nesting Translate:
oTranslate(phonenum, oTranslate(phonenum, '0123456789', ''), '')
Answer :
DECLARE #Input varchar(1000)
SET #Input = '01 vishal 98-)6543'
DECLARE #pos INT
SET #Pos = PATINDEX('%[^0-9]%',#Input)
WHILE #Pos > 0
BEGIN
SET #Input = STUFF(#Input,#pos,1,'')
SET #Pos = PATINDEX('%[^0-9]%',#Input)
END
SELECT #Input
Thank You,
Vishal Patel
I have this function to pull numerics (0-9) from a string:
CREATE FUNCTION NumbersOnly(#STR VARCHAR(2000))
RETURNS VARCHAR(1000)
AS
BEGIN
DECLARE #N INT
DECLARE #NN VARCHAR(1000)
SET #N = 0
SET #NN = ''
WHILE #N <= LEN(#STR)
BEGIN
IF SUBSTRING(#STR,#N,1) >= '0'
AND SUBSTRING(#STR,#N,1) <= '9'
BEGIN
SET #NN = #NN + SUBSTRING(#STR,#N,1)
END
SET #N = #N + 1
END
RETURN #NN
END