String manipulation with SQL Server - sql

I have the following table with (man-made) IDs.
ID Name
AB12345 John
12346 Charles
...
How do I write a SELECT that returns only the number segment of the ID column? Like this:
ID Name
12345 John
12346 Charles
...

SELECT SUBSTRING(string, PATINDEX('%[0-9]%', string), PATINDEX('%[0-9][^0-9]%', string + 't') - PATINDEX('%[0-9]%',
string) + 1) AS Number
FROM table
here string is equal to id

You could write a regex to extract the numeric values that you are looking for.
This might help
Query to get only numbers from a string

Related

Trim characters

All,
I have 2 values as below in a table.
ID Name
1 Justs_S01_3456
2 S01_56788
The output I want is below.
ID Name NewName
1 Justs_S01_3456 S01_3456
2 S01_56788 S01_56788
I tried using CHARINDEX and other functions,but I am not getting the desired output.
Can some one please help me?
Thanks,
John
In SQL Server, you could use:
select t.*, stuff(name, 1, charindex('S01_', name) - 1, '') as newname
Here is a db<>fiddle.
You can use String functions.
The PATINDEX() function returns the position of a pattern in a string (beginning position 'S01' in the Name)
AND using SUBSTRING function you can extract the string from S01 position to the end of string:
SELECT *, SUBSTRING(Name,PATINDEX('%S01%',Name),len(Name)) As NewName from Table

SQL: Obtaining Substring Prior to Dash Character. From 'ABC-123' to just 'ABC'

I have a table CARM with fields (columns) ID and AREA_DESC.
In the AREA_DESC field, I have some values that show as follow:
AREA_DESC
--------------
Felisberto-001
Ana
Mark-02
Maria
--------------
What I would like to do, is to display these values in this way:
AREA_DESC
--------------
Felisberto
Ana
Mark
Maria
--------------
As you may notices, I would like to only display the string prior to the dash - only. In other words, I would like to drop the dash - and numbers after the dash -
This is the query I have tried so far:
SELECT ID, AREA_DESC ,SUBSTRING(AREA_DESC,0,CHARINDEX('-', AREA_DESC)) as area
FROM CARM
The simplest method is to add a '-' for the charindex():
SELECT ID, AREA_DESC,
LEFT(AREA_DESC, CHARINDEX('-', AREA_DESC + '-')) as area
FROM CARM;
Notice that this also uses LEFT() it saves an argument in the string operation.
You can use left() instead with addition of '-' at the end of string:
select ID, AREA_DESC, left(AREA_DESC, charindex('-', AREA_DESC + '-'))
from CARM;

Sybase SQL: How to extract numbers from varchar-values?

I have a bunch of values (varchars) that consist partly of letters (whereby number of letters >= 0) and partly of numbers.
For example:
abc-123
defjke12345
987654
Is there a function in sybase sql that extracts the part of the value that consists of numbers?
Continuing with the given examples,
abc-123 should become 123
defjke12345 should become 12345
987654 should stay 987654
How can I achieve this in sybase sql (procedural language)?
Try this out with the substring and pattern index:
DECLARE #yourfield VARCHAR(100)
SELECT #yourfield ='defjke12345'
SELECT SUBSTRING(#yourfield , PATINDEX('%[^0-9][0-9]%', #yourfield ) + 1,
PATINDEX('%[0-9][^0-9]%', #yourfield ) - PATINDEX('%[^0-9][0-9]%', #yourfield ))

Split a column in two based based on variable lenght field

Hi: I have a table made with rows like this:
ID_CATEGORIA CATEGORIA_DRG
------------ ---------------------------------------------------------------
1 001-002-003-543 Craniotomia
2 004-531-532 Interventi midollo spinale
3 005-533-534 Interventi vasi extracranici
4 006 Decompressione tunnel carpale
I'd like to get something like this:
ID CATEGORIA DESCRIZIONE
------------ ------------------ --------------------------------------
1 001-002-003-543 Craniotomia
2 004-531-532 Interventi midollo spinale
3 005-533-534 Interventi vasi extracranici
4 006 Decompressione tunnel carpale
I don't need to alter the table, a 'formatted' query can be enough.
I Think SUBSTRING() is the right function for me, but I don't know how to mesaure the lenght of the first (numbers, dash-separated) field.
In Python I'll find that size with len("005-533-534 Interventi vasi extracranici".split(' ')[0])', but I don't have idea about how to write it in SQL
Something like this should do -
SELECT ID_CATEGORIA AS ID ,SUBSTRING(CATEGORIA_DRG,1,CHARINDEX(' ',CATEGORIA_DRG)) as CATEGORIA,SUBSTRING(CATEGORIA_DRG,CHARINDEX(' ',CATEGORIA_DRG),LEN(CATEGORIA_DRG)) AS DESCRIZIONE
FROM TABLENAME
Try this:
select id_categoria ID,
substring(categoria_drg, 1, idx) CATEGORIA,
substring(categoria_drg, idx + 1, 1000) DESCRIZIONE
from (
select id_categoria, categoria_drg, charindex(' ', categoria_drg) idx from my_table
) a
It uses charindex to detect when the code is finished, because it is followed by first space in the string, which the function finds :)

Select only when the field has more than one word

SELECT name FROM clients;
Table clients
id | name |
1 John
2 John Bravo
3 John Alves
4 Jo
In postgres, how can I select only names with more than one word? For example. This should be the output:
John Bravo
John ALves
I think just test if the name contains a space: where name like '% %'
But this will give you some problem if you name can contain space char befor or after the name, like: ' JOHN' or 'Luck '
If word means to you a space delimited token, then the following would do the trick:
SELECT name FROM clients WHERE name LIKE '% %';
But this will also give you those that have empty names made out of spaces only. Also performance-wise, this will be a costly query.
First you may have to find the number of words in the column, then only select where the number of words is greater than 1.
For example:
SELECT LENGTH(name) - LENGTH(REPLACE(name , ' ', ''))+1 FROM clients;
This will return the number of words in each row, which we have in the field name.
Then you can proceed putting this in a nested select SQL statement something like :
SELECT LENGTH(name) - LENGTH(REPLACE(name, ' ', ''))+1 as mycheck, name FROM clients where (SELECT LENGTH(name) - LENGTH(REPLACE(name, ' ', ''))+1)>1
This will return only where words are greater than 1 (>1). Else you can user >2 to return columns with more than 2 words.
LIKE is an option, or you can use split_part:
SELECT name FROM clients WHERE split_part(trim(name), ' ', 2) <> ''