SQL Statement to UPDATE three characters in a string - sql

How to take a string from a row in a column named 'link', and modify it by adding three letters to a specific index position in the string.
Specific example:
I want to select the value 'http://www.hello.no' and UPDATE it with 'http://www-x1.hello.no' using SQL statement(s).
Lets say the index position where '-x1' starts at will always be 10.
This needs to be accomplished using PostgreSQL. But if you can capture the logic with a generic SQL statement then great. :)

Postgresql has a function for doing replacements with patterns called regexp_replace. You can use that function like this:
UPDATE my_table
SET link = regexp_replace(link, 'www', 'www-x1')
WHERE <...>
Of course you could do it with the straight string manipulation, too:
UPDATE my_table
SET link = left(link, 10) || '-x1' || substring(link from 10)
WHERE <...>

This does what you ask for:
update the_table
set the_column = left(the_column, 10) || '-x1' || substring(the_column, 10);
however I'm not sure that this is what you want. It seems you want to insert the '-x1' in front of the first . which would be something different.

Related

How to replace the first letter of a string using SQL and Postgres?

I have a database column of varchar(191) with strings in the database. We need to replace the first letter of every string with an "E". So for instance, we have:
Cuohvi-AQNqalPq8zdr1cOA
Needs to be changed to
Euohvi-AQNqalPq8zdr1cOA
Do you know how we can achieve this in Postgres with a SQL query? It needs to be updated for the whole table.
Per docs use overlay():
UPDATE the_table SET the_field = overlay(the_field placing 'E' from 1 for 1);
Use a combination of the CONCAT function and the RIGHT function with an argument of -1.
SELECT CONCAT('E', RIGHT('Cuohvi-AQNqalPq8zdr1cOA', - 1))
FROM yourtable
SELECT CONCAT('E', RIGHT(yourfield, - 1))
FROM yourtable
dbfiddle: https://dbfiddle.uk/?rdbms=postgres_9.6&fiddle=e198b05f02283137afc39c24bb6c788d

How to replace sql field case-sensitive value

I have one case-sensitive column.
How can I replace (Shpersonel,shPersonel, sHperSonel) to ShPersonel in all records?
my column Data is:
1- SHpersonel = 12 And Password= Ayuy122
2- ShpeRsonel = 10 And Password= jkjIUD122
3- ShPersonel = 08 And Password= Kjjam
...
You could use LOWER (or UPPER), which lots of DBMSes support, to look for a value case insensitively and update:
update your_table
set col = 'ShPersonel'
where lower(col) = 'shpersonel';
EDIT:
Further clarification explained that above data is stored in a single column - Which is not how it should be stored.
You should store the data in separate columns and then look for further normalization to remove unwanted dependencies.
UPDATE your_table
SET your_column = 'ShPersonel'
WHERE upper(your_column) = 'SHPERSONEL';
You didn't say what DBMS you're using. If it's SQL-Server, the following should work for you:
update MyTable
set TheData =
concat(
left(TheData,charindex('shpersonel',lower(t.TheData))-1),
'ShPersonel',
right(t.TheData,len(t.TheData) - charindex('shpersonel',lower(t.TheData)) - len('ShPersonel'))
)
from MyTable t
where
charindex('shpersonel',lower(t.TheData))>0
For other DBMS's you need to replace charindex with whatever that DBMS's equivalent may be. For example, Oracle and MySQL use InStr (which needs its arguments in the reverse order).

How to replace where clause dynamically in query (BIRT)?

In my report query I have a where clause that needs to be replaced dynamically based on the data chosen in the front end.
The query is something like :
where ?=?
I already have a code to replace the value - I created report parameter and linked to the value ? in the query.
Example:
where name=?
Any value of name that comes from front end replaces the ? in the where clause - this works fine.
But now I need to replace the entire clause (where ?=?). Should I create two parameters and link them to both the '?' ?
No, unfortunately most database engines do not allow to use a query parameter for handling a dynamic column name. This is for security considerations.
So you need to keep an arbitrary column name in the query:
where name=?
And then in "beforeOpen" script of the dataset replace 'name' with a report parameter value:
this.queryText=this.queryText.replace("name",params["myparameter"].value);
To prevent SQLIA i recommend to test the value of the parameter in this script. There are many ways to do this but a white list is the strongest test, for example:
var column=params["myparameter"].value;
if (column=="name" || column=="id" || column=="account" || column=="mycolumnname"){
this.queryText=this.queryText.replace("name",column);
}
In addition to Dominique's answer and your comment, then you'll just need a slightly more advanced logic.
For example, you could name your dynamic column-name-value pairs (column1, value1), (column2, value2) and so on. In the static text of the query, make sure to have bind variables for value1, value2 and so on (for example, with Oracle SQL, using the syntax
with params as (
select :value1 as value1,
:value2 as value2 ...
from dual
)
select ...
from params, my_table
where 1=1
and ... static conditions....
Then, in the beforeOpen script, append conditions to the query text in a loop as needed (the loop left as an exercise to the reader, and don't forget checking the column names for security reasons!):
this.queryText += " and " + column_name[i] + "= params.value" + i;
This way you can still use bind variables for the comparison values.

Change string with sql UPDATE query

Question
How could I change the following code from SGC1-0001[ to SGC1-001.
The table is stations with column code.
I need to use an update query, but not sure how to use it.
There are more records in the column that differ slightly but I should be able to work it out with an example to do one.
Sample Data
I would like to do them in chunks though not one at a time.
SGC1-0001[
SGC1-0002[
..
SGC1-0019[
SGC1-0021[
SGC2-0001[
SGC2-0002[
..
SGC2-0016[
SGC2-0017[
SGC3-0003[
SGC3-0004[
...
SGC4-0018[
SGC4-0021[
SGC4-0022[
SGC4-0025[
SGC4-0029[
Logic (pseudo code)
Delete first 0 and last [ from all
If you're wanting to trim the last character of all records in that column as well as remove the leading 0, the following should do the trick:
UPDATE stations
SET code = REPLACE(LEFT(code, LEN(code) - 1), '-0', '-')
If you're just wanting to replace the leading 0, following should do:
UPDATE stations
SET code = REPLACE(code, '-0', '-')
Also, the following may be of interest:
String Functions
Try This, Update query
Because all your code have '[' at last then you can use this query. it will delete the last '[' and first 0 after '-' from all records
UPDATE stations
SET CODE = LEFT(replace(code,right(code,1),''),4) + '-'+
RIGHT(RIGHT(replace(code,right(code,1),''),4),3)
It will replace
SGC1-0029[ to SGC1-029 AND
SGC1-0001[ to SGC1-001
Better to refer some sql basic tutorials

How to update text using "regular expressions" in SQL Server?

In a column in a SQL Server database table, the value has a format of X=****;Y=****;Z=5****, where the asterisks represent strings of any lengths and of any values. What I need to do is to change that 5 to a 4 and keep the rest of the string unchanged.
Is there a way to use something like regular expressions to achieve what I want to do? If not using regular expressions, can it be done at all?
MS SQL sadly doesn't have any built in regex support (although it can be added via CLR) but if the format is fixed so that the part you want to change isZ=5toZ=4then usingREPLACEshould work:
REPLACE(your_string,'Z=5','Z=4')
For example:
declare #t table (str varchar(max))
insert #t values
('X=****;Y=****;Z=5****'),
('X=****;Y=**df**;Z=3**sdf**'),
('X=11**;Y=**sdfdf**;Z=5**')
update #t
set str = replace(str,'Z=5','Z=4')
-- or a slightly more ANSI compliant and portable way
update #t
set str = SUBSTRING(str,0, CHARINDEX('Z=5', str)) + 'Z=4' + SUBSTRING(str, CHARINDEX('Z=5', str)+3,LEN(str))
select * from #t
str:
X=****;Y=****;Z=4****
X=****;Y=**df**;Z=3**sdf**
X=11**;Y=**sdfdf**;Z=4**
We need more information. Under what circumstances should 5 be replaced by 4? If it's just where it occurs as the first character after the Z=, then you could simply do...
set Col = Replace(Col,'Z=5','Z=4')
Or, do you just want to replace 5 with 4 anywhere in the column value. In which case you'd obviously just do...
set Col = Replace(Col,'5','4')
Or possibly you mean that 5's should be replaced by 4's anywhere within the value after Z= which would be a lot harder.
update Table set Field = replace(Field, ';Z=5', ';Z=4')
And let's hope that your asterisked data doesn't contain semicolons and equality signs...