Using SQL Server 2008
String like: 'C/123232323' or '/343434343443' or 'C2323232322'
From the string i want to remove C and /
Tried Query
Select replace ('/1233434', 'C/', '')
The above query is working if C/ both is there. if / only there then the replace is not working. if C only there then the replace is not working. How to achieve for both condition
Expected output
123232323
343434343443
2323232322
Need Query output
You can achieve this by nesting replace() like so:
select replace(replace('C/12341234','/',''),'C','')
Probably not the prettiest but it works :)
You could use two nested REPLACE:
WITH SampleData(string) AS(
SELECT 'C/123232323' UNION ALL
SELECT '/343434343443' UNION ALL
SELECT 'C2323232322'
)
SELECT REPLACE(REPLACE(string,'C',''),'/','')
FROM SampleData
Use Patindex + Substring
DECLARE #str VARCHAR(50)='/343434343443' ---or '/343434343443' or 'C2323232322'
SELECT Substring(#str, Patindex('%[0-9]%', #str), Len(#str))
Related
I have a field which holds data like this
Product.Company.Price.Item
I want to substring any string after the last dot. so it will look like this
Item
I know there is a substring.index function in MySQL. How can I implement this in SQL Server?
select reverse(substring(reverse(fieldName),1,charindex('.',reverse(fieldname))-1))
This will do what you want, but I wouldn't use it as part of a where expression
declare #x varchar(50) = 'Product.Company.Price.Item'
select substring(#x,len(#x)-charindex('.',reverse(#x))+2,99)
Hope this Query works fine for your Case:
DECLARE #VAR VARCHAR(MAX)='Product.Company.Price.Item'
SELECT RIGHT(#VAR, CHARINDEX('.', REVERSE(#VAR) + '.') - 1) AS VARL
If it's always 4 parts then you could use parsename() as
select parsename('Product.Company.Price.Item', 1)
or use right() and charindex() as
select right(str, p)
from
(
values ('Product.Company.Price.Item'), ('Other.row')
) t(str) cross apply
(
values (charindex('.', reverse(str))-1)
) tt(p)
You can try this using functions like Reverse(), Left(), and the CHARINDEX().
DECLARE #Sentence VARCHAR(100) = 'Product.Company.Price.Item';
SELECT REVERSE(LEFT(REVERSE(#Sentence),
CHARINDEX('.',REVERSE(#Sentence))- 1)) AS [Last_Word]
I have a string like this 'sdf,11-df,12-asd,sadfsdf'. But I need only numbers from this string in multiple rows like this.
11
12
I need numbers between (,) and (-) that's the actual requirement. String can be like 'sd47f,11-df,12-asd,sadfsdf,12-ds,32-fsdfsd' anything, But numbers will always between (,) and (-)
or find numbers between (,) and (-) it will also help me.
Thanks in advance.
I need any solution guys please help,
Following will work with SQL Server 2016 or higher:
DECLARE #str varchar(50) = 'sd47f,11-df,12-asd,sadfsdf,12-ds,32-fsdfsd'
SELECT
LEFT(Value, CHARINDEX('-', Value)-1)
FROM STRING_SPLIT(#str, ',')
WHERE PATINDEX('%[0-9]-%',Value) > 0
try this
DECLARE #data nvarchar(max) = 'sdf,11-df,12-asd,sadfsdf'
select substring(#data,PATINDEX('%[0-9]%',#data),2)
union all
select substring(#data,PATINDEX('%[-]%',REVERSE(#data))-1,2)
Update
You can also use a function which returns as a table
CREATE FUNCTION dbo.udf_GetNumeric (#strAlphaNumeric VARCHAR(256))
RETURNS TABLE AS RETURN ( select LEFT(value,2) as Value from
string_split(#strAlphaNumeric,',') where PATINDEX('%[0-9]-%',value)
> 0 )
END GO
SELECT * from
dbo.udf_GetNumeric('sd47f,11-df,12-asd,sadfsdf,12-ds,32-fsdfsd')
Am using following query but this to string no form how many number but actually don't know about how many character after '/' and before ?
String data like :
12/KH/123/1234
output:-
1234
Query:-
SELECT SUBSTRING(ColName,9,CHARINDEX('/',ColName,4))
FROM TABLE
Use Left, Reverse and Charindex function. Try this.
DECLARE #set VARCHAR(100)= '12/KH/123/1234'
SELECT Reverse(LEFT(Reverse(#set), Charindex('/', Reverse(#set)) - 1))
Try this.It may help you
SELECT SUBSTRING('12/KH/123/1234' ,11,CHARINDEX('/','12/KH/123/1234' ,4))
I have a variable where i dynamically store the URL
For Eg. a="https://myaccessdev.searshc.com/aveksa/main?Oid=1&ReqType=GetPage&ObjectClass=com.aveksa.gui.objects.workflow.GuiWorkflowJob&WFObjectID=3478:WPDS"
I want to strip the number in the last before ":WDPS" from this whole string.
Is there any way of doing it in SQL
I want to strip the number [...] before ":WDPS" from this whole string.
This is a perfect match for REGEXP_REPLACE:
with test_data as
(select 'https://myaccessdev.searshc.com/aveksa/main?Oid=1&ReqType=GetPage&ObjectClass=com.aveksa.gui.objects.workflow.GuiWorkflowJob&WFObjectID=3478:WPDS' str
from dual)
select REGEXP_REPLACE(str, '.*=([0-9]*):WPDS.*', '\1') from test_data
-- ^^^^^^^^^^^^^^^^^^^^ ^^^^
-- replace everything before and after by the first capturing
-- the target string group (i.e.: the sequence between
-- parenthesis in the regular expression)
Producing:
3478
See http://sqlfiddle.com/#!4/d41d8/37095 for a live demo.
If you just want the number, you can do something like this:
select regexp_substr(regexp_substr(s, '=[0-9]*:WPDS', 1, 1), '[0-9]*', 1, 1)
You can do this with regexp_substr(). I'm having trouble testing it right now. The following comes quite close:
select regexp_substr(s, '=[0-9]*:WPDS', 1, 1)
Try this
DECLARE #string varchar(255) = 'https://myaccessdev.searshc.com/aveksa/main?Oid=1&ReqType=GetPage&ObjectClass=com.aveksa.gui.objects.workflow.GuiWorkflowJob&WFObjectID=3478:WPDS'
DECLARE #end varchar(10) = REVERSE(SUBSTRING(REVERSE(#string),0,CHARINDEX('=', REVERSE(#string))))
SELECT SUBSTRING(#end,0,CHARINDEX(':', #end))
Maybe this sounds a little bit crazy, but I need to come up with a query to retrieve only letters out of an alphanumeric field.
For example:
TABLE
1234ADD
3901AC
1812OPA
82711AUU
RESULTS EXPECTED
ADD
AC
OPA
AUU
Thank you!
Looks like you only want to remove numbers. You can use REGEXP_REPLACE for that in 10g or 11g:
SELECT REGEXP_REPLACE( your_column, '[0-9]*', '' ) FROM your_table;
SELECT REGEXP_REPLACE('1234ADD 3901AC 1812OPA 82711AUU', '[0-9]', '')
FROM dual
Try
SELECT TRANSLATE('1234ADD 3901AC 1812OPA 82711AUU', 'A1234567890', 'A') FROM dual;
and in general see: http://www.psoug.org/reference/translate_replace.html