How can I update many rows with SQL in a single table? - sql

I have a table and one of the columns holds web addresses like: 'http://...' or 'https://...'.
The problem is that there are some invalid entries, like 'shttp://...' or '#http//...' (the first character is invalid) and I want to correct all of them.
I use the following SQL statement:
'SELECT [...] FROM MyTable WHERE WebAddress LIKE '_http%'
and I successfuly get the problematic rows.
But how am I going to change/correct all of them using an UPDATE statement?
If you have some other solution please share it!

Simply change the SELECT to an UPDATE (of course, with some syntax changes) with a "fix" expression
UPDATE
MyTable
SET
WebAddress = SUBSTRING(WebAddress, 2, 8000)
WHERE
WebAddress LIKE '_http%'

You Can use Sub string property as you can trim odd letters .Also like '_word start' suitable for your question

Related

SQL query executing but not affecting row

So im having some trouble with my database right now, I can't seem to update a variable, Im just looking to do a simple "UPDATE" query, this is what it looks like:
MY table
So very simple table and all I am looking to do is update the "Sent" column from no to yes.
I've been using UPDATE but for some reason this dosn't work, no syntax error just says 0 rows affected and nothing changes...
UPDATE `numbers` SET `Sent`='True' WHERE `fName`='John';
(Im hoping to change the sent value of all rows containing fName = John.... As you can see in my table John is a value in the database so this should work but for some reason doesn't) Could anyone explain why my statement is wrong and what I am not doing right?
You need to debug this. Start with the select query:
SELECT n.*
FROM numbers n
WHERE fName = 'John';
This should return no rows -- meaning that what you see is not what you got. One common problem are invisible characters at the beginning, end, or both (often just spaces). So you can try:
WHERE fName LIKE '%John'
WHERE fName LIKE 'John%'
WHERE fName LIKE '%John%'
Once you figure out what works, you can figure out what to use in the UPDATE or how to fix the data.

trim whitespaces in oracle where condition

I'm facing the following issues when i tried to retrieve the data from oracle database by trimming the whitespace. I'm using oracle 12G version.
Let's say the name column has some whitespaces like 'Test '.
Table name - PGM_DETAIL
ID NAME
1 Test
When I'm trying to fetch the data using following query. It's not fetching it.
SELECT * FROM PGM_DETAIL WHERE TRIM(NAME) = TRIM('Test');
Could you anyone please suggest me what the problem in my sql statement.
Thanks in advance...,
Oracle comparisons are case-sensitive by default.
So try:
WHERE UPPER(TRIM(NAME)) = TRIM('TEST');
try
SELECT * FROM PGM_DETAIL WHERE TRIM(NAME) = TRIM('Test');

Error show when update using phpmyadmin

When I update a column using phpmyadmin in database with following query
UPDATE members
SET `refered` = (SELECT COUNT (*)
FROM `user_details`
WHERE `user_details.sponser`=`members.username`
)
It show a error message like this
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '*) FROM `user_details` WHERE `user_details.sponser`=`members.username`)' at line 1
What may be reason?
Error is in
COUNT (*)
-----^
Remove the space between COUNT and (*).
Try the below query
UPDATE members SET refered = (SELECT COUNT(*) FROM user_details
WHERE user_details.sponser=members.username)
Does the Select query returns any result. If so what is the result. Looks like all your query is inside '' single quotes that you are using it should be removed. Single quotes need to be removed for example .
UPDATE members
SET refered = (SELECT COUNT (*)
FROM user_details
WHERE user_details.sponser=members.username
)
-- there is not single quotes in the query above. please remove it from yours.
Part of your problem, or maybe the whole problem, is the WHERE clause. You've used backticks for the table name, which is correct (or, at least, it's optional in this case; it's needed if your database name or table name has a MySQL reserved name or is otherwise ambiguous). The problem, though, is that the dot separating the database from the table needs to be outside the backticks. So your WHERE clause should look like this instead:
WHERE `user_details`.`sponser`=`members`.`username`

Oracle Update table to set specific attribute value in semicolon separated values

I have a column where I have values like:
Email_Password+oi8hu907b;New_eMail+Y;Email_Username+iugbhijhb8
Now I want to update New_eMail attribute for all rows which has Y to N without affecting anything else.
Please advise.
i hate it but...
update table
set column = replace(column,'New_eMail+Y','New_eMail+N')
where column like '%New_eMail+Y%'
you don't need the WHERE clause but if you put a functional index on the table it may be quicker with it
Since it may be the only place in the string where '+Y;' occurs the following statement may do the trick:
update <your_table>
set <your_column> = replace(<your_column>,'+Y;','+N;')
where instr(<your_column>,'+Y;')>0
This solution differs from the others provided because it does not depend on the value of the email address.
My answer is a slight improvement over the answer from user davegreen100
Since they don't allow me to post it as a comment, I add it here.
update <<tablename>>
set <<columnname>> = replace(<<columnname>>,';New_eMail+Y;',';New_eMail+N;')
where <<columnname>> like '%;New_eMail+Y;%'

Add additional text to an existing string in SQL?

I have a SQL table with a column named "FileLink" and I need to add the domain name at the end of the server name for all the existing records in the table. So it would be like this:
Before:
\\ServerName\SharedFolder\Test.PDF
After:
\\ServerName.domain.net\SharedFolder\Test.PDF
So I need to add ".domain.net" to the link. Is there a sql statement to do this?
TIA
If you need to modify only one domain and this value is unique you can use REPLACE:
update Table
set Column = REPLACE(Column, 'ServerName', 'ServerName.domain.net')
If you don't want to use the replace statement you can do it like this:
Declare
#SrvName as varchar(50)
Set #SrvName = '\\ServerName'
Select
'\\ServerName.domain.net'+Substring(FileLink,Len(#SrvName)+1,Len(FileLink)-Len(#SrvName))
If the servername is the same for each record you could do it with the replace statement. Otherwise you might want to use patindex to find the first occurence of a '\' starting from position 3 to determin the place you need to insert the extra text.
If you want to prevent any issue if you run the query against old and new records, you should use
REPLACE(FileLink, '\\ServerName\', '\\ServerName.domain.net\')
Which gives:
SELECT
SELECT REPLACE(FileLink, '\\ServerName\', '\\ServerName.domain.net\') AS FileLinkUpdated
FROM MyTable
UPDATE
UPDATE MyTable
SET FileLink = REPLACE(FileLink, '\\ServerName\', '\\ServerName.domain.net\')
Note that this is assuming you don't have a link with only \\ServerName