Edit: the solution provided by vax was successful, and should work for just about anyone facing a similar issue, as it replaces 4 different types of spacing, not just spaces. Thanks to all who provided help!
I have a table that gives industry names, but for one of the columns the distinct values have duplicates.
Here is the query I have:
select distinct replace(final_industry,' ','') from industryNorm
this is the results that are generated:
Automotive,AerospaceandDefense,Transportation,Travel,IndustrialProducts,andInfrastructure
Automotive,AerospaceandDefense,Transportation,Travel,IndustrialProducts,andInfrastructure
Communications,Media&Technology
ConsumerIndustriesandRetail
ConsumerIndustriesandRetail
Energy,Healthcare&ProcessIndustries
FinancialServices
FinancialServices
As you can see, many of the repeat. It seems that they still have spaces at the end, but I don't understand why, as I've run a replace function to get rid of them. I also ran a replace to put a | in the place of any space and when I do that the spaces at the end don't show up. Why is that? What can I do?
Can anyone help me out with this? Thanks!
Try RTrim() and LTrim() instead of Replace(). Trim() will take care of other characters than just ' '.
If you believe the problem is due to other white spaces I had a similar question that someone answered
select distinct replace(replace(replace(replace(
final_industry
,char(9)/*tab*/,'')
,char(10)/*newline*/,'')
,char(13)/*carriage return*/,'')
,char(32)/*space*/,'')
from industryNorm
You might be running into a very, very old bug in the replace function.
This is going to seem dumb, and that's because it is, but try putting multiple replaces in:
replace(replace(final_industry,' ',''), ' ','')
It might not help in your case but I've had times where the first replace didn't actually replace everything properly.
Here is some more explanation of why/how this happens: https://www.xaprb.com/blog/2005/11/15/a-bug-in-microsoft-sql-servers-replace-function/
Solution given by user Vax.
select distinct replace(replace(replace(replace(
final_industry
,char(9)/*tab*/,'')
,char(10)/*newline*/,'')
,char(13)/*carriage return*/,'')
,char(32)/*space*/,'')
from industryNorm
Related
I have tried looking for answers online, but I am lacking the right nomenclature to find any answers matching my question.
The DB I am working with is an inconsistent mess. I am currently trying to import a number of maintenance codes which I have to link to a pre-existing Excel table. For this reason, the maintenance code I import have to be very universal.
The table is designed to work with 2-3 digit number (time lengths), followed by a time unit.
For example, SERV-01W and SERV-03M .
As these used to be added to the DB by hand, a large number of older maintenance codes are actually written with 1 digit numbers.
For example, SERV-1W and SERV-3M.
I would like to replace the old codes by the new codes. In other words, I want to add a leading 0 if only one digit is used in the code.
REPLACE(T.Code,'-[0-9][DWM]','-0[0-9][DWM]') unfortunately does not work, most likely because I am using wildcards in the result string.
What would be a good way of handling this issue?
Thank you in advance.
Assuming I understand your requirement this should get you what you are after:
WITH VTE AS(
SELECT *
FROM (VALUES('SERV-03M'),
('SERV-01W'),
('SERV-1Q'),
('SERV-4X')) V(Example))
SELECT Example,
ISNULL(STUFF(Example, NULLIF(PATINDEX('%-[0-9][A-z]%',Example),0)+1,0,'0'),Example) AS NewExample
FROM VTE;
Instead of trying to replace the pattern, I used PATINDEX to find the pattern and then inject the extra '0' character. If the pattern wasn't found, so 0 was returned by PATINDEX, I forced the expression to return NULL and then wrapped the entire thing with a further ISNULL, so that the original value was returned.
I find a simple CASE expression to be a simple way to express the logic:
SELECT (CASE WHEN code LIKE '%-[0-9][0-9]%'
THEN code
ELSE REPLACE(code, '-', '-0')
END)
That is, if the code has two digits, then do nothing. Otherwise, add a zero. The code should be quite clear on what it is doing.
This is not generalizable (it doesn't add two zeros for instance), but it does do exactly what you are asking for.
I apologize if this has been covered previously. I could not find exactly what I was looking for by searching. So I hope it's okay that I ask.
I have a column with many different types of alphanumeric values (e.g. A101, F576, AI01, etc.). What I'm wanting to do is find members of a specific portion with a specific pattern, F100 through F9999. Using between F1000 and F9999 gets me what I need. But, I don't think that's exactly the right way to go about querying for future reference.
Does anyone have any suggestions? Any help would be greatly appreciated!
This should do the trick:
SELECT * FROM THE_TABLE
WHERE THE_COLUMN LIKE 'F[1-9][0-9][0-9][0-9]'
That will match F1000 through F9999 The key point is you can use [0-9] as a range. If you want to do other patterns things like [A-Z] work too.
If you want F100 to F9999 you could do:
SELECT * FROM THE_TABLE
WHERE THE_COLUMN LIKE 'F[1-9][0-9][0-9][0-9]'
OR THE_COLUMN LIKE 'F[1-9][0-9][0-9]'
I am trying to find special characters in any of my fields that are not in the range of a-zA-Z0-9. However if I try this query:
select Name from table where Name like '%[?]%'
I get two records:
???? ?????
Fixed?????
Which is what I want. However, since I don't know what the special chars will be I need to use an exclusion of data that has mixed characters:
select Name from table where Name NOT like '%[a-zA-Z0-9]%'
Since this excludes all records with a-zA-Z0-9 I only get:
???? ?????
But I also need to get the 'Fixed?????' result. I need to get the data that has the special character merged into it.
I am bit at a loss as how to do this. I've seen this done with shell scripts or 'vi' (LIST), but in SQL that's not so easy.
Has anyone out there solved this?
Try this code:
select Name from table where Name like '%[^0-9a-zA-Z ]%'
Thank you for replying. I had tried your suggestions but I was still getting more results. However, it looks like you can get very specific with the exclusion. Eventually I ended up adding results from the data I got.
Like this:
select Name from table where Name LIKE '%[^0-9a-zA-Z() -._/\:=,]%'
This finally gave me what I was looking for. Although new issue I have now is how to suppress the [] brackets which apparently also are found in the data:
???? ?????
HP PCI 10/100Base-TX Core [100BASE-TX,FD,AUTO,TT=1500]
Fixed?????
Adding those brackets into the query breaks the array boundary:
'%[^0-9a-zA-Z() -._/\:=,**[]**]%'
However, this is something I can handle. As long as I am not getting "all" the data.
LIKE '%[^0-9a-zA-Z]%'
numbers (0-9), lowercase alphas (a-z), uppercase alphas (A-Z). The "^" makes that a "NOT" one of these things
In SQL Server, I am trying to select some of the records using a string which has space so I trim and use but something is wrong please correct me where something is missed by me.
SELECT * FROM projects where str_id=ltrim(rtrim(' artf130 ')) --- No rows selected
SELECT * FROM projects where str_id='artf130' -- one row selected
Update: I copied the first line from google spread sheet.
Maybe that was my bad. People keep helping.
I think my comment was enough, cause I linked to a very similar problem, which got a answer. So here for everyone:
You can see the answer to that question here.
I realize there are similar questions here but none of them are exactly what I need. I have a SQL sproc that part of it executes a view. I have 3 databases that are identical in design and function but the data is different and I am getting this error on only one of them. If i take the view from the erroring DB and execute it on one of the others, it runs just fine. The weirdest part is that this view worked, then stopped, then fixed itself and started working again and when I started making changes to some data, it stopped again.
I have boiled it down to this specific line;
Left(AO.Name,PATINDEX('%-%',AO.Name)-1) as ColumnName
The interesting part is if i change '%-% to be '%' the error goes away and my SPROC executes properly.
I have five questions;
Why am i getting this error when I have the '%-%' but not '%'? Whats the difference and what kind of a change is made by using one vs the other?
What does this error mean?
Why would the exact same view work properly on two other identical databases but not this one?
Why would this stop working after it was working, then somehow fix itself and then break itself again after some data manipulation?
Is there a better method outside of PATINDEX that I could use?
Any help is appreciated. Thank you.
This is going to happen when there is no hyphen in the string.
First, you can replace PATINDEX() with CHARINDEX() in this case:
LEFT(AO.Name, CHARINDEX('-', AO.Name) - 1) as ColumnName
That doesn't fix the problem, but it is simpler to follow.
Then, the problem is that AO.Name has no hyphen. In that case PATINDEX() (and CHARINDEX() too) returns 0. You subtract 1 and the value is illegal as a second argument to LEFT().
The easiest solution is to put a hyphen at the end:
LEFT(AO.Name, CHARINDEX('-', AO.Name + '-') - 1) as ColumnName