How to use proper syntax in where statement when updating a column where zip code columns are multiple - sql-server-2005

I am assigning salespeople to a table column based on a zip code. I want to include multiple zip codes in the where clause to set the sales person but don't know what the syntax is to do that. Here is what I have.
update [Customer]
set [Territory Code] = 1
where [Post Code] = '40205' and '40206' and '40118'
This, of course, does not work.

You will use IN
where [Post Code] in ( '40205', '40206', '40118')

Related

Access SQL: I am trying to update a table with values from the same table

I am trying to import a CSV file exported from Ebay into MS Access.
Ebay cram into one file what should be in two files. So if someone purchases two products, Ebay puts the customer details in one row and the products bought in the following lines.
Customer cruze130613 bought two products, so has information spread over three lines!
What I would like to do is fill the empty User ID column blanks.
So:
Set [User ID] equal same 'User ID' as where [Sales record number] = [Sales record number]
I think the select clause for [Sales record number] should use DISTINCT as is duplicated on >1 rows.
Any help with the SQL code will be appreciated.
Perhaps something along the lines of the following:
update YourTable t1
set t1.[user id] = dlookup
(
"[user id]",
"YourTable",
"[user id] is not null and [sales record number]=" & t1.[sales record number]
)
where t1.[user id] is null
Change both references of YourTable to the name of your table.
Here, the domain aggregate function DLookup is used to acquire the value of the User ID field for a record meeting the supplied criteria (in this case where User ID is not null, and where the sales record number is equal to that of the record being updated).
There are other ways to achieve the same result, but DLookup is used here to retain the 'updateability' of the query in MS Access.
Thank you Lee for your suggestion.
As far as I can see Ebay only export these pseudo CSV files.
I had another look today and managed to find a solution. I wonder which would be more efficient yours or mine?
UPDATE tEbImportRaw AS eir1
INNER JOIN tEbImportRaw AS eir2
ON eir1.[Sales Record Number] = eir2.[Sales Record Number]
SET eir1.[User ID] = eir2.[User ID];

Update Whole Column in one time?

I searched web but couldn't find anything same to my need. I am using northwind sample database.
For example I have new prices that came with excel sheet.
I imported to my database as new table named [Product Updates] and the column names are same as above[Product , New Price] and now I want to update products table to this new prices came with excel sheet.
I don't want to write 15 update statements. It must be done at a time with one piece of command I believe.
I wrote something like this
update Products
set UnitPrice = [Product Updates].[New Price])
where Products.ProductName=dbo.[Product Updates].Product
But it doesn't work
One simple solution uses a join:
update p
set UnitPrice = pu.[New Price]
from Products p JOIN
[Product Updates] pu
on p.ProductName = pu.Product;
I would advise you to name things without spaces, so you don't need to use the square braces. They just make queries harder to write and to read.

Verify Data in Access with two tables using SQL

I have two tables in Access, one is essentially a customer record with their personal info and another is full of valid values, for example state abbreviation and gender. I am trying tocompare the two tables so that it checks the gender and the state codes against whats in the reference table and pull the customer ID (another field in the first table) and report that theres an issue by putting the customer id and the values im using to validate, in another table.
B
ut instead of only returning values that dont match it returns every record from the first table. so its not really verifying anything so much as just copying and pasting, instead of only returning values with no match in the second table.
My code looks like this
DoCmd.RunSQL
"INSERT INTO Issues
SELECT Eligibility.ID, Eligibility.[Member Id], Eligibility.[Sex Code], Eligibility.State
FROM Eligibility
LEFT JOIN Ref ON
(Eligibility.[Sex Code]<>Ref.[Sex Code]) AND (Eligibility.State<>Ref.State);"
What you will want to do, is actually change the JOIN clause to match the Sex Code and State, but only return the rows that do NOT match.
INSERT INTO Issues
SELECT Eligibility.ID, Eligibility.[Member Id], Eligibility.[Sex Code], Eligibility.State
FROM Eligibility
LEFT JOIN Ref ON
(Eligibility.[Sex Code]=Ref.[Sex Code]) AND (Eligibility.State=Ref.State)
WHERE Ref.[Sex Code] IS NULL
The WHERE Ref.[Sex Code] IS NULL will filter out the valid rows, and only return the rows that do not meet your JOIN criteria.

JOIN on Access "Lookup" field returns no results

I have a strange problem that I believe is related to the way my lookups are structured.
TABLE Category
ID
CategoryName
TABLE Product
ID
ProductName
Category - Number (SELECT [Category].[ID], [Category].[Category Type] FROM Category ORDER BY [Category Type];)
TABLE SalePrices
Several fields related to sale date, price, &c.
ProductName - Text (SELECT [Product].[ID], [Product].[Product Name] FROM Product ORDER BY [Product Name];
For some reason I get a blank result set when I run the following query:
SELECT SalePrices.[Product Name], Product.[Product Name]
FROM SalePrices INNER JOIN Product ON SalePrices.[Product Name] = Product.[Product Name];
I have a query that displays the MIN of SalePrices.UnitPrice which I want to display with the ProductName and CategoryName, but I'm not getting results for that so I wanted to simplify things first.
When I join Product and Category I have to match Product.[Category Type] = Category.ID;, but when I try to do SalePrices.[Product Name] = Product.[ID]; I get a TYPE MISMATCH error. I'm not sure where I went wrong.
The eventual goal is to combine the SalesPrices <-> ProductName join with this one:
SELECT Product.[Product Name], Category.[Category Type]
FROM Product INNER JOIN Category ON Product.[Category Type] = Category.ID;
As suggested in the comments by #Scotch and #HansUp, defining "Lookup" fields in an Access table is generally regarded as a Bad Idea for all but the most trivial of cases. They hide what is really going in the database and they can lead to all kinds of confusion (as you have discovered) when you actually try to use them. (Ref: here, via comment by #HansUp.)
Your best course of action would be to replace the Lookup fields in your tables with regular numeric fields containing the ID (key) values in the related tables. Then, if necessary, you can create and save Select Queries to explicitly do the lookups and display the results as they previously appeared in the [Product] and [SalePrices] Datasheet Views.

Access 2007 Update Query has more Rows than Database

I have an Update query that says it's updating 1029 rows, when there are only 994 rows in the database that it's updating.
The query references an external Excel file and updates the "Master Calendar" with "Posted Date" when the batch numbers are equivalent:
UPDATE [Mapped Link] RIGHT JOIN Master_Calendar ON [Mapped Link].Reference = Master_Calendar.[Batch Number] SET Master_Calendar.[Actual Posted Date] = [Mapped Link]. [Entry Date];
Whenever rows get added to the external 'Mapped Link' document, the rows increase on the update query. There are about 2000 rows in 'Mapped Link', so it's not adding the rows from Master Calendar to Mapped Link together.
The data doesn't seem to be updating incorrectly -- all the data is correct. But how can it be updating more rows than it exists? Is it just counting wrong? Thoughts?
Thanks, guys.
You have either duplicates in either Master_Calendar.[Actual Posted Date] or [Mapped Link].[Entry Date]
To check for duplicate dates, you can run a query. For example:
SELECT [Actual Posted Date], Count(ID) AS CountOfID
FROM Master_Calendar
GROUP BY [Actual Posted Date]
HAVING Count(ID)>1
You will need to change ID to the name of the index on your table.