Białe znaki w MS SQL
Migracja; autor Wojtek, ostatnia modyfikacja przed importem 2014-05-30T13:58:57.000Z; Białe-znaki-w-MS-SQL
Jest problem z tym co może operator wkleić do formatki jakiejś bazy
A w przypadku adresu E-Mail to przeważnie metoda Kopiuj i Wklej, która skutkuje tym że do pola są kopiowane niewidoczne znaki (tzw. białe znaki) i dla operatora wygląda wszystko dobrze ale jak już mamy wysłać na taki adres E-Mail jest problem bo komputer nie przepuści nic nawet jeśli nie widać że występuje
Dlatego napisałem kilka funkcji pomocnych do identyfikacji problemu i jego naprawy
dbo.IsAllWhiteSpace(<varchar(MAX)>)- funkcja zwracam True jak w @Str będzie choć jedno wystąpienie znaku z ASCII (0-31,255)dbo.IsWhiteSpace(<varchar(MAX)>)- funkcja zwracam True jak w @Str będzie choć jedno wystąpienie znaku z ASCII (0-9, 11-12, 14-31, 255)dbo.TrimAllWhiteSpace(<varchar(MAX)>)- funkcja usuwam znaki z zakresu ASCII (0-31,255) z @Strdbo.TrimWhiteSpace(<varchar(MAX)>)- funkcja usuwam znaki z zakresu ASCII (0-9, 11-12, 14-31, 255) z @Str
Przykłady użycia:
select '-'+YKO_Mail+'-', YKO_Akronim, YKO_Archiwum
from YKontrahent
where dbo.IsAllWhiteSpace( YKO_Mail) = 1
select *
from YKontrahent join YAdres_Kontrahent on YKO_IdAdres = YAK_Id
where isnull( yko_archiwum,0)= 0 and dbo.IsWhiteSpace( YAK_Nazwa)=1
Kod funkcji
drop function dbo.IsAllWhiteSpace
drop function dbo.IsWhiteSpace
drop function dbo.TrimAllWhiteSpace
drop function dbo.TrimWhiteSpace
GO
-- Zwracam True jak w @Str będzie choć jedno wystąpienie znaku z ASCII (0-31,255)
create function dbo.IsAllWhiteSpace(@Str varchar(8000))
returns BIT
as
begin
declare @i int
set @i = 0
if @Str is null
begin
return 0
end
while @i < 32
begin
if CharIndex( Char( @i), @str) > 0
begin
return 1
end
set @i = @i+1
end
if CharIndex( Char( 255), @str) > 0
begin
return 1
end
return 0 -- nie znalazłem
end
GO
-- Zwracam True jak w @Str będzie choć jedno wystąpienie znaku z ASCII (0-9, 11-12, 14-31, 255)
create function dbo.IsWhiteSpace(@Str varchar(8000))
returns BIT
as
begin
declare @i int
set @i = 0
if @Str is null
begin
return 0
end
while @i < 10
begin
if CharIndex( Char( @i), @str) > 0
begin
return 1
end
set @i = @i+1
end
set @i = 14
while @i < 32
begin
if CharIndex( Char( @i), @str) > 0
begin
return 1
end
set @i = @i+1
end
if (CharIndex( Char( 11), @str) > 0) Or (CharIndex( Char( 12), @str) > 0) Or (CharIndex( Char( 255), @str) > 0)
begin
return 1
end
return 0 -- nie znalazłem
end
GO
-- Usuwam znaki z zakresu ASCII (0-31,255) z @Str
create function dbo.TrimAllWhiteSpace(@Str varchar(8000))
returns varchar(8000)
as
begin
declare @ret varchar(8000)
declare @i int
if @Str is null
begin
return null
end
set @ret = @Str
set @i = 0
while @i < 32
begin
set @ret = Replace( @ret, Char( @i), '')
set @i = @i+1
end
set @ret = Replace( @ret, Char( 255), '')
return @ret
end
GO
-- Usuwam znaki z zakresu ASCII (0-9, 11-12, 14-31, 255) z @Str
create function dbo.TrimWhiteSpace(@Str varchar(8000))
returns varchar(8000)
as
begin
declare @ret varchar(8000)
declare @i int
if @Str is null
begin
return null
end
set @ret = @Str
set @i = 0
while @i < 10
begin
set @ret = Replace( @ret, Char( @i), '')
set @i = @i+1
end
set @i = 14
while @i < 32
begin
set @ret = Replace( @ret, Char( @i), '')
set @i = @i+1
end
set @ret = Replace( @ret, Char( 11), '')
set @ret = Replace( @ret, Char( 12), '')
set @ret = Replace( @ret, Char( 255), '')
return @ret
end
GO