Description of Problem:
I want to read data in a column in my sql table and clean its values.
It should essentially only have ‘numbers’ of length 9.
It currently is of varchar(11) and holds values like: A:123456789, 1-2:3456789, 123456789H,A-123456789 etc.
How can I add multiple checks for different values of the table and replace the incorrect values and update my table.
I tried righting a search function for checking if values contained are : , –
SELECT budget, descp
FROM yearlybudget
WHERE CONTAINS(budget, '":" OR "-");
SELECT budget, descp
FROM yearlybudget
WHERE CONTAINS(budget, '":" AND "-");
UPDATE dbo.yearlybudget
SET budget = replace(budget, '":"', '""')
WHERE CONTAINS(budget, '":");
Above are 3 different SQL statements. Can I merge the “OR” and the “AND” conditions checks in anyway? Also how can I add replace to check all the conditions?
Is it best to do it separately for each condition?
Please advise.
Thank you.
First, these queries don’t do what you think they do. The big problem is that there are two single quotes, which together for a string. So your first two queries are actually checking if the string
":" OR "-"); SELECT budget, descp FROM YearlyBudget WHERE CONTAINS(budget,is present.Second, doing
":" OR "-"in a contains will not check for both of those strings. Rather, the strings will be evaluated as numbers and then combined with binary OR. Specifically, they’ll evaluate to 0 and then 0 OR 0 will give you 0, so you’re checking CONTAINS(budget,0).Third, your replace is not replacing
:, but":". So, it won’t replace the : character unless it’s surrounded by double quotes.Fourth, my googling indicates that MySQL doesn’t have a “CONTAINS” keyword.
The query you probably want looks more like this:
The % in a LIKE string means “match any characters here”, so it winds up doing the same thing that CONTAINS does on some other database management systems. So, this query will update the budget column by removing all – and : characters, which I think is what you wanted. If you want to do something different, you should probably say what. For example, if you only want to get a version of the budget column without : or -, then you should use a different query, since this one will actually change the data in the database. Be sure that you want to remove – from the budget column before running this, because that has the potential to turn negative budgets positive, which could be a problem.
To answer the original question about whether or not you can combine the OR and AND, the answer is probably, but it depends on what you’re trying to do. OR is not exclusive, so A=1 OR B=1 will be true when both A=1 and B=1.