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))
Related
Situation:
I have a column decoded from Hex to varchar where values look like this:
{"something":"example"}
Objective:
I would like to extract the second word between the quotes.
What i tried:
I started with a couple of substring and charindex functions but my code looks more complicated than it should be.
SELECT SUBSTRING(
SUBSTRING(
'{"something":"example"}',
charindex(':"','{"something":"example"}')+2,
LEN('{"something":"example"}')-charindex(':"','{"something":"example"}')+2),
0,
CHARINDEX('"',SUBSTRING(
'{"something":"example"}',
charindex(':"','{"something":"example"}')+2,
LEN('{"something":"example"}')-charindex(':"','{"something":"example"}')+2))
)
Any ideas?
If you're on SQL Server 2016+, you can use OPENJSON:
SELECT [value]
FROM OPENJSON('{"something":"example"}');
db<>fiddle
So against a table:
SELECT [value]
FROM (VALUES('{"something":"example"}'),
('{"another":"sample"}'))V(S)
CROSS APPLY OPENJSON(V.S);
This returns:
value
-------
example
sample
i would make a script that iterate over the records of a table with a cursor
and extract from a column value formatted like that "yyy://xx/bb/147011"
only the final number 147011and to put this value in a variable.
It's possible to do something like that?
Many thanks.
You don't need a cursor for this. You can just use a query. The following gets everything after the last /:
select right(str, charindex('/', reverse(str)) - 1 )
from (values ('yyy://xx/bb/147011')) v(str)
It does not specifically check if it is a number, but that can be added as well.
You can also use the below query.
SELECT RIGHT(RTRIM('yyy://xx/bb/147011'),
CHARINDEX('/', REVERSE('/' + RTRIM('yyy://xx/bb/147011'))) - 1) AS LastWord
If numeric value has exact position defined with sample data, then you can do :
SELECT t.*, SUBSTRING(t.col, PATINDEX('%[0-9]%', t.col), LEN(t.col))
FROM table t;
For example, I have got a string with table name and the schema like:
[dbo].[statistical]
How can fetch just the table name statistical out from this string?
This is what PARSENAME is used for:
SELECT PARSENAME('[dbo].[statistical]', 1)
SELECT PARSENAME('[adventureworks].[dbo].[statistical]', 1)
SELECT PARSENAME('[adventureworks]..[statistical]', 1)
SELECT PARSENAME('[statistical]', 1)
SELECT PARSENAME('dbo.statistical', 1)
-- all examples return 'statistical'
You could alternatively try this:
declare #s varchar(100) = 'asd.stadfa';
select reverse(substring(s, 1, charindex('.', s) - 1)) from (
select reverse(#s) s
) a
charindex returns first occurence of character, so you reverse initial string to make last dot first. Then you just use substring to extract first part of reversed string, which is what you are looking for. Finally, you need to apply reverse one more time to reverse back extracted string :)
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))
Using T-SQL, how would I go about getting the last 3 characters of a varchar column?
So the column text is IDS_ENUM_Change_262147_190 and I need 190
SELECT RIGHT(column, 3)
That's all you need.
You can also do LEFT() in the same way.
Bear in mind if you are using this in a WHERE clause that the RIGHT() can't use any indexes.
You can use either way:
SELECT RIGHT(RTRIM(columnName), 3)
OR
SELECT SUBSTRING(columnName, LEN(columnName)-2, 3)
Because more ways to think about it are always good:
select reverse(substring(reverse(columnName), 1, 3))
declare #newdata varchar(30)
set #newdata='IDS_ENUM_Change_262147_190'
select REVERSE(substring(reverse(#newdata),0,charindex('_',reverse(#newdata))))
=== Explanation ===
I found it easier to read written like this:
SELECT
REVERSE( --4.
SUBSTRING( -- 3.
REVERSE(<field_name>),
0,
CHARINDEX( -- 2.
'<your char of choice>',
REVERSE(<field_name>) -- 1.
)
)
)
FROM
<table_name>
Reverse the text
Look for the first occurrence of a specif char (i.e. first occurrence FROM END of text). Gets the index of this char
Looks at the reversed text again. searches from index 0 to index of your char. This gives the string you are looking for, but in reverse
Reversed the reversed string to give you your desired substring
if you want to specifically find strings which ends with desired characters then this would help you...
select * from tablename where col_name like '%190'