SQL Server - select substring of all characters following last hyphen - sql

I am working with a database of products, trying to extract the product color from a combined ID/color code column where the color code is always the string following the last hyphen in the column. The issue is that the number of hyphens, product ID, and color code can all be different.
Here are four examples:
ABC123-001
BCD45678-0165
S-XYZ999-M2235
A-S-ABC123-001
The color codes in this case would be 001, 0165, M2235, and 001. What would be the best way to select these into their own column?

I think the following does what you want:
select right(col, charindex('-', reverse(col)) - 1)
In the event that you might have no hyphens in the value, then use a case:
select (case when col like '%-%'
then right(col, charindex('-', reverse(col)) - 1)
else col
end)

It is great to check whether the hyphen exists or not in the string, to avoid the following error:
Invalid length parameter passed to the right function.
SELECT CASE WHEN Col like '%\%' THEN RIGHT(Col,CHARINDEX('\',REVERSE(Col))-1) ELSE '' END AS ColName

Related

equals letter start and end SQL

how to select a cells with contents (varchar) starting and ending with equals letter in SQL??
You can use string functions. Assuming a table called mytable with a column called col:
select *
from mytable
where left(col, 1) = right(col, 1)
Note that this also allows values that are made of just one character - you did not tell which behavior you want in that case. You can easily avoid that, if that's what you want, with another condition on the length of the string:
select *
from mytable
where left(col, 1) = right(col, 1) and len(col) > 1

TSQL extract part of string with regex

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;

SQL Server: Substring based on first two characters being a number

I have a text field and want to pull out an ID number from the field - the ID always starts with an 8 and is 12 characters long (e.g 899900014658), the current code uses the substring method below:
substring(textfield,charindex('8',textfield),12) as extractedID
This pulls out anything starting with an 8 though so I'm getting results like '8 am', '8 February' etc in the extractedID field.
Is there a method of extracting anything starting with an 8 where the second and third characters are also a number?
edit - solved using PATINDEX
SUBSTRING(SubmissionDiaryEntry,(PATINDEX('%8[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]%',SubmissionDiaryEntry)),12)
If I understand your question:
Select *
From YourTable
Where textfield like '8[0-9][0-9]%'
Ckeck below, it may help:
declare #textfield nvarchar(20) = '845456798234'
select case when substring(#textfield, 2, 1) like '[0-9]'
and substring(#textfield, 3, 1) like '[0-9]'
then substring(#textfield,charindex('8',#textfield),12)
else 'not valid'
end
as extractedID
-- test
set #textfield = '8r5456798234'
select case when substring(#textfield, 2, 1) like '[0-9]'
and substring(#textfield, 3, 1) like '[0-9]'
then substring(#textfield,charindex('8',#textfield),12)
else 'not valid'
end
as extractedID
however John's answer is much simplier and it looks like exactly what you are looking for.
Below is just to clarify..., using John's script, answer to your issue would be:
select substring(textfield,charindex('8',textfield),12) as extractedID
from yourTable
where textfield like '8[0-9][0-9]%'

TSQL - Remove Everything after the last period

I have a column string that may be one of the following.
10.0.2531.0
10.50.2500
10.0.2531.60
My requirement is, if there are 3 periods/decimal points, remove the last period/decimal and everything after that.
If I use the following, this will take care of the first row where there is only ".0", however, it does not work for the third row.
select
case
when right(column_1,2) = '.0' then left(column_1,len(column_1)-2)
else column_1 end,
FROM
table_1
I also tried the following but that didn't work.
select
case
when right(column_1,2) = '.' then left(column_1,len(column_1)-2)
when right(column_1,3) = '.' then left(column_1,len(column_1)-3)
else column_1 end,
FROM
table_1
The number after the third period/decimal may be a 0 or another number.
The following works, under the assumption that there are never five periods:
select (case when ip like '%.%.%.%'
then left(ip, len(ip) - charindex('.', reverse(ip))
else ip
end) as firstThree
Use a combination of charindex and substring.
SQL Fiddle
select reverse(substring(reverse(column_1), charindex('.',reverse(column_1))+1, len(column_1)))
from table_1
where len(column_1) - len(replace(column_1,'.','')) = 3

Removing Characters From SQL String (Search)

Should be a pretty simple one today. I have a column with a list of contracts:
As you see, some contracts have a "-...." at the end. I need to removed this and any character after this (see desired output). Unfortunately, it's not just 1 character (could be multiple/differenct #s). So I imagine it's going to be a right/len/search combo.
Thoughts?
If the contracts all have the same form, the following is almost standard SQL for doing what you want:
select (case when CTNumber like '%-%-%'
then left(CTNumber, 7)
else CTNumber
end) as DesiredOutput
from t
The like syntax is standard, the case is standard, and most databases have a left() function. This does assume that the form of the CTNumber is such that you always want the first seven characters.
You can use patindex to confirm the presence of two dashes. With a double charindex, you can then return the part of the string before the second dash:
select case
when patindex('%-%-%', col1) > 0 then
left(col1, charindex('-', col1, charindex('-', col1) + 1) - 1)
else col1
end as col1
from dbo.YourTable