I want to change the string of AttachmentCopyLoc columns from D:\IT\Public\FTX_RobotAlerts\336 to V:\IT\Public\FTX_RobotAlerts\336 only change here is D to V, the remaining string is the same (I don't event want to change that).
How can I do that?
Thanks in advance for your help.
The exact syntax depends on the platform, but it would be something akin to
UPDATE {table}
SET AttachmentCopyLoc = REPLACE ( AttachmentCopyLoc , 'D:' , 'V:' )
WHERE AttachmentCopyLoc LIKE 'D:%'
In SQL Server, I would recommend using stuff():
update t
set AttachmentCopyLoc = stuff(AttachmentCopyLoc, 1, 1, 'V')
where AttachmentCopyLoc like 'D:%';
This version has two advantages:
The use of STUFF() ensures that only the first occurrence of 'D:' is replaced. Admittedly, it is unlikely that this substring occurs more than once in the column, but why take the chance?
The use of LIKE allows an index on AttachmentCopyLoc to be used, if one is available and use of hte index is appropriate.
Another way would be basic string manipulation
UPDATE {table}
SET AttachmentCopyLoc ='V' + substring(AttachmentCopyLoc,2,len(AttachmentCopyLoc ))
where AttachmentCopyLoc like 'D%'
This will only alter the first letter of the string:
update <tablename>
set AttachmentCopyLoc = 'V'+substring(AttachmentCopyLoc,2,len(AttachmentCopyLoc)-1)
Related
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).
I am trying to write an update query that will remove the last character if it is a period ("."). In EXCEL, I would use the following statement:
=IF(RIGHT(A1,1)=".",LEFT(A1,LEN(A1)-1),A1)
How can I modify this for ACCESS? I found the following statement in a forum, but ACCESS seems to have a problem with "Substring" and won't let me run the query.
UPDATE table SET field = SUBSTRING(field, 1, CHAR_LENGTH(field) - 1)WHERE field LIKE '%.'
Any thoughts?
I think the right way to do this in Access is:
UPDATE table
SET field = LEFT(field, LEN(field) - 1)
WHERE field LIKE '*.' ;
Note that the like wildcards are different in MS Access.
You could simply create a substring that was one character shorter than your existing string if it ended with a period via the LEFT() function :
UPDATE YourTable
SET YourColumn = LEFT(YourColumn, LEN(YourColumn - 1))
WHERE YourColumn LIKE '*.'
Could anyone help me with a command on SQL to update a column as this example bellow.
IP : 192.168.010.001
I need to remove zero and update column to be like this:
IP : 192.168.10.1
If you need to remove the last four characters, then most databases support LEFT():
update table t
set col = left(col, length(col) - 4);
The name of the length() function also differs among databases (typically either len() or length()). And substr()/substring() could be used instead of left().
EDIT:
Ahhh, Jeffrey pointed out that the above is an incorrect interpretation. I misread the numbers. There is a method to do this, which is pretty database independent. Generally, there will not be more than two zeros after a period (if there are three, then you probably want the third). So:
update table t
set ip = replace(replace(ip, '.0', '.'), '.0', '.')
where ip like '%.0%';
which Database you are using , if you are using Sybase you can try str_replace and use to replace pattern like '.0' or '.00' to '' .
Query could be something like this
`update X set Y=str_replace(Y,'.0','.') where Y like '%.0%' `
and
`update X set Y=str_replace(Y,'.00','.') where Y like '%.00%' `
A simple replace twice will also work:
replace(replace('192.168.010.001','.0','.'),'.0','.')
or
IP_Field = replace(replace(IP_Field,'.0','.'),'.0','.')
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...
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.