How to reverse data of a column into another column in MySql? - sql

I need to run a script to update a field of a table in mysql by reversing the string stored in another field of the same table. Original column is term and the one to be filled with the reversed is revTerm. I use this, but it produce an error. Any suggestions?
UPDATE `tu_cla_terms` WHERE tId = '11583' SET revTerm = REVERSE(term)

You have the syntax of an UPDATE query wrong. SET comes before WHERE:
UPDATE `tu_cla_terms` SET revTerm = REVERSE(term) WHERE tId = '11583'
If tId is a numeric column you don't want quotes around its value either.

Related

Adding or removing columns in result set of sql query depending on value of other field

Assume I have a query that returns a result set of columns A and B from table First_Table. I want to limit the result set to those columns if the value of column X in table Second_Table is 0, and I want to add column C from table First_Table if the value of column X is 1.
The problem is easily resolved using a Python for example whereby I just have a variable as an empty string if value in column X is 0 or it would be equal to the string 'First_Table.ColumnC AS [Dynamic Value],', and I just format the sql in the script accordingly.
If Else solution is not an elegant way because I have multiple columns to add dynamically depending on multiple values...
I am just looking for some ideas on directions.. I have been looking at this for a while, might be bogged up
Dynamic sql is the best way to resolve this as suggested in the comments.

Casting a string as decimal in PSQL

I'm trying to convert a string to a decimal using this command:
SELECT cast(minimum_ticket_price AS DECIMAL(6,2)
FROM all_event_details
WHERE minimum_ticket_price ~ E'^\\d+$';
But this doesn't actually update anything in my database. It just displays the selected column in my terminal. Do I need to combine the select with an update? I've tried that but I must have the syntax wrong as I'm not able to get the conversion saved in the database.
Here's what I tried:
UPDATE all_event_details
SET minimum_ticket_price = cast(minimum_ticket_price AS DECIMAL(6,2))
WHERE ( minimum_ticket_price <> '') IS TRUE;;
Updating to a data type which minimum_ticket_price column's data can support is possible otherwise it will give an error.
for example if minimum_ticket_price column data type is varchar then your code must work.
What are you doing?
First add new column, decimal(but I'm goint to suggest to use the basics data type such real or double precision, are most efficient)
ALTER TABLE my_table ADD COLUMN minimum_ticket_priceR real ;
than
UPDATE all_event_details
SET minimum_ticket_priceR = to_number(coalesce(minimum_ticket_price,"0"),"999999D99") --for each row
than I'm going to suggest to drop the colum minimum_ticket_price and rename the other column (ever with ALTER TABLE):
What you did is not to understand, if minimum_ticket_price is string, you cannot set a number... and if is a number has no meaning to set it as string

UPDATE column with VARCHAR(2) 4000 byte value

I want to update a table with complex entries in a column and just a clause in the column without effecting other values.
Want to update #Rule number=0# to #Rule number=1# but without affecting other values in the column. Is this possible?
Are you looking for replace()? If dynamic_attributes is a string:
update t
set dynamic_attributes = replace(dynamic_attributes,
'#Rule number=0#',
'#Rule number=1#'
)
where dynamic_attributes like '%#Rule number=0#%';
Note: Strings may not be the best way to store such a list. You should consider a table with one row per customer_id and dynamic attribute.
Here it is:
UPDATE A
SET A.dynamic_attributes = REPLACE(A.dynamic_attributes,'#Rule number=0#','#Rule number=1#')
FROM yourtable AS A
WHERE A.dynamic_attributes LIKE '%#Rule number=0#%'

SQL - UPDATE / SET statement query

I wanted to update 1 column from a table present in a database maintained in SQL 2008 R2.
The entire column contains same value of data type int and I want to change to different value of same int data type.
When do we use single apostrophe in SET statement & when to avoid or not use it?
UPDATE TABLENAME
SET COLUMNNAME = X
OR
UPDATE TABLENAME
SET COLUMNNAME = 'X'
Appreciate any suggestions/advise. Thanks.
In SQL, single quotes are used around string values. Other "values", like integers, references to other columns etc, should never be single quoted.

Update A multi-valued field in Access

I have created a lookup table in Access to provide the possible values for a column. Now I need to update this column with the data it had before I converted the column. I am unable to figure out a SQL Query that will work. I keep getting the error "An UPDATE or DELETE query cannot contain a multi-valued field." My research has suggested that I just need to set the value of the column but this always updates 0 records:
UPDATE [table_name] SET [column_name].Value = 55 WHERE [table_name].ID = 16;
I know this query will work if I change it to update a text column, so it is definitely a problem with just this column.
If you're adding a value to your multi-valued field, use an append query.
INSERT INTO table_name( [column_name].Value )
VALUES (55)
WHERE ID = 16;
If you want to change one particular value which exists in your multi-valued field, use an UPDATE statement. For example, to change the 55 to 56 ...
UPDATE [table_name]
SET [column_name].Value = 56
WHERE [column_name].Value = 55 And ID = 16;
See Using multivalued fields in queries for more information.
I have figured this out! It certainly was counter-intuitive! You have to use an INSERT statement to do the update.
-- Update a record with a multi-valued field that has no value
INSERT INTO [table_name] ( [[column_name].[Value] )
VALUES(55)
WHERE [table_name].ID = 16;
This confused me because I was expecting an UPDATE statement. I think it actually inserts a record into a hidden table that is used to associate multiple values with this column.
I am working with Sharepoint, I created the tables as multi-value fields, ran into the error with my INSERT INTO statement, went back to Sharepoint to change to non-multi-value fields, but that didn't fix it.
Recreated the table without using multi-value fields, and the INSERT INTO worked just fine.
do not use the .value part
UPDATE [table_name] SET [column_name] = 55 WHERE [table_name].ID = 16;
INSERT INTO Quals (cTypes.[value])
SELECT Quals_ContractTypes.ContractType
FROM Quals_ContractTypes
WHERE (Quals.ID = Quals_ContractTypes.ID_Quals);
I gotta say I didn't understand very well your problem but I saw something strange in your query. Try this:
UPDATE [table_name] SET [column_name]= 55 WHERE [table_name].ID = 16;
UPDATE:
Look at this link: it has an example
UPDATE Issues
SET Issues.AssignedTo.Value = 10
WHERE (((Issues.AssignedTo.Value)=6)
AND ((Issues.ID)=8));
NOTES
You should always include a WHERE
clause that identifies only the
records that you want to update.
Otherwise, you will update records
that you did not intend to change. An
Update query that does not contain a
WHERE clause changes every row in the
table. You can specify one value to
change.
The Multi-Valued field refers to Access databases that have tables with columns, that allow you to select multiple values, like a Combo Checkbox list.
THOSE are the only Access types that SQL cannot work with. I've tested all Access lookup possibilities, including hard-coded values, and lookup tables. They work fine, but if you have a column that has the Allow Multiple select options, you're out of luck. Even using the INSERT INTO as mentioned below, will not work as you'll get a similar but different error, about INSERTing into multi-valued fields.
As mentioned it's best to avoid using such tables outside of Access, and refer to a table specifically for your external needs. Then write a macro/vba script to update the real tables with the data from the "auxiliary" table.