Find and replace string in main string - tsql

I'm trying to find a string in a main string and remove it.
Example:
#Main_text = 'some text some text... // remove text remove text \\ some text some text'
What I want is to remove the following text:
// remove string remove string \\ of the main text
What I tried
declare #main_text varchar(255) = 'some text some text... // remove text remove text \\ some text some text'
SELECT STUFF(#main_text,
charindex('//', #main_text),
charindex('\\', #main_text) ,
'');
This partly works. it removes the searched text but also remove the end of the text.

The third parameter to STUFF is the number of characters, from the starting point in the second parameter, to replace.
SELECT
STUFF(#main_text,
CHARINDEX('//', #main_text),
CHARINDEX('\\', #main_text) - charindex('//', #main_text) + 2,
'')
FROM yourTable
WHERE #main_text LIKE '%//%\\%'b
Demo
text // blah \\
6 14
We want to remove blah and remove the markers. The difference in marker positions returned from CHARINDEX is 8, but we want to remove 10 characters, hence the +2 in the STUFF call.
We may use a WHERE clause to restrict the query from only targeting records having the replacement markers. You could put the above query into a CTE, and then update it.

Related

how to remove certain alphanumeric characters from a specific column

I have a column that displays certain data in the form of Y14H1050101P01T01.
I want to remove the first 4 alphanumeric characters and the last 6 alphanumeric characters so it reads 1050101 instead.
How do I go about doing this?
We can use a regex replacement here:
SELECT col, REGEXP_REPLACE(col, '^[A-Z0-9]{4}|[A-Z0-9]{6}$', '', 'g') AS col_out
FROM yourTable;
Demo
If you want the change to stick, then use an update:
UPDATE yourTable
SET col = REGEXP_REPLACE(col, '^[A-Z0-9]{4}|[A-Z0-9]{6}$', '', 'g')
WHERE col ~* '^[A-Z0-9]{4}.*[A-Z0-9]{6}$';

How to break line into jsonb array element?

I have a table:
create table table_name
(id varchar(255), details jsonb)
Then i need to update some array into "details"
update table_name
set details = '["firstLineSecondLine", "ThirdLineFourthLine"]'::jsonb
where id = '1';
How can i break lines between "firstLineSecondLine"?
I need thst my body be like:
"details": [
"firstLine
SecondLine",
"ThirdLine
FourthLine"
]
Any ideas?
literal newlines inside quotes in JSON are represented by \n. They are represented this way both on input and on output, so you will not able to get it to show up the way you want to other than converting to plain text, or something else.
Also, your example is invalid for another reason, it is lacking the outer curlies.

How to have a cell display the table's name in LibreOffice Calc?

So, basically I want to have a cell show the table's name it is in. I figured out how to get the table ID, starting with 1, but no idea how to get the name of it.
AFAIK, you can't get the name directly. But you could use the CELL function with its filename parameter to get a string containing path, filename and table name of the current cell. Using that string, you can extract the table name as follows:
=RIGHT(CELL("filename");LEN(CELL("filename"))-FIND("$";CELL("filename")))
Split on multiple lines:
=RIGHT( # return substring from the right
CELL("filename"); # of the filename (incl. table name)
LEN( # calculate the length of the table name substring:
CELL("filename") # take the complete filename string;
) - # and subtract ...
FIND( # the position...
"$"; # of the dollar sign (preceding the table name)
CELL("filename") # of the "filename" string
)
)
inspired by a OOo forum post from villeroy
Depending on your localization, you may have to replace the semicolons ; by commas ,.

Pulling variable length substring from middle of string

I am trying to grab variable length string from a primary string.
Example:
ABC*12*1*name name****XX*123456789~
ABC*12*1*diffname diffname****XX*234567890~
ABC*12*1*diffname2 diffname2***XX*345678901~
I need to pull out the 'name name', 'diffname diffname', 'diffname2 diffname2'
etc from the string. And then replace the ' ' between the names with an asterisk - but, I cant just insert in the first space in the string, there could be multiple names, and so I would want to insert the '*' into the second, or third space, depending on the length of the name string.
SELECT
CHARINDEX('*1*',data)+3 AS startpos,
CHARINDEX('***',data) AS Endpos,
data
from #t
where data like '%ABC*12*1*%'
This gives me a start point and end point for the variable length string. So I try:
SELECT SUBSTRING(data,CHARINDEX('*1*',data)+3,CHARINDEX('***',data) -CHARINDEX('*1*',data)+3)
FROM #t
WHERE data like '%ABC*12*1*name%'
But this gives me
name n name aa*****X
as a result set, basically starting at the start point and then running well past the end point.
What am I doing wrong?
This part is the problem :
SELECT .....-CHARINDEX('*1*',data)+3
FROM .....
WHERE .....
You want to substract with Endpos so it supposed to be written in brackets like so :
-(CHARINDEX('*1*',data)+3)
and if the brackets are removed the last part should become -3 :
-CHARINDEX('*1*',data)-3

PgSQL trim whole string at end, not every characters

i'm trying to rebuild relations in my DB. I need to "repair" some strings that are stored badly in my table named city_list.
The data:
"Berlin"
"London "
"Kijev&nbsp"
"Poznan&nbsp"
I used pgsql function rtrim(string text [, characters text]) in that way:
UPDATE city_list SET city_name=RTrim(city_name);
UPDATE city_list SET city_name=RTrim(city_name, ' ');
Now I have:
"Berlin"
"Londo"
"Kijev"
"Pozna"
Is there way to force rtrim to cut whole " " string from end not every single characters?
Use regexp_replace().
The trim() function's second argument is the full list of chars to be trimmed.