how to set one field in a table equal to another field in a table in access - ms-access

I have a table with two fields, IDCopy and ID. I want to copy the value of ID into IDCopy because ID is a number field and I need a second copy of this field as a text field.
I am used to doing things like this on sql server
UPDATE table SET table.IDCopy= table.ID;
But when I try to run that query in access it asks me for the parameter value of ID. What is the syntax for setting one column in a table to another column in Access?

You can use CStr() to cast the ID number to text. This should work when IDCopy is text and ID is numeric.
UPDATE [table] SET IDCopy = CStr(ID);
I bracketed the table name because table is a reserved word.
If Access still thinks ID is a parameter with this query, then [table] does not include a field named ID.

Related

Query in Access with a "contains" between columns

I need a query in Access with the following:
If Column2, Cell("B1") = "A1" and
Column 1, Cell ("A1")= "B1_A1_DTTROB"
THEN Col3 NEEDS TO SHOW "Contains" or "Correct"
Thanks
As #Minty said - Access understands fields and records. It hasn't got a clue about cells, columns or rows. You can, if you must, think of a field as a column and a record as a row - but you will more than likely get corrected every time you say it out loud.
Saying that, I can see you're trying to compare your first field with the second field on the same record and if it's a match return the text.
SELECT IIF(Column2 = [Column 1],"Contains","Correct") AS Col3
FROM MyTableName
This query will return a table containing a single field called Col3. It will have the same number of records as your original table as it compares the two fields on each record.
To return other fields from your table just add them to the Select clause separated by a comma:
Select Column2, [Column 1], ....
Field names can contain spaces, but you have to wrap the field name in square brackets []. You can also use reserved words in fields, but again wrap them in square brackets [Name].... using spaces or reserved words can causes problems further down the line, so easier to use Column1 rather than [Column 1] and strName rather than Name.
In the field properties in the table design view there is a Caption property that lets you give the field an alias name, but I find this confusing when writing queries in SQL View - Design view will show the correct field name.

Make work REPLACE INTO with specific column

I have a table with a column ID Auto-increment and SKU with UNIQUE index and some more columns.
ID is just a counter and SKU is the unique property for each row.
Instead of checking a record with SELECT query if it exists and then updating, I want to do it with REPLACE INTO to work with SKU column.
I know REPLACE INTO behavior that it deleted the previous row and inserts the new one.
But currently it is working with ID column.
Can I specifically make REPLACE INTO work with SKU column?
Don't specify the ID column in your statement, only specify the SKU column. Since it is a UNIQUE index, the REPLACE INTO should take it into account. The ID column is auto-increment, if you leave it empty it should be auto-generated by the DB.

Using variable name instead of a Table name in Select Statement

I'm working on SQL Server to get the datas from one database and compare with another database. The database we have, has around 400's of table. I have to write a query to get all the table names based on the DB name, and having one column name, I have to get the datas from all the tables.
Still now, I have written a query to get the primary key value and to get the table names having that primary key value. My plan is to call the primary key details in first cursor, and within that create another cursor and fetch the details of the table name and column name.
Based on the values retrieved, I have to write a query to fetch the datas like "select * from #cursor_variable_tablename where primarykeyval = #cursor_variable_primarykeyval".
Is it possible to work like this by calling a variable instead of giving the table name.?
Please help me with this. Thanks In Advance.
Not sure what you are trying to achieve here but you can use dynamic sql to execute the query having table name as a variable.

fist time SQL syntax. Concatenate some string to each instance of a field in a table

using mySQL for the first time for what I believe must be a really easy task.
I have a table called "incidents". In that table there is a field called "id". There are about 90 records in this table.
For each record, I'd like to concatenate:
'somestring'+id
What I've tried:
SELECT * FROM incident;
CONCAT('https://somewebsite.com/reports/view/',id);
After hitting go I'm then being told I have an error in my syntax?
How would I create a SQL statement that returns the concatenation of the same string with each unique ID in a table where the ID is the field to be appended?
SELECT CONCAT('https://somewebsite.com/reports/view/',id) FROM incident;

how to change a lot of records at once in phpmyadmin

I'd like to know how to update several records at once where a certain column type is selected.
I have found how to select records. I have found how to update records. But i don't know how to do it both together for it to work.
Selecting:
SELECT * FROM users WHERE 'type'='new'
Updating:
update table
set column = 937
So basically i want to change the info in the 'column' to 937 in the 'table' if another column 'type' is 'new'.
Thanks,
You can do this by simply adding a WHERE clause to your UPDATE statement:
UPDATE `users`
SET `myColumn` = 937
WHERE `type` = 'new'
Of course, change myColumn to match your column name
You can do this with a subquery :
update users
set column = 937
where id in (select id from users where type='new')
Just change the columns name if I got them wrong.
After researching for a while I found another solution to this problem. When referencing the same table or field name in a query, the name space in MySQL ends up with duplicates and fails. To overcome this use the "AS" syntax to name the duplicate items. Also, update queries often require a key field to be used as the parameter for the where clause, this is often your auto-incremented ID field. This example provides a solution for both of these problems.
UPDATE tbl
SET field=newValue
WHERE tbl.key IN
(SELECT idNew FROM
(Select tbl.key AS idNew
FROM tbl
WHERE field=editValue) AS tblNew);
tbl - Table name being updated
field - Target field for update
newValue - Value to replace items in the target field
tbl.key - Field name for the key in your table
idNew - New name for key in your table, to replace the name and prevent failure from duplicating names in query. could be any alphanumeric string. 'id' or 'id_new' or 'id_n'
editValue - The value to change
tblNew - New name for query, to prevent duplicate table names in the query. Could be any alphanumeric string 'tbl_n' or 'tbl_new' or 'tbl_test'
This query gets the key values for all the records that match records that have values you want edited, then changes the names of the key and tbl so they can be processed by MySQL, lastly the update query runs and changes the values based on the keys provided in the sub-query.
Hopefully this saves someone else a few hours!