Removing last character in ACCESS if it is "." - sql

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 '*.'

Related

Regex to find a pattern using sql

I have a column called 'user_details' in a SQL table 'customers' with the below value in it:
{4:"2021-06-07T16:17:26.327+02:00",5:"1623075805735.phna3uyo",6:"www.abc.com/connexion",10:"loggedOut",12:"567879",2:"1026530505.1619610156",3:"event"}
{4:"2021-06-01T13:11:34.742+02:00",5:"1622545894742.ml2cyuw",6:"www.seigneuriegauthier.com/connexion",10:"loggedOut",12:"",2:"435305774.1622545085",3:"event"}
{4:"2021-06-01T10:13:30.85+02:00",5:"1622535210085.vlowlxmj",6:"www.seigneuriegauthier.com/connexion",10:"loggedOut",12:"278356",2:"1381684281.1622534907",3:"event"}
{4:"2021-06-01T10:24:51.808+02:00",5:"1622536405142.h45exkgg",6:"seigneuriegauthier.com/connexion",10:"loggedOut",12:"251666",2:"1019448131.1621925108",3:"event"}
{4:"2021-06-01T14:13:54.476+02:00",5:"1622551449049.k14838ij",6:"www.seigneuriegauthier.com/connexion",10:"loggedOut",12:"601322",2:"1975087820.1622548509",3:"event"}
I'm trying to extract the id after number 12:" without the "" i.e. 567879,278356 etc into another column.
Since there are many reputative "" I'm unable to build the regex expression.
I tried the below but didn't get the exact match
(?<=:)"[0-9][0-9][0-9][0-9][0-9][0-9]
How can I write a SQL query to retrieve this. Pls help.
On SQL Server, with no regex, support, we can try using the base string functions SUBSTRING along with CHARINDEX:
SELECT
user_details,
SUBSTRING(user_details,
CHARINDEX('12:"', user_details) + 4,
CHARINDEX('"', user_details, CHARINDEX('12:"', user_details) + 4) -
CHARINDEX('12:"', user_details) - 4) AS val
FROM customers;
Demo

SQL update for the varchar column

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)

update table removing some data

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','.')

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

SQL Statement to UPDATE three characters in a string

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.