Get the text between two characters in sql server - sql-server-2008

i have tried using charindex function but unable to get it.
Text is :- Offshore/BLR/IN
i want only BLR" out of this.
please help.

Presuming that what you are actually asking is how to get the text between the forward-slashes, you could do this easily using CHARINDEX.
See the following example:
DECLARE #Text varchar(100)
SET #Text = '- Offshore/BLR/IN'
PRINT CHARINDEX('/', #Text)
PRINT CHARINDEX('/', #Text, CHARINDEX('/', #Text) + 1)
This will output 11 and 15, respectively, which shows the position of the first and second forward-slash.
The second position is found by searching the text that occurs after the first position.
Following this it's just a simply case of using SUBSTRING to extract the text from the middle of the slashes:
PRINT SUBSTRING(#text, CHARINDEX('/', #text) + 1, CHARINDEX('/', #text, CHARINDEX('/', #text)+ 1) - CHARINDEX('/', #text) - 1)
This essentially gets the text between the first slash (+1 position to exclude the slash), to the position of the second slash minus the position of the first slash minus 1 (to remove the first slash).
This is basically:
PRINT SUBSTRING(#text, 11 + 1, 15 - 11 - 1)
Note that is the text does not contain both slashes then this code will fail. It contains no bounds or error checking at all, and you should look to add this.

Related

Incorrect display of TEXT in SVG

The code that generates SVG:
"<text x=\"" + (Width / 2) + "\" y=\"18\" width=\"30\" text-anchor=\"middle\" font-weight=\"Bold\" font-family=\"Simplex\" font-size=\"7\">" + "123" + "</text>"
The code is looped so it displays repeatedly
What's wrong with this text?
Conclusions:
centers the first digit
other elements start at the beginning of the element
When I remove the text-anchor, it changes a little but it still displays incorrectly
The problem is probably division by 2 (Width / 2)
You get a floating point value, which means that the separator is , instead of . in String Value
Use:
(Width / 2).ToString("0.00", CultureInfo.InvariantCulture)

SSRS Insert Space Between Numeric and Alpha Characters

I am having an issue where a field is stored in our database as '##ABC' with no space between the number and letters. The number can be anything from 1-100 and the letters can be any combination, so no consistency of beginning letter or numeric length.
I am trying to find a way to insert a space between the number and letters.
For example, '1DRM' would transform to '1 DRM'
'35PLT' would transform to '35 PLT'
Does anyone know of a way to accomplish this?
You can use regular expressions like the one below (assuming your pattern is digits-characters)
= System.Text.RegularExpressions.Regex.Replace( Fields!txt.Value, "(\d)(\D)", "$1 $2")
Unfortunately, there's no built in function to do this.
Fortunately, Visual Studio lets you create functions to help with things like this.
You can add Visual BASIC custom code by going to the Report Properties and going to the Custom Code tab.
You would just need to write some code to go through some text input character by character. If it finds a number and a letter in the next character, add a space.
Here's what I wrote in a few minutes that seems to work:
Function SpaceNumberLetter(ByVal Text1 AS String) AS String
DIM F AS INTEGER
IF LEN(Text1) < 2 THEN GOTO EndFunction
F = 1
CheckCharacter:
IF ASC(MID(Text1, F, 1)) >= 48 AND ASC(MID(Text1, F, 1)) <=57 AND ASC(MID(Text1, F + 1, 1)) >= 65 AND ASC(MID(Text1, F + 1, 1)) <=90 THEN Text1 = LEFT(Text1, F) + " " + MID(Text1, F+1, LEN(Text1))
F = F + 1
IF F < LEN(Text1) THEN GOTO CheckCharacter
EndFunction:
SpaceNumberLetter = Text1
End Function
Then you call the function from your text box expression:
=CODE.SpaceNumberLetter("56EF78GH12AB34CD")
Result:
I used text to test but you'd use your field.

Remove characters between () SQL

I need to remove specific text from a string value in SQL.
I've tried various CHARINDEX and LEN combinations but keep getting it wrong!
I have a name field which, contains names. Some of the fields have text in () added in.
Example :
Smith (formerly Jones)
I need to remove the whole section inside the brackets. as well as the brackets themselves. Unfortunately sometimes the value can be
Smith (formerly Jones) Reeves
So I can't just remove everything from the ( onwards!
Here are two examples how to accomplish this. You can do this without declaring the #StartIndex and #EndIndex variables, but I have used them for the sake of clarity.
DECLARE #StartIndex int, #EndIndex int;
DECLARE #Str varchar(100);
SET #Str = 'This is a (sentence to use for a) test';
SELECT #StartIndex = CHARINDEX('(', #Str, 0), #EndIndex = CHARINDEX(')', #Str, 0);
SELECT SUBSTRING(#Str, 0, #StartIndex) + SUBSTRING(#Str, #EndIndex + 1, LEN(#Str) - #EndIndex) AS [Method1],
LEFT(#Str, #StartIndex - 1) + RIGHT(#Str, LEN(#Str) - #EndIndex) AS [Method2];
Note that this code does not remove the spaces before or after the parentheses, so you end up with two spaces between "a" and "test" (since that wasn't part of your question).
Additional error checking should be included before actually using code like this as well, for example if #Str does not contain parentheses it would cause an error.

Find the word and replace with html tag using regex

I have a text equation like: 10x^2-8y^2-7k^4=0.
How can I find the ^ and replace it with <sup>2</sup> in the whole string using regex. The result should be like:
I tried str = str.replace(/\^\s/g, "<sup>$1</sup> ") but I’m not getting the expected result.
Any ideas that can help to solve my problem?
I think you're looking for something like
\^(\d+)
It matches the ^, captures the exponent and replace with
<sup>$1</sup>
See it here at regex101.
Edit:
To meet your new demands, check this fiddle. It handles the sub as well using replace with a function.
Your current pattern matches a caret followed by a space character (space, tab, new-line, etc.), but you want to match a caret followed by a single character or multiple characters wrapped in accolades, as your string is in TeX.
/\^(?:([\w\d])|\{([\w\d]{2,})\})/g
Now, using str = str.replace(/\^(?:([\w\d])|\{([\w\d]{2,})\})/g, "<sup>$1</sup>"); should do the job.
You can make a more generic function from this expression that can wrap characters prefixed by a specific character with a specific tag.
function wrapPrefixed(string, prefix, tagName) {
return string.replace(new RegExp("\\" + prefix + "(?:([\\w\\d])|\\{([\\w\\d]{2,})\\})"), "<" + tagname + ">$1</" + tagname + ">");
}
For instance, calling wrapPrefixed("1_2 + 4_{3+2}", "_", "sub"); results in 1<sub>2</sub> + 4<sub>3+2</sub>.

Cut off text character 'IIf' + 'Left' (?)

Been struggling with a formula in a calculated field to cut off the last character IF it is a text, otherwise the return should be itself.
(I think the 'iif' and the 'left' formulas could be used)
Should be simple, but some help would be appreciated!
Untested but should get you headed in the right direction. Try: =IIf(IsNumeric(Right([YourField],1), [YourField],Left ([YourField], Len([YourField])-1))
Translation:
If the last character Is a digit Then
return the field
Else
Return the field minus the last character
End If
That could be:
ValidatedNumber = Left([CheckNumber], Len([CheckNumber]) - 1 + Abs(IsNumeric(Right([CheckNumber], 1))))