Updating parts of a record in Access - sql

Is there a way to update only parts of a database record? I have a table that lists a bunch of different items followed by their cost, but I would like to remove the cost from the item. For example:
Current table looks like this
---Item-------------
Apple- 1.35
Orange - 1.24
Grape - 2.00
ETC..
---------------------
I would like to update the table with the same records, but without the hyphened price at the end. There are hundreds of different records in this table, so I can't just update by a specific record. I've tried using wildcards, but I wasn't able to get the results I'm looking for. Is there a way of doing this?

If the part you want to remove always begins with a hyphen, and there won't be any hyphens as part of the item name then this code should do what you want:
update YourTable set item = left(item, instr(item,"-")-1)
Before you run the update you might want to try it as a select:
select left(item, instr(item,"-")-1) as newitem from YourTable
If your item name can contain hyphens maybe searching for a hyphen followed by a space would work: "- "
Also, a where clause should probably be used to avoid trying to update rows without the price part.
SELECT Left(item,InStr(item,"- ")-1) AS newitem
FROM YourTable
WHERE InStr(item,"- ") > 0;

I think I would go for Mid:
SELECT t.ExistingField,
Trim(Mid([existingfield],1,InStr([existingfield],"-")-1)) AS NewData
FROM Table t
So:
UPDATE Table
SET Table.UpdatedField = Trim(Mid([existingfield],1,InStr([existingfield],"-")-1))
WHERE Table.ExistingField Like "*-*"

Get the ParseWord function, stick it in a module, and reference it in your UPDATE query like so:
ParseWord([NameOfYourField], 1)
You can also use the Split() function to do the same thing.

Related

How to retrieve the records that matches a part of the search?

I need to retrieve the records which matches a part of a searched value.
Description:
The Column in the image above holds the Event IDs that the student attended.
All the events has a corresponding ID. In my VB code, I used the Split() function to split the EventsAttended column value with the delimiter "," and they will be put in an array.
What I need:
I need to know how I'm gonna use a query to retrieve a record by matching a part of my search. It has a very long value and a WHERE alone won't do the job because it represents exact value. But in my case, like for example, I want all the records that contains 77 in it thus It will return the rows 0,6 and 8 because they have the 77. If let's say for example that I want all the records that contains 144 and 146 thus it will return the rows 6 and 8 only.
Thank you in advance.
You can use like ....
Assuming you want serch '77' try
select *
from my table
where EventsAttended like ('77,%')
or EventsAttended like ('%,77,%')
or EventsAttended like (',77%') ;
You can use:
Select * From YourTable Where "," & [EventsAttended] & "," Like "*," & [EventID] & ",*"
Extremely ineffective, so do listen to #jmcilhinney and redesign this from scratch.

Countif query in access

I am trying to run a query that calculate with a countif function but I am having trouble with it. I have used the count and the iif functions in the builder but I think something weird is going on. I am trying to count the number of times a certain value occurs in a column so I do not want a specific value to equal to if that's possible?
Thanks!
To count the number of times a value appears you can use something like.
If you want to know how many times each value appears just omit the WHERE clause (without a sample of data I've used a table in the database I'm working on).
SELECT ProcessID,
COUNT(ProcessID)
FROM tbl_PrimaryData_Step1
WHERE ProcessID = 4
GROUP BY ProcessID
if you need just the value you can use:
SELECT COUNT(ProcessID)
FROM tbl_PrimaryData_Step1
WHERE ProcessID = 4
GROUP BY ProcessID
Another way is:
SELECT DCOUNT("ProcessID","tbl_PrimaryData_Step1","ProcessID = 4")
Edit:
In reply to your comment on your original post this SQL will give the result you're after:
SELECT Concatenate,
COUNT(Concatenate)
FROM MyTable
GROUP BY Concatenate

How to use Like Operator during update

I have these type of docnos (BP/2016/00344,BP/2016/00345,BP/2016/00346) and (CP/2016/00344,CP/2016/00345,CP/2016/00346)
like that i have more than 100 records,now my task is to change the year to 2015 instead of 2016 by one update query
pls help me
Why do you need to use a like? I would think like could result in improper records being updated.
It appears year is always in positions 4-7. If this is true, using string functions this task becomes pretty straight forward.
I suggest the following approach over using a replace function because your trailing numbers in your string of 00345 may reach 2015 and I don't think you'd want to replace them.
So
UNTESTED:
update table
set docnos = left(Docnos,3)+'2016'+right(Docnos,len(docnos)-7)
where substring(docnos,4,4) = '2015'
Why minus 7? (3 for ZZ/, 4 more for the year totaling 7)
Why use a
where clause? becuase you indicated you only wanted to update the
2015 records.
Assumptions.
First 3 positions will ALWAYS Be in format ZZ/ where zz could be any 2 values
Positions 4-7 are the year you need to evaluate
Now if you must use a like...
I Suppose
we could change our where to be where docNo like '%/2015/%'
UPDATE Table
SET docnos = REPLACE(docnos,'/2015/','/2016/')
This should work as well.
Also, like xQbert said, if LIKE is a must use you can add in where docnos like '%/2015/%'
This is an elaboration on xQbert's approach. Assuming that the year always starts on 4th character, then you can do:
update t
set docno = stuff(docno, 7, 1, '6')
where docno like '__/2015/%';

How to retrieve a part of a value in a column

Correction - I only need to Pick the WORK value every result set in the column will contain comma seperated values like below..
"SICK 0.08, WORK 0.08" or "SICK 0.08,WORK 0.08"
I only need to pick WORK 0.08 from this.
I am quite new to SQL
I am using the following script to get some results;
select Work.Work_summary, Work.emp_id
from Work
all work fine. but the first column has values like the following :
WORK 08.57, SICK 08.56 (Some columns)
SICK 07.80, WORK 06.80 , OT 02.00 (Some columns)
How can i only retrieve the column with only the WORK% value, if there is no WORK value the results shall be empty.
select Work_summary, emp_id
from Work
where Work_summary like '%WORK%'
This will return the rows in the Work table where Work_summary column contains the word WORK. See the documentation for more details.
Contains is faster than like.
SELECT Work_summary, emp_id FROM Work WHERE CONTAINS(Work_summary, 'WORK');
then use: this will give only the result where work summary contains work content.
select Work.Work_summary, Work.emp_id
from Work where contains(work.Work_summary ,'work');
select replace(Work_summary,",","") as work_summary
from Work
where upper(Work_summary) like '%WORK%'

No Exact match when using LIKE in SQL statement

SELECT bp.*,r.rating,COUNT(r.review_for),bp.business_name,bp.profile_member
FROM ibf_business_reviews r
LEFT JOIN ibf_business_profiles bp ON ( r.review_for=bp.profile_member )
WHERE bp.sub_category LIKE '%{$id},%'{$location_sql}
GROUP BY r.review_for HAVING COUNT(r.review_for) >=1
ORDER BY r.date_posted DESC LIMIT 0,2");
This query is used to show results for business_name in a certain sub_category id '%{$id} in a certain location. My problem is that extra results are showing in categories that share a second or third digit aka ...viewcat&id=54 will show in ..viewcat&id=154 etc
I using the LIKE may be my issue? WHERE bp.sub_category LIKE '%{$id},%'
You are storing a comma-separated list in a varchar, when you should store one number per row in a child table. Then you wouldn't have to use LIKE at all.
Read up on First Normal Form.
Here was my comment
+! for the need to reformat the SQL. You do realize that the "percent"
signs (%) are the wildcards. So you're
essentially telling it that you can
return ANYTHING that includes id... so
if you search "23" you could get
"123", you could get "234" or
"1234"... etc.
and you replied
Thanks #Rock removing the wildcards worked!
Now my answer to this is... If you removed BOTH wildcards from your string, then you're essentially doing an "equals".
IE:
WHERE bp.sub_category LIKE '{$id},'
should be the same as
WHERE bp.sub_category = '{$id},'
Because you don't have any wildcards to "match" in the "LIKE" statement.
Please forgive me if I screwed up the "$" or the ","... I'm not a MySQL guy