Trying to use update query to only update fields that are blank in Microsoft Access - ms-access

I am trying to use an update query to update fields from one table to another for fields but only if the fields in the table that i am updating into is blank. If they contain information, I do not want to overwrite the existing data
e.g
Field: Name
Table: Table 1
Update to: [Table2.][Name]
Criteria:
I am unsure of what to put in the criteria. I tried, 'Is Null', Like "".

Here is an example:
UPDATE MyTable SET MyTable.FieldB = "MyNewValue" WHERE (((MyTable.FieldB) Is Null));

Looking at the Query from within Access, you can switch to SQL view. You just need to put Is Null in the criteria column: UPDATE MyTable SET MyTable.FieldB = "MyNewValue" WHERE (((MyTable.FieldB) Is Null));
Furthermore, you can just write Is Null on alternate lines and it will count as OR.

Related

How to UPDATE with SELECT WEEK() in same table, more than 1 row [mysql]

This is my code
UPDATE ks_tidy SET announcedWeek = (
SELECT week(dateAnnounced) as weekAnnounced
FROM ks_tidy ) WHERE announcedWeek = ''
and the problem I'm running into is it doesn't work when I'm trying to update more than 1 row?
1242 - Subquery returns more than 1 row
My table has two columns: dateAnnounced (DATE[yyyy-mm-dd], UNIQUE, PRIMARY KEY), weekAnnounced (INT). The weekAnnounced column was added to store weekAnnounced, which I am trying to "calculate" using the WEEK() SQL function. I have thousands of rows of data and this table gets updated regularly so manually doing this is not an option.
I don't see why you think you need a sub query for this
UPDATE ks_tidy SET announcedWeek = week(dateAnnounced)
WHERE announcedWeek = ''
If this is not the case please add sample data and expected output as text to the question. AND should you be testing for null rather than ''? AND do you really need to store an easily derived value?

How to update column if another column fulfill specific criteria?

I need to update one column if another column has a specific data.
Usually if I want to update one column, I do the following SQL Query:
UPDATE table1
SET field1 = replace(field1, 'oldstring', 'newstring')
But what I can't figure is how to make it look up one column, and if that field has some data, it should update another column's field.
Here is what I want to do.
look in table: phpbb_tree
under column: spouses_total
if the field is empty (has no data)
update column: page_template
update from: tree_body_spouse_1.html to: tree_body_single.html
So basically, I know how to do the "update" part, but don't know how to make it look first in one column, and if empty (or matches) it should do the following:
UPDATE phpbb_tree
SET page_template = replace(page_template, 'tree_body_spouse_1.html', 'tree_body_single.html')
Hopefully someone could tell me how to write it up. I don't even know if it's even possible to do a search for an empty data in a column?
You could use CASE expression to fulfill different condition of replacement.
UPDATE phpbb_tree
SET page_template = (CASE
WHEN spouses_total is null
THEN replace(page_template, 'tree_body_spouse_1.html', 'tree_body_single.html')
ELSE page_template
END
);
Edit:
Please check this..
SQL Fiddle HERE

MYSQL: Update field with concat of multiple fields

I'm trying to update a field of my table with the CONCAT of the some fields of the same table.
Whith this
UPDATE tabex SET field1=CONCAT(tabex.a1,', ',tabex.a2,', ',tabex.a3,', ',tabex.a4,', ',tabex.a5,', ',tabex.a6,', 'tabex.a7,', ',tabex.a8,', ',tabex.a9 );
This query has 0 rows affected and no errors.
With this other query
UPDATE tabex SET field1=CONCAT_WS(tabex.a1,', ',tabex.a2,', ',tabex.a3,', ',tabex.a4,', ',tabex.a5,', ',tabex.a6,', 'tabex.a7,', ',tabex.a8,', ',tabex.a9 );
If the content of some of a(n) fields is NULL mysql puts a copy of the previous result
Someone can help me?
When this query
UPDATE tabex SET field1=CONCAT(tabex.a1,', ',tabex.a2,', ',tabex.a3,', ',tabex.a4,', ',tabex.a5,', ',tabex.a6,', 'tabex.a7,', ',tabex.a8,', ',tabex.a9 );
doesn't affect a row, the only explanation would be, that the table is empty. It would update every row in the table. But if one of the columns is NULL, your field1 column will also be NULL.
To avoid that, you have to use the COALESCE() function. This function returns the first of its parameters which is not NULL.
UPDATE tabex SET field1=CONCAT(COALESCE(tabex.a1, ''),', ',...);
On a sidenote I have to ask, why you want to do this. Comma separated values in columns are a bad idea most of the times.
And finally, your query using CONCAT_WS() is wrong. The _WS in the function name is short for "with separator", so the first parameter is the separator which then is placed between the other parameters of the function. So you should write it like this:
UPDATE tabex SET field1=CONCAT_WS(',', tabex.a1, tabex.a2, tabex.a3,...);
Another advantage of the CONCAT_WS() function is, that it ignores NULL values. Read more about the two functions in the manual.

Mysql update rows dynamically based on column input

I have the following issue. I have a table called titles with the following structure:
id int(10),
name varchar(100),
At some point later we added a new column called modified_name. It is defined as the same as name except that it is lower case and has all of the spaces replaced with a -. We added this column and so we needed to now get the right modified name value into each record of that column. To do this we wrote a PHP script that handled that by loading in values from the database and processing them, but that is highly inefficient. Is it possible to write a single UPDATE query that would add the correct value to each record in the titles table. I can think of ways to do this with a stored procedure and a while loop there in, but I want to know if something more efficient is possible. It there any way to achieve something like the following:
UPDATE `titles`
SET
`modified_name` = LOWER(REPLACE(SELECT `name` FROM `titles` WHERE id = PRESENT_VALUE), ' ', '-');
The goal being to SET the modified_title column of every record in the titles table to a unique value that results from that record's name column as followed:
# Before modification update query
name = "Hello Goodbye"
modified_name = ""
# After modification update
name = "Hello Goodbye"
modified_name = "hello-goodbye"
Thank you for your help, any advice on how best to do this would be appreciated.
UPDATE `titles` SET `modified_name` = LOWER(REPLACE(`name`, ' ', '-'))

access append-query: set column to a literal for each record

i have multiple tables containing similar records. i want to merge them into one table.
therefore i use an update query and map the fields from the various tables to the ones in my target-table. but i need to keep track from which table a record comes, so id like to add a literal "TABLE_XY" in the ORIGINALTABLE field in the resulting table to each record. but the query designer always wants a source-field. I cant just put a literal anywhere an select ORIGINALTABLE in "Append To"...
what to do? do i really have to add a NAMEOFTHISTABLE field to the original tables...?
thanks for your help!
Make a backup copy of your database. Create a new query and switch to SQL View. Then paste in this statement, and modify the table and field names to match yours:
INSERT INTO master_table (
ORIGINALTABLE
, field1
, field2
)
SELECT
"TABLE_XY" AS ORIGINALTABLE
, field_a
, field_b
FROM
TABLE_XY;
Using the Query Designer in Design View for an Update Query:
Field: ORIGINALTABLE
Table: <tableName>, where tableName is the name of the table you are updating.
Update To: "TABLE_XY", make sure to include the quotes.
Using the Query Designer in Design View for an Append Query:
Field: Expr1: "TABLE_XY", where Expr1 is an alias name.
Table: <leaveBlank>
Append To: ORIGINALTABLE