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

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

Related

Sql query to add Zero between string with character and digits

What will be the generic sql query to update id by adding zeros in id. containing alphabets as well as numbers? In between the character and digits 00 needs to be padded e.g QTX23675 should turn into QTX0023675
UPDATE Table
SET QID = 'QTX0023675'
WHERE QID='QTX23675';
There are many records to update and i found query with leading zeros.
Tried with spliting the string.
In most databases, you could do something like this:
UPDATE Table
SET QID = CONCAT(LEFT('QTX0023675', 3), '00', RIGHT('QTX0023675', 5))
WHERE QID LIKE '________';
In any given database, there might be somewhat simpler methods, but the idea is the same.
Try This to make the result Dynamic
WITH CTE
AS
(
SELECT Val = 'XTP231'
)
SELECT
*,
NewVal = SUBSTRING(Val,1,PATINDEX('%[0-9]%',Val)-1)+'00'+SUBSTRING(Val,PATINDEX('%[0-9]%',Val),LEN(Val))
FROM CTE
THe Output is as follows
UPDATE TABLE SET QID= STUFF(QID,PATINDEX('%[0-9]%',QID),0,'00')

Translate function not returning relevant string in amazon redshift

I am trying to use a simple Translate function to replace "-" in a 23 digit string. The example of one such string is "1049477-1623095-2412303" The expected outcome of my query should be 104947716230952412303
The list of all "1049477-1623095-2412303" is present in a single column "table1". The name of the column is "data"
My query is
Select TRANSLATE(t.data, '-', '')
from table1 as t
However, it is returning 104947716230952000000 as the output.
At first, I thought it is an overflow error since the resulting integer is 20 digit so I also tried to use following
SELECT CAST(TRANSLATE(t.data,'-','') AS VARCHAR)
from table1 as t
but this is not working as well.
Please suggest a way so that I could have my desirable output
This is too long for a comment.
This code:
select translate('1049477-1623095-2412303', '-', '')
is going to return:
'104947716230952412303'
The return value is a string, not a number.
There is no way that it can return '104947716230952000000'. I could only imagine that happening if somehow the value is being converted to a numeric or bigint type.
Try regexp_replace()
Taking your own example, execute:
select regexp_replace('[string / column_name]','-');
It can be achieve RPAD try below code.
SELECT RPAD(TRANSLATE(CAST(t.data as VARCHAR),'-','') ,20,'00000000000000000000')

Remove string in SQL Server

In my table Products in SQL Server, I have a column UrlLink with values that look like this:
Id UrlLink
-----------------------------------
1 domain/product1.html?7
2 domain/product2.html?34
3 domain/product294.html?6576
4 domain/product54.html?765
How to remove parameter
?7, ?34, ?6576, ?765
from column UrlLink?
Thanks!
To remove the query string part from the UrlLink column in the table, you need to use Left and CharIndex in your UPDATE statement.
UPDATE Products
SET UrlLink = LEFT(UrlLink, CHARINDEX('?',UrlLink)-1)
Using left and charindex should work:
select left(UrlLink, charindex('?',UrlLink)-1) from Products;
This would return everything before the first occurrence of a ?. You might want to add some null checks if parameter isn't mandatory in the UrlLink column.
Try with this code:
DECLARE #myvar varchar(100);
SET #myvar = 'domain/product1.html?7';
SELECT REVERSE(SUBSTRING((REVERSE(#myvar)), (CHARINDEX('?',REVERSE(#myvar))+1),500)) AS result ;
GO
Simple and won't fail in case you have a Urllink that does not contain?.
In case of multiple ? removes everything from the first ? since in URL this is the only ? with special significance.
select id,left(Urllink,charindex('?',Urllink+'?')-1)
from Products
You just have to find ? in your url and take string upto it. You can use LEFT/SUBSTRING for getting substring and CHARINDEX for finding ? in your string. Check out my query below
select id, substring(urllink,1,charindex('?',urllink)-1)
from products
In case multiple ? symbols are there in the UrlLink column values and if you want to take the value after the last ? symbol. Then use a combination of SUBSTRING and CHARINDEX .
Query
SELECT
[Id],
SUBSTRING([UrlLink], 1,
LEN([UrlLink]) - CHARINDEX('?', REVERSE([UrlLink]), 1)) AS [UrlLink]
FROM [Products];
Demo

SQL Server: How to select rows which contain value comprising of only one digit

I am trying to write a SQL query that only returns rows where a specific column (let's say 'amount' column) contains numbers comprising of only one digit, e.g. only '1's (1111111...) or only '2's (2222222...), etc.
In addition, 'amount' column contains numbers with decimal points as well and these kind of values should also be returned, e.g. 1111.11, 2222.22, etc
If you want to make the query generic that you don't have to specify each possible digit you could change the where to the following:
WHERE LEN(REPLACE(REPLACE(amount,LEFT(amount,1),''),'.','') = 0
This will always use the first digit as comparison for the rest of the string
If you are using SQL Server, then you can try this script:
SELECT *
FROM (
SELECT CAST(amount AS VARCHAR(30)) AS amount
FROM TableName
)t
WHERE LEN(REPLACE(REPLACE(amount,'1',''),'.','') = 0 OR
LEN(REPLACE(REPLACE(amount,'2',''),'.','') = 0
I tried like this in place of 1111111 replace with column name:
Select replace(Str(1111111, 12, 2),0,left(11111,1))

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.