In my work, I am stuck with a SQL problem.
I have a field in one of my table which contains strings of the form "%_abc". I want to update this column by removing "_abc" at end of every entry. Is there a nice way to get this done using SQL?
Thanks,
Anil.
update table1 set field1 = substr(field,1,length(field1)-4) where ...
HTH
If your database is ANSI SQL-92 compliant, you can use:
UPDATE myTable SET myColumn = TRIM(trailing '_abc' FROM myColumn);
Related
Is this possible in one SQL statement?
I pointer to a nice SQL tutorial on the subject is also appreciated.
I know I can use a command like SELECT * FROM MYTAB WHERE MYFIELD = 0
and then use a server script to go through the result and UPDATE MYFIELD
of the result rows.
Wouldn't this work?
UPDATE MYTAB SET MYFIELD=1 WHERE MYFIELD=0
Edited to Add:
If you are interested in SQL tutorials, a lightweight one that has the nice feature of allowing you edit and run SQL commands in the webpage to see how they work is from w3schools here: http://www.w3schools.com/sql/
If you can execute a query to read the data you should also be able to write the data;
UPDATE MYTAB SET MYFIELD = 99 WHERE MYFIELD = 0
UPDATE (Transact-SQL)
UPDATE Statement (Oracle)
UPDATE Syntax (MySQL)
UPDATE Statement (PostgreSQL)
UPDATE query..
UPDATE TABLE
SET FIELD = 1
WHERE FIELD = 0
I think it should be as simple as:
UPDATE MYTAB
SET MYFIELD = 1
WHERE MYFIELD = 0
Hope this helps!
Why do you want to execute a SELECT query when your purpose can be solved by a simple UPDATE query? It seems that you want to switch the flag column of your table.
how to select all records with field set to 0 and set the value of
that field to one
Use below query.
Update MYTAB
set MYFIELD=1
Where MYFIELD=0
This query will find all records where MYFIELD is set to 0 and will update all those records with value 1.
You can browse thru following links to learn advance SQL.
SQLCourse2.com
SQL Tutorial
Tutorials Point
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;%'
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
I want to update a column value. But my Update procedure statement updates even the value of the this column is the same.
UPDATE TableName
SET ColumName=#ParameterName
WHERE Id=#ParameterId
Any Idea?
Thank you.
From what I get from your post, you are wondering why MySQL is updating even when the value is not changed. Well, you don't check beforehand if the value is really different, so that's just what an update statement with an ID does...
You can add additional condition AND (COALESCE(ColumName,'')<>COALESCE(#ParameterName,'')
so you're only updating when those are different.
UPDATE TableName
SET ColumName=#ParameterName
WHERE (Id=#ParameterId) AND (COALESCE(ColumName,'')<>COALESCE(#ParameterName,''))
The coalsece in my example assumes that ColumnName is of type varchar if is a numeric value use AND (COALESCE(ColumName,0)<>COALESCE(#ParameterName,0) instead.
I want to Select a column and set it equal to 0. In SQL I just do this: SELECT Dealer_Fee = 0.
Do I have to use an update? When I try the same thing in Oracle I get "FROM keyword not found where expected."
I am not sure what do you want to do and in which SQL dialect the construction you mentioned works. If you just want to retrieve some value and place it in a named column in Oracle you have to use DUAL table. Try this:
SELECT 0 AS dealer_fee FROM dual;
On the other hand, if you ment T-SQL and placing a value into variable you need to use PL/SQL SELECT INTO clause, like that:
SELECT 0 INTO dealer_fee from dual;
If not try to explain in more detail what are you trying to achieve.
1) In Oracle you must specify FROM clause within SELECT, is not like MS sqlserver where you can omit a FROM clause.
2) If you want to update one specific value, you must use UPDATE clause instead SELECT.
hth
You should use the Update statement. Please visit this oracle reference which explains the Update statement in detail: Update Statement
Take a look at the reference for UPDATE in Oracle:
UPDATE <table_name>
SET <column_name> = <value>