Let me start off be saying I am new to trasactsql and query so please be kind.
I have a DB with about 1700 records for my product file, and one field (name) has the product code, a space and then the name of my product. I have another field (pcode) with just the product code that has numbers and possibly a letter or two that matches the beginning of the "name" field. I need to remove all characters in front of the space in 'name" including the space. Is there a way to remove the characters if they match what is in "pcode"?
Here is an example.
What I have;
pcode | name
1234 | 1234 widget
What I want;
pcode | name
1234 | widget
There are some names with spaces in them. ie. "Left Handle Button side" or "Control "Lever"
Thanks in advance.
Michael
In a select, you can do:
select pcode, substring_index(name, ' ', -1)
. . .
In an update:
update table t
set name = substring_index(name, ' ', -1)
where name like '% %';
If you want to change the data in place (using an update), check the logic (or copy the table) before doing the update. This might not do what you expect, for instance, if the name itself has a space in it.
Try to export it to MS Excel for example and do it manually. Next time learn about 12 gold rules when creating SQL database.
http://en.m.wikipedia.org/wiki/Codd%27s_12_rules
Related
I am using Microsoft Access 2013
I need to make a query so that the program can grab the last name of the ACTOR'S NAME when the ACTOR'S name has their full name for example Kelly Garman, and Weird Al Yankovic. Yankovic has 3 values and Garman has 2 values. Any help?
There are over 10,000 records with tons of different names, so I need to have it so the user will be asked to input the last name and once they do, the database will query by the last name.
Its also a query where the user inputs something in when asked.
Columns in the database are in the SELECT statement
SELECT [RECORD #], [ACTOR'S NAME], [PRODUCTION NAME]
FROM Actors AS A
WHERE [A.ACTOR'S NAME] = [What's the last name?]
The Database file is here. https://drive.google.com/open?id=0B19CRcQGkXoVTTZpaldDWnFuS28
You can use REVERSE along with SUBSTRING as below to get the last name.
REVERSE( SUBSTRING( REVERSE([ACTOR'S NAME]), 1, INSTR(' ', REVERSE([ACTOR'S NAME])) - 1))
To find the last name you need to get the word after the last space. You can't do this directly in MS Access. Instead, you can find the position of the last space with InstrRev(Name, ' '). Then use Right to get everything to the right of that, but that takes a position from the end of the string. To get that, subtract what you got from InstrRev from the length of the name.
SELECT Right(Name, Len(Name) - InstrRev(Name, ' ')) as 'Last Name'
FROM Actors
WHERE Right(Name, Len(Name) - InstrRev(Name, ' ')) = 'Yankovic';
Unfortunately I do not have a copy of MS Access to test this.
You can use a pattern match in the WHERE clause.
PARAMETERS [What's the last name?] Text ( 255 );
SELECT A.[Record #], A.[ACTOR'S NAME]
FROM Actors AS A
WHERE A.[ACTOR'S NAME] ALike '% ' & [What's the last name?];
I have a typeahead select box for finding users in a database. Frequently there are meny users with the same first name & different last names.
A person searching for a user would generally type in: "John doe".
The problem with this is that the typeahead will start getting all the John users but then return no results when the space is typed in.
"WHY" this is happening is obvious, user first names are trimmed & anything with a space will not match ~ let alone with another character following that, first & last names are stored in separate columns.
Here is the query as it stands:
SELECT Entities.* FROM `Entities`
WHERE `Entities`.`first_name` LIKE '%john doe%'
OR `Entities`.`last_name` LIKE '%john doe %'
How can I re-write this query so that the results would be:
all users whose first name is john
ignore any amount of spaces
AND where last name is doe
when a user types in"john doe" in the search control
(Keeping in mind that the search control is ONE text type field)
If you want the first name like john and the last name like doe, then try something like:
where concat(Entities`.`first_name`, ' ', `Entities`.`last_name`) like '%john% doe%'
If you want do do this based on a search string, then something like:
where concat(`Entities`.`first_name`, ' ', `Entities`.`last_name`) like
concat('%', replace('john doe', ' ', '%'), '%')
Or, use full text search.
I'm trying to create a SQL query which will supply values for auto completion for a text field. Everything is working however I can't seem to create an SQL query which is exact enough for the purposes I want. I am using MySQL.
If there is a space (or multiple spaces) in the search term, I only want the query to do a LIKE comparison on the part of the string after the last space.
For example, say I have two possible values in the database:
Bolt
Bolts Large
Currently if the user types 'Bolt' then a space, both values above are returned using this query -
SELECT name FROM items WHERE name LIKE 'SEARCH_TERM%'
What I want is that if the user types 'Bolt' then a space, then only Bolt is returned from the database.
Effectively meaning that only the last part of the search term after the space is compared using LIKE, the results should match exactly up until the last space.
I've also tried:
SELECT name FROM items WHERE name LIKE 'SEARCH_TERM[a-z]%'
But that actually returns no results using the above scenario.
Is what I'm after possible? I've also tried to explore using Full Text Search but have had no look with that. I believe full text search is enabled on the name field, however I have limited experience with this. The query below didn't work.
SELECT name FROM items WHERE MATCH(name) AGAINST('SEARCH_TERM')
Any advice or points would be very appreciated.
The query
SELECT name FROM items WHERE name LIKE 'Bolt %'
doesn't return any record, because both 'Bolt' and 'Bolts Large' don't match 'Bolt %'.
SELECT name FROM items WHERE name LIKE 'Bolt%'
returns both records, because both 'Bolt' and 'Bolts Large' match 'Bolt%'.
To look for 'Bolt' and not 'Bolts', you must add a space to both your search string and the column string:
SELECT name FROM items WHERE concat(name, ' ') LIKE 'Bolt %'
returns 'Bolt' but not 'Bolts Large'.
SELECT name FROM items WHERE REPLACE(name, ' ', '') LIKE 'SEARCH_TERM%'
You could also use CONCAT and TRIM, or just trim
SELECT name FROM items WHERE name LIKE TRIM('SEARCH_TERM')
or your choice
SELECT name FROM items WHERE name LIKE CONCAT(TRIM('SEARCH_TERM'), '%')
SELECT name FROM items WHERE name LIKE CONCAT('%',TRIM('SEARCH_TERM'))
SELECT name FROM items WHERE name LIKE CONCAT('%',TRIM('SEARCH_TERM'), '%')
I have a situation:
I have a database (MySQL) which contains products and their codes like this
BLACK SUGAR BS 709
HOT SAUCE AX889/9
TOMY 8861
I got an excel spreadsheet which I converted to CSV, this contains prices for the products. Its structure consists in 2 columns, code and price, like this:
BS709 23.00
AX 889 /9 10.89
8861 1.69
I made a script to update the products prices by searching in the database for the respective product code, using a FOREACH and %LIKE% query.
FOREACH row in CSV, search the database using "WHERE product_code LIKE %code%.
This is offcourse a primitive and not so succesfull way of updating the prices, because the codes in CSV are not an exact match (in syntax) of those in the database so if I have two products in the DB containing BS709 (BS70923) in their code I get multiple matches.
Is there a better way of doing this ?
You could trim the columns of spaces and other characters using MySQL replace() before comparing. This will return all exact matches, regardless of any spaces contained.
SELECT * FROM table WHERE REPLACE( product_code, ' ', '' ) LIKE 'code'
Given your examples, I would recommend removing all spaces from both, and then just looking for when the beginning or end of a code matches exactly:
where replace(e.code, ' ', '') like concat(replace(db.code, ' ', ''), '%') or
replace(e.code, ' ', '') like concat('%', replace(db.code, ' ', '')) or
replace(db.code, ' ', '') like concat(replace(e.code, ' ', ''), '%') or
replace(db.code, ' ', '') like concat('%', replace(e.code, ' ', ''));
This may not work for the specific case when one code is a prefix of another.
In any case, if the product codes in a spreadsheet are different from the product codes in the database, I think you have bigger problems. If you cannot really fix the spreadsheets, I would recommend that you manually/semi-automatically create a synonyms table in the database. This would have the Excel product code in one column and the correct product code in the other. Then you can do the lookup just by joining this together.
Yes. That is work. But probably less work than struggling with this problem and getting poor results that have to be repeatedly updated.
I've got a mySQL database table called "booking", which contains a column of credit card numbers. Obviously this is not good or secure, and I really only need the last 4 digits in that field. I need a mySQL call that will fix this mistake.
Is there a way have one SQL statement that could go through the entire database table and replace all the Visa, Mastercard, Discover card #'s (which are different lengths), and overwrite all but the last 4 chars with XXX's. Or would I need to write something that loops through each record to do the replace?
The table is called "booking", and the field is called "creditcardnumber"
Try like this
SELECT
CONCAT(
REPEAT('X', CHAR_LENGTH(card_number) - 4),
SUBSTRING(card_number, -4)
) AS masked_card
FROM
table_name ;
Check on this reference: * SQLFIDDLE.
Don't mind the table schema, it's just the sample old data. I changed the first record to a credit card number (duh - it's not starting with 5, 4, or 3 ;) but LAPD does the job.
Query:
select right(hasha,4),
lpad(right(hasha,4),length(hasha),'x')
from actors
where id = 1
;
results:
RIGHT(HASHA,4) LPAD(RIGHT(HASHA,4),LENGTH(HASHA),'X')
3456 xxxxxxxxxxxx3456