Substringing from a text a number - sql

From the following string value of session i will like to keep with only the part when the first number big or the last | beginning
session
nea|fact|za|ninja|web|14ff95092e3x1d214cd2
nea|fact|za|ninja|web|15001274f5ex323c9f96
nea|fact|za|ninja|web|1502897832ax418ecf1a
nea|fact|za|ninja|web|150399c1418x215f0e52
nea|fact|za|ninja|web|1503b3cdf02x386fc450
ta|fact|za|ninja|web|1503b3cdf02x386fc450
ta|fact|za|ninja|web|1503b3cdf02x386fc450
expected result
14ff95092e3x1d214cd2
15001274f5ex323c9f96
1502897832ax418ecf1a
150399c1418x215f0e52
1503b3cdf02x386fc450
1503b3cdf02x386fc450
1503b3cdf02x386fc450

if your db mysql then SUBSTRING_INDEX will help you
select SUBSTRING_INDEX(session, "|", -1);
Example:
select SUBSTRING_INDEX('nea|fact|za|ninja|web|14ff95092e3x1d214cd2', "|", -1);
returned: 14ff95092e3x1d214cd2
In mysql string related function
For Sql server your query will be
SELECT RIGHT(session , CHARINDEX ('|' ,REVERSE(session))-1)

In Oracle -
SELECT SUBSTR(session, '|', -1)
FROM TABLE_NAME;

For sql server:
declare #test varchar(1000) = 'nea|fact|za|ninja|web|14ff95092e3x1d214cd2'
SELECT RIGHT(#test , CHARINDEX ('|' ,REVERSE(#test))-1)

Related

how to convert any char to - in sql server?

i have one column in sql table like this
codel
-------------
124/500/319/1
500/2698794/3
130.500.2804508.1
800/283478/2
155-305-340007-1
130.500.2686821.1
how i can convert this code to
codel
-------------
124-500-319-1
500-2698794-3
130-500-2804508-1
800-283478-2
155-305-340007-1
130-500-2686821-1
thank you for read my qustion
In SQL Server 2017, you can also do it using TRANSLATE like following.
SELECT TRANSLATE(codel,'/.','--') AS Codel FROM YOUR_TABLE
Try below - using replace() function
select replace(replace(codel,'/','-'),'.','-') from tablename
try like below by using replace
SELECT REPLACE(codel, '/', '-');
go
SELECT REPLACE(codel, '.', '-');

SQL server Rex fetch string in wildcard

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 :)

How To Extract Only Number From String Having chars & date as well as numbers

I have a field in sql
NAME :
5010001918/2/03/000 DATED:20.12.2013
5010002069/2/03/00 DATED:29.04.2014
ADV NO:5210041376/2/03/00 DATED:06.01.2015
LICENCE NO-5010002165/2/03/00 DATE:-14.11.2015
I want output :
5010001918/2/03/000
5010002069/2/03/00
5210041376/2/03/00
5010002165/2/03/00
I tried substring part but it doesn't seems to be working.
try if sqlserver
select substring(your_column,patindex('%[0-9]%',#n),19) from yourTable
you can use substring
select substring(your_column,1,instr(your_column," ") -1) my_value,
from tablename;

select all character right of forward slash in sql server?

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))

How to extract Certain nth character from a string in SQL

I have a field that returns the value as xxx-xxx-xxx-xxxxx-xx-x. How do i extract the 10th character from that code.
select substring('xxx-xxx-xxx-xxxxx-xx-x', 10, 1)
The documentation for the function on MSDN is here.
The SQL Fiddle demo is here (with different letters so you can clearly see which letter is extracted).
Use substring function
select substring('xxx-xxx-xax-xxxxx-xx-x', 10, 1)
you can use SUBSTRING, in your case use...
SELECT SUBSTRING(field, 10, 1)
field being the one that returns the value.
declare #x varchar(20) = 'xxx-xxx-xxx-xxxxx-xx-x'
select SUBSTRING(#x,10,CHARINDEX('-',#x)-4 )