SQL - UPDATE / SET statement query - sql

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.

Related

Sql Query to update data by prepending text

I have a table with following data :-
I want to change data of SetType Column as 'CATEGORY_3'. So the number will remain as it is, i need to prepend the number with text 'CATEGORY_'.
so SetType values will be like following :-
CATEGORY_3
CATEGORY_3
CATEGORY_5
CATEGORY_5
How to update data by prepeding predefined text by SQL Query?
You use update. Here is one method:
update t
set SetType = replace('Category_#val', '#val', SetType);
This assumes that SetType is a character field. If not, you will need to alter the type of the column before making such a change.
you can use CONCAT.
update table_name set SetType = CONCAT('CATEGORY_', SetType)
CONCAT will simply append strings to one another and you can use multiple strings in argument by separating it with , .
Use UPDATE statement, But if your SetType columns Data Type is Int then first convert it into varchar.
UPDATE your_tablename
SET SetType = 'CATEGORY_'+ CONVERT(NVarchar(2),SetType)
Use UPDATE statement of all rows :
UPDATE your_tablename SET SetType = 'CATEGORY_'+SetType
IF SetType datatype is not in VARCHAR means use below query :
UPDATE your_tablename SET SetType = 'CATEGORY_'+CAST(SetType AS VARCHAR)
If SetType is currently an integer type, it is probably an int for a reason. If you only need certain queries to prepend 'Category_' modify your select queries instead of the updating the table.
select
SetName
, SetType = convert(varchar(20),('Category_'+convert(varchar(11),SetType)))
, FieldValue1
from t

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

How to update a part of the column data using replace function in SQL?

I am trying to update a table coulmn, the table has thousands of records.
Currently I am updating the table by running the following query manually for some of the records.
UPDATE MyTable
SET column = REPLACE(column, 'ABC', 'ABC9')
WHERE where column like ‘ABC%’
Now I am trying to generate a generic query to update the table by adding a letter '9' after the alphabets. Thanks for your help
Use PATINDEX and STUFF
Patindex - Helps you to identify the first occurrence of numeric character in the string
Stuff - Helps you to insert 9 before the first occurrence of numeric character in the string
UPDATE MyTable
SET column = stuff(column,patindex('%[0-9]%',column),0,'9')

how to clean unwanted data in a column in sql server 2008

I have a column name Newspath varchar(max). The data in the column is like this
Newspath
423.jpg64265
789.jpg41546
546.jpg7894
I want to remove all the words after .jpg word like this
Newspath
423.jpg
789.jpg
546.jpg
Please help
I am assuming you need to update all the rows of your table. You can update all the rows of your table like
BEGIN TRAN
UPDATE yourtable
SET newpath = SUBSTR(newpath,0,CHARINDEX('jpg',newpath)+2)
You can look at more for Substring and Charindex.

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

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.