SQL query to extract text from a column and store it to a different column in the same record - sql

I need some help with a SQL query...
I have a SQL table that holds in a column details of a form that has been submitted. I need to get a part of the text that is stored in that column and put it into a different column on the same row. The bit of text that I need to copy is always in the same position in the column.
Any help would be appreciated guys... my mind has gone blank :">

UPDATE mytable
SET other_column = SUBSTRING(column, begin_position, length)

You may just want to use a computed column. This way if the source string changes, your computed column is still correct. If you need to seek to this substring then you might want a persisted computed column if your db supports it.

UPDATE table
SET Column2 = SUBSTRING(Column1, startPos, length)

What if the value you wanted to copy was in a different position in each record, but always followed the same text?

Related

SQL: How to apply a function (stored procedure) within an UPDATE-clause to change values?

the following function deletes all blanks in a text or varchar column and returns the modified text/varchar as an int:
select condense_and_change_to_int(number_as_text_column) from mytable;
This exact query does work.
Though my goal is to apply this function to all rows of a column in order to consistently change its values. How would I do this? Is it possible with the UPDATE-clause, or do i need to do this within a function itself? I tried the following:
UPDATE mytable
SET column_to_be_modiefied = condense_and_change_to_int(column_to_be_modiefied);
Basically i wanted to input the value of the current row, modify it and save it to the column permanantly.
I'd welcome all ideas regarding how to solve scenarios like these. I'm working with postgresql (but welcome also more general solutions).
Is it possible with an update? Well, yes and sort-of.
From your description, the input to the function is a string of some sort. The output is a number. In general, numbers should be assigned to columns with a number type. The assumption is that the column in question is a number.
However, your update should work. The result will be a string representation of the number.
After running the update, you can change the column type, with something like:
alter table mytable alter column column_to_be_modiefied int;

REPLACE query format in DB2

I have a set of json format stored in a column and now i need to replace a particular word. How to use the replace query. Every time i use it, i'm getting token exception. please advise. Its a DB2 i'm using
I have 3 columns
Name Age Data
ABD 15 [{"Name":"ABC","type":"Regular","Math":18}]
In the Data column, I need to do a replace for "type", It should be StudentType.
REPLACE(Data,'type','StudentType');
This did not work. How to do it?
Thanks much in advance
Just like #mustaccio pointed out, if you use REPLACE in select statement it will just return your data with 'StudentType' instead of 'type'. This does not actually change data in your database. If you want to update your data you need UPDATE statement
UPDATE MyTable
SET MyColumn = REPLACE(MyColumn,'OldString','NewString')

Any way to add value into existing rows?

I am still not so good with SQL query and can't find answer via Google too !
I got table with existing data and a very complicated data like several values with ASCII code in between .
At SQL Table, it will just appear as space
But It wont work if I just copy the value from that Column and use update statement as there are some ASCII chars acting as space ( for e.g. if you copy from Column,it will appear as ValueA ValueB ValueC but in reality,it is something like ValueA+0+7+7+1+7+ValueB
Long explanation ..
So What I need to do now is add new value into that Column
If needed ,I can add this new value with ASCII too since ASCII number are fixed
I need the query to be something like
Update DB-1
set Data=(select Prob_ColumnA from DB-1) +0+7+7+1+7+'New Value'
You'll need to publish more details about your database to get a good answer. Assuming the table name is DB-1 and the column name is Prob_ColumnaA, the following might help you find what you're looking for:
UPDATE DB-1
SET Prob_ColumnA = Prob_ColumnA + 'New Value'

SQL insert avoid column names

In SQL insert, generally we specify the names of the columns in the SQL. Is there a way to generate that dynamically? basically if we specify the names of columns then tomorrow if a new col is added, there is a code change involved. How can i avoid this?
I am thinking of follg solution -
How about getting the names of the columns via
select column_name,* from information_schema.columns where table_name = '' order by ordinal_position;
& then create the INSERT statement with the columns? This way we do not specify the column names in the SQL...
Any thoughts?
You can only leave out the column names if you fill all values, but that would also break if a new column is added.
If you specify the names, you can add new columns as much as you want. They will automatically be assigned their default value if you leave your query as it is.
If you don't want them to have the default value, you need to edit the code anyway. Sure, you can dynamically generate the SQL and assign a default value, but that is what your RDBMS does anyway! I don't see your problem.
You can use a default value for the new column.
Assuming you're saying that you'll be getting the column values as input from somewhere else, then you could keep the column names in a config file like a properties file. Then if you're willing to assume that the ordering will always match up, just match up the input values to the column names from the file, and adding a column just means getting a new value to match the new column.
I solved this by getting the names of the columns via
select column_name,* from information_schema.columns where table_name = '' order by ordinal_position;
& then create the INSERT statement with the columns. By this I avoided specifying the column names in the query

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.