Regrex lọc kí tự Chuỗi-Số trong SQL Server

Theo: nguyenhaidang.name.vn | 03/07/2017 - 04:13

Create table

CREATE TABLE [dbo].[tab](

[Name] [varchar](50) NULL

) ON [PRIMARY]

 

GO

Insert data to table

Insert into [tab] value ''123''

Insert into [tab] value ''abc''

Insert into [tab] value ''ABC''

Insert into [tab] value ''12aa''

Insert into [tab] value ''ax1205''

Insert into [tab] value ''1q2d''

Insert into [tab] value ''b2v2sdfsf331313213''

 

Create function

CREATE FUNCTION [dbo].[fnReplaceInvalidChars] (@string VARCHAR(300))

RETURNS VARCHAR(300)

BEGIN

    DECLARE @str VARCHAR(300) = @string;

    while PatIndex(''%[^0-9]%'', @str) > 0

    select  @str = Replace(@str, Substring(@str, PatIndex(''%[^0-9]%'', @str), 1), '''')   

    RETURN @str

 

END

 

Select and result:

select dbo.fnReplaceInvalidChars(name),* from tab

 

Back Head Print
Tin khác

Search GridView with Paging on TextBox KeyPress using jQuery in ASP.Net    (28/07/2010)

Bootstrap AutoComplete TextBox example using jQuery TypeAhead plugin in ASP.Net with C# and VB.Net    (28/07/2010)

Disable Button and Submit button after one click using JavaScript and jQuery    (29/07/2010)

Split and convert Comma Separated (Delimited) String to Table in SQL Server    (01/09/2010)

Select Column values as Comma Separated (Delimited) string in SQL Server using COALESCE    (01/09/2010)