SQL select the last numbers from the right - sql

I am trying to select only the numbers from a string, starting from the right. So from the following examples:
1ECCA15
ECCB9
I would only like to select the numbers from the right. So the result would be:
15
9
I've tried this, but this does not select the numbers from the right.
substring(col, PatIndex('%[0-9]%', col), len(col))

Try this:
DECLARE #t TABLE(Col VARCHAR(255));
INSERT #t SELECT '1ECCA15';
INSERT #t SELECT 'ECCB9';
SELECT REVERSE(LEFT(REVERSE(col), PATINDEX('%[a-z]%', REVERSE(col)) - 1)) FROM #t;

There you go, make use of REVERSE
CREATE TABLE #temp(col varchar(20))
INSERT INTO #temp values('1ECCA15'),('ECCB9')
SELECT REVERSE(SUBSTRING(REVERSE(Col), 0, PATINDEX('%[^0-9]%',REVERSE(col))))
FROM #temp

How about this?
select right(col, patindex('%[^0-9]%',reverse(col))-1) from your_table;

Related

Sort Alphanumeric column in SQL Server

My data is in this way
<400
1000-1200
1200-1400
1400-1600
1600-1800
400-600
600-700
700-800
800-1000
And I want this way
<400
400-600
600-700
700-800
800-1000
1000-1200
1200-1400
...
order by LEFT(WC.[WGHT_CLAS_DESC], PATINDEX('%[0-9]%', WC.[WGHT_CLAS_DESC])-1),
CONVERT(INT, SUBSTRING(WC.[WGHT_CLAS_DESC], PATINDEX('%[0-9]%', WC.[WGHT_CLAS_DESC]), LEN(WC.[WGHT_CLAS_DESC])))
This is the code I have used in SQL Server but its not working
Try this:
DECLARE #DataSource TABLE
(
[Column] VARCHAR(32)
);
INSERT INTO #DataSource ([Column])
VALUES ('<400')
,('1000-1200')
,('1200-1400')
,('1400-1600')
,('1600-1800')
,('400-600')
,('600-700')
,('700-800')
,('800-1000');
SELECT [Column]
,TRY_CONVERT(INT, SUBSTRING([Column], 0, CHARINDEX('-', [Column])))
FROM #DataSource
ORDER BY TRY_CONVERT(INT, SUBSTRING([Column], 0, CHARINDEX('-', [Column])));
Note, that TRY_CONVERT is available after SQL Server 2012, so if you are using earlier version you can use CAST:
ORDER BY CAST(SUBSTRING([Column], 0, CHARINDEX('-', [Column])) AS INT)
I think you mean this:
ORDER BY LEFT(WC.[WGHT_CLAS_DESC], PATINDEX('%[0-9]%', WC.[WGHT_CLAS_DESC])-1) DESC,
CONVERT(INT, SUBSTRING(WC.[WGHT_CLAS_DESC], PATINDEX('%[0-9]%', WC.[WGHT_CLAS_DESC]), PATINDEX('%[^0-9]%', WC.[WGHT_CLAS_DESC])-1))
Try this:
declare #t table(val varchar(max))
insert into #t
select '<400'
union select '1000-1200'
union select '1200-1400'
union select '1400-1600'
union select '1600-1800'
union select '400-600'
union select '600-700'
union select '700-800'
SELECT rn=row_number() over( order by StartNumber,EndNumber),StartNumber,EndNumber Val
FROM (
select
CAST(RIGHT(val,LEN(val)-CHARINDEX('-',val)) as int) EndNumber,
CAST(REPLACE(val,'-'+RIGHT(val,LEN(val)-CHARINDEX('-',val)),'') as int) StartNumber,
val
from (
SELECT REPLACE(Val,'<','0-') As Val from #t
) a
)Data
of course, i suggest to use TRY_CONVERT after SQL2012

Using SQL to query numbers that have more than tenths decimal place

I have a text field that contains numbers. Some of these numbers have two digits after the decimal and others only one.
Is there a SQL expression to query out only the numbers containing the hundredths values?
My end goal is to use the "round" function to round these isolated numbers to the tenths place.
RDBMS used: sql-server
You can do this with charindex and reverse.
select val
from tbl
where charindex('.', reverse(val)) = 3
Demo
declare #tbl table(val varchar(50))
insert into #tbl select '11.02'
insert into #tbl select '411.0'
insert into #tbl select '11.44'
insert into #tbl select '1144.03'
insert into #tbl select '1441.5'
select val
from #tbl
where charindex('.', reverse(val)) = 3
output:
11.02
11.44
1144.03

split string in sql server 2008

In my table, I have Experience field like
Experience
0to0Years
2to0Years
7to12Years
Here I want to split into
Yearfrom - 0
Yearto- 0
How to split strings here. I search so many articles. I can't find the correct solution. Is there any way to do here?
Here is a working solution
declare #yearfrom varchar(2),#yearto varchar(2)
select #yearfrom=substring('0to0Years',0,patindex('%to%','0to0Years')),
#yearto=substring('0to0Years',patindex('%to%','0to0Years')+2,patindex('%Years%','0to0Years')-patindex('%to%','0to0Years')-2)
SqlFiddle: http://www.sqlfiddle.com/#!3/d41d8/12483
For working on your column replace '0to0Years' with column name
declare #yearfrom varchar(2),#yearto varchar(2)
select #yearfrom=substring(col_name,0,patindex('%to%',col_name)),
#yearto=substring(,patindex('%to%',col_name)+2,patindex('%Years%',col_name)-patindex('%to%',col_name)-2)
from table_name where <condition>
Try with following Steps...
--Create Table :
Create Table #Table
(
Name Varchar(50),
Experience Varchar(20)
)
Go
-- Insert Values :
Insert into #Table Values('Tom','0to0Years')
Insert into #Table Values('Victor','0to1Years')
Insert into #Table Values('Mark','11to12Years')
Go
--View Data
Select * from #Table
--Using CharIndex and Substring :
Select Name,
Substring(Experience,1,CharIndex('to',Experience)-1) as Yearfrom,
Substring(Experience,(CharIndex('to',Experience)+2),Len(Replace(Experience,'Years','')) - (CharIndex('to',Experience)+1)) as YearTo
from #Table
Please try:
select 'YearFrom - '+substring(Data, 0, PatIndex('%[to]%', Data)) YearFrom,
'YearTo - '+replace(stuff(Data, 1, PatIndex('%[to]%', Data)+1, ''), 'Years', '') YearTo
from
(
Select '0to0Years' Data union
Select '2to0Years' Data union
Select '756to12Years' Data
)x
Try this using Parsename function
Select parsename(replace(Experience,'to','.'),2) ,
substring(parsename(replace(Experience,'to','.'),1),0,
charindex('Y',parsename(replace(Experience,'to','.'),1)))
from YourTable
Demo in SQLFiddle

how to substring of a string in SQL

My data will be like that
IPDocument/XR/test_20120409_clearGAC.txt
IPDocument/XR/test_20120409_clearGAC_05.txt
IPDocument/XR/test_20120409_clearGAC_01.txt
I would like to extract clearGAC.txt from above.
How can I use substring in SQL ?
I assume that you want the _05 and _01 - the question is not clear.
DECLARE #var VARCHAR(250)
DECLARE #table TABLE ( val VARCHAR(250) )
INSERT INTO #table
SELECT 'IPDocument/XR/test_20120409_clearGAC_01.txt'
UNION ALL
SELECT 'IPDocument/XR/test_20120409_clearGAC_05.txt'
UNION ALL
SELECT 'IPDocument/XR/test_20120409_clearGAC.txt'
SELECT SUBSTRING(val, CHARINDEX('clearGAC', val), 50),
SUBSTRING(SUBSTRING(val, CHARINDEX('_', val)+1, 50), CHARINDEX('_', SUBSTRING(val, CHARINDEX('_', val)+1, 50))+1, 50)
FROM #table
Returns this:
clearGAC_01.txt
clearGAC_05.txt
clearGAC.txt
The third parameter in the CHARDINDEX can be larger than the length.
EDIT: I added logic if you don't know if clearGAC is the name
After reading your comment, you may just want this:
I am going to pass ClearGAC.txt and see if it's in the DB. this
ClearGAC.txt may be named like above codes.
DECLARE #var VARCHAR(250)
DECLARE #table TABLE ( val VARCHAR(250) )
INSERT INTO #table
SELECT 'IPDocument/XR/test_20120409_clearGAC_01.txt'
UNION ALL
SELECT 'IPDocument/XR/test_20120409_clearGAC_05.txt'
UNION ALL
SELECT 'IPDocument/XR/test_20120409_clearGAC.txt'
SELECT *
FROM #table
WHERE val LIKE '%clearGAC%.txt'
SELECT SUBSTRING('IPDocument/XR/test_20120409_clearGAC.txt', CHARINDEX('09_','IPDocument/XR/test_20120409_clearGAC.txt')+3, 100)
result :
clearGAC.txt

How to Split string value in sqlserver

I have a following string
90-PMR-450
90-PMRA-340
I want to get part 3 of string. example 450 or 340.
plese help me. thanks
declare #T table
(
Value varchar(15)
)
insert into #T values
('90-PMR-450'),
('90-PMRA-340')
select stuff(Value, 1, 1+len(Value)-charindex('-', reverse(Value)), '')
from #t
DECLARE #x TABLE(v VARCHAR(32));
INSERT #x SELECT '90-PMR-450'
UNION ALL SELECT '90-PMRA-340';
SELECT Part3 = PARSENAME(REPLACE(v, '-', '.'), 1) FROM #x;
i think you will find this user defined function to split the string helpful:
http://www.codeproject.com/Articles/7938/SQL-User-Defined-Function-to-Parse-a-Delimited-Str