I am new to MS ACCESS & SQL
I am trying to replace all the "[" & "]" within the table.
Am I suppose to use REPLACE command or should Select & Loop through the Table?
Either way, I don't know how to write it in VBA. Thanks for help
I tried this, and it is not working
DoCmd.RunSQL "SELECT REPLACE" & "(StudentFirstName,'[','')" & "FROM StudentName"
See the Table Structure:
Just run an UPDATE query with REPLACE() function:
UPDATE tableName SET fieldName = REPLACE(fieldName, '[', '')
and than with the other bracket:
UPDATE tableName SET fieldName = REPLACE(fieldName, ']', '')
If you want to use in your vba code:
Docmd.RunSQL "UPDATE tableName SET fieldName = REPLACE(fieldName, '[', '')"
Docmd.RunSQL "UPDATE tableName SET fieldName = REPLACE(fieldName, ']', '')"
Remember: SELECT it's used only for VIEW data and not for edit.
If you want to EDIT data in your table you need to use UPDATE.
Related
Im trying to remove all instances of double quotes from an entire SQL table, and replace these double quotes " " with signle quotes ' '.
Is there an efficient way to do this? thanks!
If you want to change the column names, you would need to update the table:
update tablename
select column_name = replace(column_name, '"', '')
where column_name like '%"%';
To replace double quotes with single quotes, simply do
Update table
set column=replace('"','''')
where column like '%"%'
If you just want to select that column without any double quote in it:
SELECT *,REPLACE(columnname, '"','') FROM tableName
If you want to update you column by replacing all the double quote(") then Gordon provided you the right answer.
update tablename select columnname = replace(columnname, '"', '') WHERE charindex('"',columnname)>0
I'm trying to create a "dynamic" field in my SQL query
INSERT INTO tblCARS (CreateField("[tblCARS]","[colors]","TEXT"))
SELECT avail_colors
FROM tblSHOPS
WHERE car_model = model_car;
This should give me the same result as
INSERT INTO tblCARS (colors)
SELECT avail_colors
FROM tblSHOPS
WHERE car_model = model_car;
The field [colors] does not exist, so i want to append it to the existing table called [tblCARS] when i run the query.
The function CreateField() looks like this
Public Function CreateField(strTable As String, strField As String, strType As String) As Variant
CreateField = Null
If (strField <> vbNullString) And (DoesTblFieldExist(strTable, strField) = False) Then
CurrentDb.Execute "ALTER TABLE " & strTable & " ADD COLUMN " & strField & " " & strType
End If
CreateField = strField
End Function
When i run the query, Access gives me the error "incorrect syntax" (translated) and the cursor stops at the second parenthesis.
Have anyone succeeded with creating fields at runtime on a Access query?
I know i could do this entirely on VBA, but that is not my goal.
Any hints?
That approach attempts to have the db engine determine the name of the destination field when the statement is executed. Unfortunately, Access' db engine does not provide that capability.
Essentially, the statement treats that CreateField() function as a parameter. And Access won't let you use a parameter for a field name.
Consider a straightforward parameter example. This statement succeeds ...
INSERT INTO tblFoo(some_text) VALUES ("bar");
But when providing the same field name (some_text) as a parameter, the statement fails ...
PARAMETERS which_field Text ( 255 );
INSERT INTO tblFoo(which_field) VALUES ("bar");
So you'll need to use 2 operations instead of a single query. You could execute the ALTER TABLE and then a revised INSERT with the field name hard-coded into the statement.
I am trying to run a query like
UPDATE myTable SET Name='B&L' WHERE ID = 1;
The problem is I am getting the following error:
Unclosed quotation mark after the character string 'B'.
My column is of type varChar and as you can see I am escaping the string with 'quotes'. How can I get the & symbol to insert?
Thanks.
For MSSQL try escaping the & by enclosing it in square brackets so it would become:
UPDATE myTable SET Name='B[&]L' WHERE ID = 1;
Or you could use the ESCAPE statment like so:
UPDATE myTable SET Name='B\&L' WHERE ID = 1 ESCAPE '\';
It seems like your statement should work in SQL Server. I saw some instances of Oracle that had issues with ampersands. In those cases, you could use something like this:
UPDATE myTable SET Name='B&' + 'L' WHERE ID = 1
I am having difficulty retrieving a comma separated list from MSAccess using SQL. It is very easy to do with SQL Server and I have accomplished it there. But the MSAccess solution seems to elude me.
DECLARE #EmployeeList varchar(100)
SELECT #EmployeeList = COALESCE(#EmployeeList + ', ', '') +
CAST(Emp_UniqueID AS varchar(5))
FROM SalesCallsEmployees
WHERE SalCal_UniqueID = 1
SELECT #EmployeeList
http://www.sqlteam.com/article/using-coalesce-to-build-comma-delimited-string
Has anybody accomplished this using MS Access or am I just doomed to never getting lists like this through SQL?
You can select the fields (columns) one by one:
SELECT ID & ",", Other & ","
FROM table
My table has a large number of columns. I have a command to copy some data - think of it as cloning a product - but as the columns may change in the future, I would like to only select everything from the table and only change the value of one column without having to refer to the rest.
Eg instead of:
INSERT INTO MYTABLE (
SELECT NEW_ID, COLUMN_1, COLUMN_2, COLUMN_3, etc
FROM MYTABLE)
I would like something resembling
INSERT INTO MYTABLE (
SELECT * {update this, set ID = NEW_ID}
FROM MYTABLE)
Is there a simple way to do this?
This is a DB2 database on an iSeries, but answers for any platform are welcome.
You could do this:
create table mytable_copy as select * from mytable;
update mytable_copy set id=new_id;
insert into mytable select * from mytable_copy;
drop table mytable_copy;
I don't think this is doable entirely within SQL without going to the trouble of creating a temp table. Doing it in memory should be much faster. Beware if you go the temporary table route that you must choose a unique name for your table for each function invocation to avoid the race condition where your code runs twice at the same time and mangles two rows of data into one temp table.
I don't know what kind of language you're using but it should be possible to obtain a list of fields in your program. I would do it like this:
array_of_field_names = conn->get_field__list;
array_of_row_values = conn->execute ("SELECT... ");
array_of_row_values ["ID"] = new_id_value
insert_query_string = "construct insert query string from list of field names and values";
conn->execute (insert_query_string);
Then you can encapsulate that as a function and just call it specifying table, old id and new id and it'd work it's magic.
In Perl code the following snippet would do:
$table_name = "MYTABLE";
$field_name = "ID";
$existing_field_value = "100";
$new_field_value = "101";
my $q = $dbh->prepare ("SELECT * FROM $table_name WHERE $field_name=?");
$q->execute ($existing_field_value);
my $rowdata = $q->fetchrow_hashref; # includes field names
$rowdata->{$field_name} = $new_field_value;
my $insq = $dbh->prepare ("INSERT INTO $table_name (" . join (", ", keys %$rowdata) .
") VALUES (" . join (", ", map { "?" } keys %$rowdata) . ");";
$insq->execute (values %$rowdata);
Hope this helps.
Ok, try this:
declare #othercols nvarchar(max);
declare #qry nvarchar(max);
select #othercols = (
select ', ' + quotename(name)
from sys.columns
where object_id = object_id('tableA')
and name <> 'Field3'
and is_identity = 0
for xml path(''));
select #qry = 'insert mynewtable (changingcol' + #othercols + ') select newval' + #othercols;
exec sp_executesql #qry;
Before you run the "sp_executesql" line, please do "select #qry" to see what the command is that you're going to run.
And of course, you may want to stick this in a stored procedure and pass in a variable instead of the 'Field3' bit.
Rob
Your example should almost work.
Just add the column names of the new table to it.
INSERT INTO MYTABLE
(id, col1, col2)
SELECT new_id,col1, col2
FROM TABLE2
WHERE ...;
i've never worked with db2 but in mssql you could solve it with following procedure. this solution only works if you dont care what new id the items get.
1.) create new table with same scheme but where the id column incrementes automatically. (mssql "identitity specification = 1, identity increment = 1)
2.) than a simple
insert into newTable(col1, col2, col3)
select (col1, col2, col3) from oldatable
should be enough, be sure not to include your id colum in the above statement