Hi everyone this is my first time that i asked something.
I have multiple codes on my db with numbers and '/', for example:
510325205
510325205/000/01
510565025-01
510565025-01/090/03
...
I need to trim the / - I need these results:
510325205
510325205
510565025-01
510565025-01
...
I already searched and tried this
left(code, charindex('/', code))
and it works for the codes with / in it, but the codes without / are excluded from the results.
Thanks for your help!
Your try was very close.
All you needed to add was -1.
As you can see the explanation for the left() function:
The LEFT() function extracts a number of characters from a string
(starting from left): LEFT(string, number_of_chars)
With charindex() function you have told the left() function how many characters to take. By adding -1 to that you have removed the '/' sign because you have told the LEFT() function to take 10 characters and not 11(for example) because 11th character is '/'.
select left('510325205/000/01',charindex('/','510325205/000/01')-1)
or because you have column named code
select left(code,charindex('/',code)-1)
If you have values without / you can use this:
select case when charindex('/',code_c)-1 = -1
then
code_c
else
left(code_c,charindex('/',code_c)-1)
end RESULT
from test
OR
select left(code_c,iif(charindex('/',code_c)-1 = -1, len(code_c), charindex('/',code_c)-1))
from test
Here is DEMO
Related
I have an easy problem (I think) that I couldn't solve:
I have many tag like this:
'FOOT05_00120_RIG_1_GOAL_K'
but the length is not the same for all the records and I need a query to take all the character before the last _, does anybody how can I do?
(It could be within such a query:
SELECT tag FROM Football
)
Thanks to everyone
This problem seems almost designed for regexp_replace():
select regexp_replace(tag, '(.*)_[^_]*$', '\1')
from football;
The regular expression is interpreted as:
'(.*)_[^_]*$'
-^ remember this expression for a reference
--^ any character
---^ any number of times
----^ followed by _
-----^ followed by any characters that are not _
----------^ any number of times
-----------^ at the end of the string
You can use REVERSE function to determine the position of last hyphen, and then apply SUBSTRING() along with LENGTH() function such as
WITH Football AS
(
SELECT 'FOOT05_00120_RIG_1_GOAL_K' AS tag
)
SELECT SUBSTRING( tag, 1, LENGTH(tag) - POSITION('_' IN REVERSE(tag)) ) AS Result
FROM Football
Demo
I have a number of rows in a database column that look like this
AB01-52
AB01-52/1
AB01-53/2
AB01-54
I can get the 52 where there is no slash in the string but when I try to get it between the dash and the slash I either get an error or get the 52/1 for example
I have tried most of the researched solutions using substring and charindex but to no avail
Has anyone seen a solution for this.
Thanks in advance
Rick
You can subtract the characters positions (i.e. /) and use as length for substring()
substring(col, charindex('-', col)+1, len(col)-charindex('/', col)+1)
If the numbers have variable length then do the subtraction from both characters and use of length
substring(col, charindex('-', col)+1, charindex('/', col+'/')-charindex('-', col)-1)
Here's one approach....
with cte as(
select 'AB01-53/2' as C1
union
select 'AB01-54')
select left(right(c1,len(c1) - charindex('-',c1)),len(right(c1,len(c1) - charindex('-',c1))) - charindex('/',right(c1,len(c1) - charindex('-',c1)))+ 1)
from cte
If the field is ALWAYS AAAA-BB[/CCC], then simply:
SUNSTRING('AB01-52/1',6,2) ;
Of course, the 'AB01-52/1' may be substituted by a variable or column name.
I may have found an answer but it involves a 2 stage process
First add an additional slash to the column that contains the data where there is no slash at the moment ie AB01-52 becomes AB01-52/ the others like AB01-1023/01 remain the same
stage 2 use this code:
substring(right(col1,len(col1)-charindex('-',col1)),0,charindex('/',right(col1,len(col1)-charindex('-',col1))))
this allows me to get the middle string. The use of left and right by various people who contributed helped to get to this - however I still thing there should be an answer without stage 1
Thanks you all for your ideas which got me to this point
Input string: ["1189-13627273","89-13706681","118-13708388"]
Expected Output: ["14013627273","14013706681","14013708388"]
What I am trying to achieve is to replace any numbers till the '-' for each item with hard coded text like '140'
SELECT replace(value_to_replace, '-', '140')
FROM (
VALUES ('1189-13627273-77'), ('89-13706681'), ('118-13708388')
) t(value_to_replace);
check this
I found the right way to achieve that using the below regular expression.
SELECT REGEXP_REPLACE (string_to_change, '\\"[0-9]+\\-', '140')
You don't need a regexp for this, it's as easy as concatenation of 140 and the substring from - (or the second part when you split by -)
select '140'||substring('89-13706681' from position('-' in '89-13706681')+1 for 1000)
select '140'||split_part('89-13706681','-',2)
also, it's important to consider if you might have instances that don't contain - and what would be the output in this case
Use regexp_replace(text,text,text) function to do so giving the pattern to match and replacement string.
First argument is the value to be replaced, second is the POSIX regular expression and third is a replacement text.
Example
SELECT regexp_replace('1189-13627273', '.*-', '140');
Output: 14013627273
Sample data set query
SELECT regexp_replace(value_to_replace, '.*-', '140')
FROM (
VALUES ('1189-13627273'), ('89-13706681'), ('118-13708388')
) t(value_to_replace);
Caution! Pattern .*- will replace every character until it finds last occurence of - with text 140.
using SQL 2008; I have the following string:
EMCo: 1 WorkOrder: 12770 WOItem: 10
I am trying to get the WorkOrder #.
When the string did not have the WOItem on end of it, I was able to use the following statement to get WorkOrder #.
[WorkOrder] = LTRIM(RTRIM(RIGHT(HQMA.KeyString,CHARINDEX(':',REVERSE(HQMA.KeyString))-1)))
This statement moves and may have double digits for the Co#, and it does not always have WOItem #. Was hoping to find something that would split after the ":" and just take 2nd group.
Any suggestions?
The patindex suggestion above will work beautifully, now if you still want to use your current statement, substring will pull the same values, and replace will take out WOItem. Not very elegant, it works whether you have WOItem or not:
select substring(LTRIM(RTRIM(RIGHT(REPLACE(HQMA.KeyString,'WOItem:',''),
CHARINDEX(':',REVERSE(REPLACE(HQMA.KeyString,'WOItem:','')))-1))),0,7)
select substring(LTRIM(RTRIM(RIGHT(REPLACE(HQMA.KeyString,'WOItem:',''),
CHARINDEX(':',REVERSE(REPLACE(HQMA.KeyString,'WOItem:','')))-1))),0,7)
How about using patindex()? Assuming the work order always has five characters:
select substring(HQMA.KeyString,
patindex('%WorkOrder: %', HQMA.KeyString) + 11,
5) as WorkOrder
I am trying to only return the second to last character from a string using MS SQL.
I've tried using MID and Substring but the length of the string isn't always the same for the column I am trying to return, So I can't do it that way.
So say I am returning the codes of something:
Code
'1234'
I want to just return '3' from that code.
How can I do this?
Cheers in advance :)
Use SUBSTRING and LEN. LEN gives you the length of the string, then subtract 1 to get the previous char:
SELECT SUBSTRING(Code, LEN(Code)-1,1)
How about
select left(right(code, 2), 1) from MyTable;
You might need to validate that the string actually has at least 2 chars, however.
SqlFiddle here