SQL Server 2000 - How to remove the hidden characters in the column? - sql

I'm trying to remove a hidden characters from a varchar column, these hidden characters (i.e. period, space) was taken from a scanned bar code and it is not visible in the result set once query was executed. I have tried to use below script but it failed to remove the hidden characters(see attached screenshot for reference.)
Any help is highly appreciated.
SELECT Replace(Replace(LTrim(RTrim(mycolumn)), '.', ''), ' ', '')
FROM MyTable
WHERE serialno = '123456789'

One thing that has worked for me is to select the column with the special characters, then paste the data into notepad++ then turn on View>Show Symbol>Show All Characters. Then I could copy the special characters from Notepad++ into the second argument of the REPLACE() function in SQL.

Related

Removing white spaces and special characters from SQL

I have a table where I have a ColumnA which has data with white spaces and special characters. I want to generate ColumnB with the data from ColumnA with the removal of white spaces and special characters.
For example, ColumnA has values like:
N/A
#email
Hot-topic
#sql#%
White paper.
I want a new column with values:
NA
email
HotTopic
sql
Whitepaper
I tried below SQL in SSMS, but it is not working completely. Could someone help me out?
SELECT code,
REPLACE(REPLACE(code, TRIM(TRANSLATE(code,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',' '))
,'') ,' ','')
FROM SAMP
It is not working for the record with value: #sql#%
Added as a wiki answer in order to retain the comment made by #lptr. Query by #lptr explanation mine (#DaleK).
Your attempt was close, but only worked for single characters... the one that failed was because you had multiple characters that needed replacing and once you remove the white space they are all next to each other and don't match the original string anymore.
This answer cleverly replaces all the letter characters with a "*" using translate as step 1, then using translate again on the original column value, replaces all the non-letter characters with a "*" as step 2, then finally replaces all "*" characters with an empty string.
Note also the use of replication to avoid typing the same character in multiple times.
create table samp(code varchar(50));
insert into samp(code)
values
('N/A'),
('#email'),
('Hot-topic'),
('#sql#%'),
('White paper. ');
select s.code, n.nonletters, l.letters
from samp as s
cross apply (values(translate(s.code, 'abcdefghijklmnopqrstuvwxyz', replicate('*', 26)))) as n (nonletters)
cross apply (values(replace(translate(s.code, n.nonletters, replicate('*', len(n.nonletters+'.')-1)), '*', ''))) as l (letters);

SSIS Flat File destination Comma separated after each fixed column value (After Fixed Length of column)

I've spent almost a day to find a solution for this, but so far no luck.
Here is the problem:
In my SSIS Package I'm trying to export data from SQL server table to flat file destination with fixed length option followed by comma.
Ex: Account Address Zip
123456 ,2525 Est dr ,77077
563 ,12 we dr ,75023
If you see the above example the Account is 10 character length, and Address is 16 character length.
So I need write like this to a flat file with fixed length followed by comma (in other words comma should come between the columns)
But the in flat file destination of SSIS, I am not able to set both Fixed length and comma delimiter at the same time. Either one is working , but not both.
Please suggests me the options, to get this behavior.
That is an odd schema for a flatfile, but OK. Set the flatfile to be fixed with. Then pad the columns with trailing spaces and a comma yourself. Two methods come to mind.
Update your OLE DB Sourcecomponent to use a SQL statement that does the padding for you ..
SELECT
Account + REPLICATE(' ',10 - LEN(Account)) + ',',
Address + REPLICATE(' ',16 - LEN(Address)) + ',',
Zip + REPLICATE(' ',5 - LEN(Zip)) + ','
FROM MyTable
Use a Derived Column Transformation to do a similar thing.

White spaces in sql

that will sounds stupid, but I have a table with names, those names may finish with white space or may not. E.g. I have name ' dummy ', but even if in the query I write only ' dummy' it will find the record ' dummy '. Can I fix it somehow?
SELECT *
FROM MYTABLE where NAME=' dummy'
Thanks
This is how SQL works (except Oracle), when you compare two strings the shorter one will be padded with blanks to the length of th 2nd string.
If you really need to consider trailings blanks you can switch to LIKE which doesn't follow that rule:
SELECT *
FROM MYTABLE where NAME LIKE ' dummy'
Of course, you better clean your data during load.
There's only one thing which is worse than trailing spaces, leading spaces (oh, wait a minute, you got them, too).

Query for blank white space before AND after a number string

How would i go about constructing a query, that would return all material numbers that have a "blank white space" either BEFORE or AFTER the number string? We are exporting straight from SSMS to excel and we see the problem in the spreadsheet. If i could return all of the material numbers with spaces.. i could go in and edit them or do a replace to fix this issue prior to exporting! (the mtrl numbers are imported in via a windows application that users upload an excel template to. This template has all of this data and sometimes they place in spaces in or after the material number). The query we have used to work but now it does not return anything, but upon export we identify these problems you see highlighted in the screenshot (left screenshot) and then query to find that mtrl # in the table (right screenshot). And indeed, it has a space before the 1.
Currently the query we use looks like:
SELECT Mtrl
FROM dbo.Source
WHERE Mtrl LIKE '% %'
Since you are getting the data from a query, you should just have that query remove any potential spaces using LTRIM and RTRIM:
LTRIM(RTRIM([MTRL]))
Keep in mind that these two commands remove only spaces, not tabs or returns or other white-space characters.
Doing the above will make sure that the data for the entire set of data is fine, whether or not you find it and/or fix it.
Or, since you are copying-and-pasting from the Results Grid into Excel, you can just CONVERT the value to a number which will naturally remove any spaces:
SELECT CONVERT(INT, ' 12 ');
Returns:
12
So you would just use:
CONVERT(INT, [MRTL])
Now, if you want to find the data that has anything that is not a digit in it, you would use this:
SELECT Mtrl
FROM dbo.Source
WHERE [Mtrl] LIKE '%[^0-9]%'; -- any single non-digit character
If the issue is with non-space white-space characters, you can find out which ones they are via the following (to find them at the beginning instead of at the end, change the RIGHT to be LEFT):
;WITH cte AS
(
SELECT UNICODE(RIGHT([MTRL], 1)) AS [CharVal]
FROM dbo.Source
)
SELECT *
FROM cte
WHERE cte.[CharVal] NOT BETWEEN 48 AND 57 -- digits 0 - 9
AND cte.[CharVal] <> 32; -- space
And you can fix in one shot using the following, which removes regular spaces (char 32 via LTRIM/RTRIM), tabs (char 9), and non-breaking spaces (char 160):
UPDATE src
SET src.[Mtrl] = REPLACE(
REPLACE(
LTRIM(RTRIM(src.[Mtrl])),
CHAR(160),
''),
CHAR(9),
'')
FROM dbo.Source src
WHERE src.[Mtrl] LIKE '%[' -- find rows with any of the following characters
+ CHAR(9) -- tab
+ CHAR(32) -- space
+ CHAR(160) -- non-breaking space
+ ']%';
Here I used the same WHERE condition that you have since if there can't be any spaces then it doesn't matter if you check both ends or for any at all (and maybe it is faster to have a single LIKE instead of two).

Unwanted Spaces when concatenating fields and text strings

I'm putting the following in my SQL select statement to concatenate text strings (which are not fields in the database) with a couple database fields and I'm getting spaces where I try to use the to_char function to add leading zeros to a couple fields.
Running:
SELECT 'EP.'||TO_CHAR(PWROTPR_TRANSX_NBR,'0000000')||'.'||TO_CHAR(PWROTPR_SUBMIT_COUNTER,'00') as ATS_NBR
Yields:
EP. 0017092. 01
How to I eliminate the unnecessary spaces?
Try using replace function:
SELECT replace(('EP.'||TO_CHAR(PWROTPR_TRANSX_NBR,'0000000')||'.'||TO_CHAR(PWROTPR_SUBMIT_COUNTER,'00')), ' ', '') as ATS_NBR