Created
July 1, 2015 15:26
-
-
Save ststeiger/8d792411d7884b9b04d5 to your computer and use it in GitHub Desktop.
Remove all non-number chars from varchar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fu_RPT_RemoveChars]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT')) | |
| DROP FUNCTION [dbo].[fu_RPT_RemoveChars] | |
| GO | |
| CREATE FUNCTION [dbo].[fu_RPT_RemoveChars](@Input varchar(50)) | |
| RETURNS VARCHAR(1000) | |
| BEGIN | |
| DECLARE @pos INT | |
| SET @Pos = PATINDEX('%[^0-9]%',@Input) | |
| WHILE @Pos > 0 | |
| BEGIN | |
| SET @Input = STUFF(@Input,@pos,1,'') | |
| SET @Pos = PATINDEX('%[^0-9]%',@Input) | |
| END | |
| RETURN @Input | |
| END | |
| GO | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment