Remove numbers from string sql server
I have a large database in which I want to do a part string search. The user will enter characters: JoeBloggs.
For arguments sake if I had a name Joe 23 Blo Ggs 4
in the database. I want to remove everything in the name other than A-Z.
I have the REPLACE(Name, ' ','')
function to remove spaces and the UPPER()
function to capitalize the name.
Is there a more efficient fast way maybe by terms of regex to replace anything other than A-Z. I cannot change the values in the database.
Thanks in advance
sql sql-server sql-server-2008
add a comment |
I have a large database in which I want to do a part string search. The user will enter characters: JoeBloggs.
For arguments sake if I had a name Joe 23 Blo Ggs 4
in the database. I want to remove everything in the name other than A-Z.
I have the REPLACE(Name, ' ','')
function to remove spaces and the UPPER()
function to capitalize the name.
Is there a more efficient fast way maybe by terms of regex to replace anything other than A-Z. I cannot change the values in the database.
Thanks in advance
sql sql-server sql-server-2008
You say you can't change the data. Can you add a computed column to the existing table? Or add a new table with a foreign key and your computed value?
– Laurence
Nov 5 '12 at 21:04
@Laurence - Yea I suppose I can do this but can we do this in a temporary table in a stored procedure? If not then I can request this computed column, all i need is the function to do the replace. Thanks for your prompt response
– CR41G14
Nov 5 '12 at 21:07
I'd not recommend using regexps if performance matters. The way you use is obvious but still a good one!
– vyakhir
Nov 5 '12 at 21:13
@DmitryVyakhirev - There could be some rouge characters like ' " @ /, it is a messy database so will I need to do multiple REPLACES to get rid of every instance of a bad character
– CR41G14
Nov 5 '12 at 21:19
add a comment |
I have a large database in which I want to do a part string search. The user will enter characters: JoeBloggs.
For arguments sake if I had a name Joe 23 Blo Ggs 4
in the database. I want to remove everything in the name other than A-Z.
I have the REPLACE(Name, ' ','')
function to remove spaces and the UPPER()
function to capitalize the name.
Is there a more efficient fast way maybe by terms of regex to replace anything other than A-Z. I cannot change the values in the database.
Thanks in advance
sql sql-server sql-server-2008
I have a large database in which I want to do a part string search. The user will enter characters: JoeBloggs.
For arguments sake if I had a name Joe 23 Blo Ggs 4
in the database. I want to remove everything in the name other than A-Z.
I have the REPLACE(Name, ' ','')
function to remove spaces and the UPPER()
function to capitalize the name.
Is there a more efficient fast way maybe by terms of regex to replace anything other than A-Z. I cannot change the values in the database.
Thanks in advance
sql sql-server sql-server-2008
sql sql-server sql-server-2008
asked Nov 5 '12 at 21:00
CR41G14CR41G14
3,17653156
3,17653156
You say you can't change the data. Can you add a computed column to the existing table? Or add a new table with a foreign key and your computed value?
– Laurence
Nov 5 '12 at 21:04
@Laurence - Yea I suppose I can do this but can we do this in a temporary table in a stored procedure? If not then I can request this computed column, all i need is the function to do the replace. Thanks for your prompt response
– CR41G14
Nov 5 '12 at 21:07
I'd not recommend using regexps if performance matters. The way you use is obvious but still a good one!
– vyakhir
Nov 5 '12 at 21:13
@DmitryVyakhirev - There could be some rouge characters like ' " @ /, it is a messy database so will I need to do multiple REPLACES to get rid of every instance of a bad character
– CR41G14
Nov 5 '12 at 21:19
add a comment |
You say you can't change the data. Can you add a computed column to the existing table? Or add a new table with a foreign key and your computed value?
– Laurence
Nov 5 '12 at 21:04
@Laurence - Yea I suppose I can do this but can we do this in a temporary table in a stored procedure? If not then I can request this computed column, all i need is the function to do the replace. Thanks for your prompt response
– CR41G14
Nov 5 '12 at 21:07
I'd not recommend using regexps if performance matters. The way you use is obvious but still a good one!
– vyakhir
Nov 5 '12 at 21:13
@DmitryVyakhirev - There could be some rouge characters like ' " @ /, it is a messy database so will I need to do multiple REPLACES to get rid of every instance of a bad character
– CR41G14
Nov 5 '12 at 21:19
You say you can't change the data. Can you add a computed column to the existing table? Or add a new table with a foreign key and your computed value?
– Laurence
Nov 5 '12 at 21:04
You say you can't change the data. Can you add a computed column to the existing table? Or add a new table with a foreign key and your computed value?
– Laurence
Nov 5 '12 at 21:04
@Laurence - Yea I suppose I can do this but can we do this in a temporary table in a stored procedure? If not then I can request this computed column, all i need is the function to do the replace. Thanks for your prompt response
– CR41G14
Nov 5 '12 at 21:07
@Laurence - Yea I suppose I can do this but can we do this in a temporary table in a stored procedure? If not then I can request this computed column, all i need is the function to do the replace. Thanks for your prompt response
– CR41G14
Nov 5 '12 at 21:07
I'd not recommend using regexps if performance matters. The way you use is obvious but still a good one!
– vyakhir
Nov 5 '12 at 21:13
I'd not recommend using regexps if performance matters. The way you use is obvious but still a good one!
– vyakhir
Nov 5 '12 at 21:13
@DmitryVyakhirev - There could be some rouge characters like ' " @ /, it is a messy database so will I need to do multiple REPLACES to get rid of every instance of a bad character
– CR41G14
Nov 5 '12 at 21:19
@DmitryVyakhirev - There could be some rouge characters like ' " @ /, it is a messy database so will I need to do multiple REPLACES to get rid of every instance of a bad character
– CR41G14
Nov 5 '12 at 21:19
add a comment |
6 Answers
6
active
oldest
votes
1st option -
You can nest REPLACE()
functions up to 32 levels deep. It runs fast.
REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE (@str, '0', ''),
'1', ''),
'2', ''),
'3', ''),
'4', ''),
'5', ''),
'6', ''),
'7', ''),
'8', ''),
'9', '')
2nd option --
do the reverse of -
Removing nonnumerical data out of a number + SQL
3rd option - if you want to use regex
then
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=27205
add a comment |
This one works for me
CREATE Function [dbo].[RemoveNumericCharacters](@Temp VarChar(1000))
Returns VarChar(1000)
AS
Begin
Declare @NumRange as varchar(50) = '%[0-9]%'
While PatIndex(@NumRange, @Temp) > 0
Set @Temp = Stuff(@Temp, PatIndex(@NumRange, @Temp), 1, '')
Return @Temp
End
and you can use it like so
SELECT dbo.[RemoveNumericCharacters](Name) FROM TARGET_TABLE
add a comment |
Try below for your query. where val is your string or column name.
CASE WHEN PATINDEX('%[a-z]%', REVERSE(val)) > 1
THEN LEFT(val, LEN(val) - PATINDEX('%[a-z]%', REVERSE(val)) + 1)
ELSE '' END
I've replacedELSE '' END
withELSE val END
so it also returns when no numbers are in it.
– KoalaBear
Dec 10 '18 at 6:56
add a comment |
One more approach using Recursive CTE..
declare @string varchar(100)
set @string ='te165st1230004616161616'
;With cte
as
(
select @string as string,0 as n
union all
select cast(replace(string,n,'') as varchar(100)),n+1
from cte
where n<9
)
select top 1 string from cte
order by n desc
**Output:**
test
add a comment |
Quoting part of @Jatin answer with some modifications,
use this in your where
statement:
SELECT * FROM .... etc.
Where
REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE (Name, '0', ''),
'1', ''),
'2', ''),
'3', ''),
'4', ''),
'5', ''),
'6', ''),
'7', ''),
'8', ''),
'9', '') = P_SEARCH_KEY
add a comment |
Not tested, but you can do something like this:
Create Function dbo.AlphasOnly(@s as varchar(max)) Returns varchar(max) As
Begin
Declare @Pos int = 1
Declare @Ret varchar(max) = null
If @s Is Not Null
Begin
Set @Ret = ''
While @Pos <= Len(@s)
Begin
If SubString(@s, @Pos, 1) Like '[A-Za-z]'
Begin
Set @Ret = @Ret + SubString(@s, @Pos, 1)
End
Set @Pos = @Pos + 1
End
End
Return @Ret
End
The key is to use this as a computed column and index it. It doesn't really matter how fast you make this function if the database has to execute it against every row in your large table every time you run the query.
very slow solution, fast is required
– CR41G14
Nov 5 '12 at 21:36
If you insist on doingSelect name from test where dbo.AlphasOnly(name) = 'JoeBloggs'
then this will be slow even if the function takes 0 time. You still pay the cost of reading all the rows off the disk (assuming when you say large table you mean your database doesn't fit in memory). To make it fast you need to find a way of indexing it, which is why I suggest a computed column.
– Laurence
Nov 5 '12 at 22:45
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13240298%2fremove-numbers-from-string-sql-server%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
1st option -
You can nest REPLACE()
functions up to 32 levels deep. It runs fast.
REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE (@str, '0', ''),
'1', ''),
'2', ''),
'3', ''),
'4', ''),
'5', ''),
'6', ''),
'7', ''),
'8', ''),
'9', '')
2nd option --
do the reverse of -
Removing nonnumerical data out of a number + SQL
3rd option - if you want to use regex
then
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=27205
add a comment |
1st option -
You can nest REPLACE()
functions up to 32 levels deep. It runs fast.
REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE (@str, '0', ''),
'1', ''),
'2', ''),
'3', ''),
'4', ''),
'5', ''),
'6', ''),
'7', ''),
'8', ''),
'9', '')
2nd option --
do the reverse of -
Removing nonnumerical data out of a number + SQL
3rd option - if you want to use regex
then
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=27205
add a comment |
1st option -
You can nest REPLACE()
functions up to 32 levels deep. It runs fast.
REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE (@str, '0', ''),
'1', ''),
'2', ''),
'3', ''),
'4', ''),
'5', ''),
'6', ''),
'7', ''),
'8', ''),
'9', '')
2nd option --
do the reverse of -
Removing nonnumerical data out of a number + SQL
3rd option - if you want to use regex
then
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=27205
1st option -
You can nest REPLACE()
functions up to 32 levels deep. It runs fast.
REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE (@str, '0', ''),
'1', ''),
'2', ''),
'3', ''),
'4', ''),
'5', ''),
'6', ''),
'7', ''),
'8', ''),
'9', '')
2nd option --
do the reverse of -
Removing nonnumerical data out of a number + SQL
3rd option - if you want to use regex
then
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=27205
edited Aug 1 '18 at 22:35
Brien Foss
2,43631226
2,43631226
answered Nov 5 '12 at 21:19
JatinJatin
1,17221123
1,17221123
add a comment |
add a comment |
This one works for me
CREATE Function [dbo].[RemoveNumericCharacters](@Temp VarChar(1000))
Returns VarChar(1000)
AS
Begin
Declare @NumRange as varchar(50) = '%[0-9]%'
While PatIndex(@NumRange, @Temp) > 0
Set @Temp = Stuff(@Temp, PatIndex(@NumRange, @Temp), 1, '')
Return @Temp
End
and you can use it like so
SELECT dbo.[RemoveNumericCharacters](Name) FROM TARGET_TABLE
add a comment |
This one works for me
CREATE Function [dbo].[RemoveNumericCharacters](@Temp VarChar(1000))
Returns VarChar(1000)
AS
Begin
Declare @NumRange as varchar(50) = '%[0-9]%'
While PatIndex(@NumRange, @Temp) > 0
Set @Temp = Stuff(@Temp, PatIndex(@NumRange, @Temp), 1, '')
Return @Temp
End
and you can use it like so
SELECT dbo.[RemoveNumericCharacters](Name) FROM TARGET_TABLE
add a comment |
This one works for me
CREATE Function [dbo].[RemoveNumericCharacters](@Temp VarChar(1000))
Returns VarChar(1000)
AS
Begin
Declare @NumRange as varchar(50) = '%[0-9]%'
While PatIndex(@NumRange, @Temp) > 0
Set @Temp = Stuff(@Temp, PatIndex(@NumRange, @Temp), 1, '')
Return @Temp
End
and you can use it like so
SELECT dbo.[RemoveNumericCharacters](Name) FROM TARGET_TABLE
This one works for me
CREATE Function [dbo].[RemoveNumericCharacters](@Temp VarChar(1000))
Returns VarChar(1000)
AS
Begin
Declare @NumRange as varchar(50) = '%[0-9]%'
While PatIndex(@NumRange, @Temp) > 0
Set @Temp = Stuff(@Temp, PatIndex(@NumRange, @Temp), 1, '')
Return @Temp
End
and you can use it like so
SELECT dbo.[RemoveNumericCharacters](Name) FROM TARGET_TABLE
answered Feb 25 '13 at 16:40
Ben AndersonBen Anderson
4,47333137
4,47333137
add a comment |
add a comment |
Try below for your query. where val is your string or column name.
CASE WHEN PATINDEX('%[a-z]%', REVERSE(val)) > 1
THEN LEFT(val, LEN(val) - PATINDEX('%[a-z]%', REVERSE(val)) + 1)
ELSE '' END
I've replacedELSE '' END
withELSE val END
so it also returns when no numbers are in it.
– KoalaBear
Dec 10 '18 at 6:56
add a comment |
Try below for your query. where val is your string or column name.
CASE WHEN PATINDEX('%[a-z]%', REVERSE(val)) > 1
THEN LEFT(val, LEN(val) - PATINDEX('%[a-z]%', REVERSE(val)) + 1)
ELSE '' END
I've replacedELSE '' END
withELSE val END
so it also returns when no numbers are in it.
– KoalaBear
Dec 10 '18 at 6:56
add a comment |
Try below for your query. where val is your string or column name.
CASE WHEN PATINDEX('%[a-z]%', REVERSE(val)) > 1
THEN LEFT(val, LEN(val) - PATINDEX('%[a-z]%', REVERSE(val)) + 1)
ELSE '' END
Try below for your query. where val is your string or column name.
CASE WHEN PATINDEX('%[a-z]%', REVERSE(val)) > 1
THEN LEFT(val, LEN(val) - PATINDEX('%[a-z]%', REVERSE(val)) + 1)
ELSE '' END
edited Jan 6 '18 at 8:06
answered Jan 2 '18 at 10:02
SutirthSutirth
785814
785814
I've replacedELSE '' END
withELSE val END
so it also returns when no numbers are in it.
– KoalaBear
Dec 10 '18 at 6:56
add a comment |
I've replacedELSE '' END
withELSE val END
so it also returns when no numbers are in it.
– KoalaBear
Dec 10 '18 at 6:56
I've replaced
ELSE '' END
with ELSE val END
so it also returns when no numbers are in it.– KoalaBear
Dec 10 '18 at 6:56
I've replaced
ELSE '' END
with ELSE val END
so it also returns when no numbers are in it.– KoalaBear
Dec 10 '18 at 6:56
add a comment |
One more approach using Recursive CTE..
declare @string varchar(100)
set @string ='te165st1230004616161616'
;With cte
as
(
select @string as string,0 as n
union all
select cast(replace(string,n,'') as varchar(100)),n+1
from cte
where n<9
)
select top 1 string from cte
order by n desc
**Output:**
test
add a comment |
One more approach using Recursive CTE..
declare @string varchar(100)
set @string ='te165st1230004616161616'
;With cte
as
(
select @string as string,0 as n
union all
select cast(replace(string,n,'') as varchar(100)),n+1
from cte
where n<9
)
select top 1 string from cte
order by n desc
**Output:**
test
add a comment |
One more approach using Recursive CTE..
declare @string varchar(100)
set @string ='te165st1230004616161616'
;With cte
as
(
select @string as string,0 as n
union all
select cast(replace(string,n,'') as varchar(100)),n+1
from cte
where n<9
)
select top 1 string from cte
order by n desc
**Output:**
test
One more approach using Recursive CTE..
declare @string varchar(100)
set @string ='te165st1230004616161616'
;With cte
as
(
select @string as string,0 as n
union all
select cast(replace(string,n,'') as varchar(100)),n+1
from cte
where n<9
)
select top 1 string from cte
order by n desc
**Output:**
test
answered Aug 17 '16 at 13:20
TheGameiswarTheGameiswar
21.4k53063
21.4k53063
add a comment |
add a comment |
Quoting part of @Jatin answer with some modifications,
use this in your where
statement:
SELECT * FROM .... etc.
Where
REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE (Name, '0', ''),
'1', ''),
'2', ''),
'3', ''),
'4', ''),
'5', ''),
'6', ''),
'7', ''),
'8', ''),
'9', '') = P_SEARCH_KEY
add a comment |
Quoting part of @Jatin answer with some modifications,
use this in your where
statement:
SELECT * FROM .... etc.
Where
REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE (Name, '0', ''),
'1', ''),
'2', ''),
'3', ''),
'4', ''),
'5', ''),
'6', ''),
'7', ''),
'8', ''),
'9', '') = P_SEARCH_KEY
add a comment |
Quoting part of @Jatin answer with some modifications,
use this in your where
statement:
SELECT * FROM .... etc.
Where
REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE (Name, '0', ''),
'1', ''),
'2', ''),
'3', ''),
'4', ''),
'5', ''),
'6', ''),
'7', ''),
'8', ''),
'9', '') = P_SEARCH_KEY
Quoting part of @Jatin answer with some modifications,
use this in your where
statement:
SELECT * FROM .... etc.
Where
REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE (Name, '0', ''),
'1', ''),
'2', ''),
'3', ''),
'4', ''),
'5', ''),
'6', ''),
'7', ''),
'8', ''),
'9', '') = P_SEARCH_KEY
answered Aug 20 '14 at 8:28
simsimsimsim
1,47521540
1,47521540
add a comment |
add a comment |
Not tested, but you can do something like this:
Create Function dbo.AlphasOnly(@s as varchar(max)) Returns varchar(max) As
Begin
Declare @Pos int = 1
Declare @Ret varchar(max) = null
If @s Is Not Null
Begin
Set @Ret = ''
While @Pos <= Len(@s)
Begin
If SubString(@s, @Pos, 1) Like '[A-Za-z]'
Begin
Set @Ret = @Ret + SubString(@s, @Pos, 1)
End
Set @Pos = @Pos + 1
End
End
Return @Ret
End
The key is to use this as a computed column and index it. It doesn't really matter how fast you make this function if the database has to execute it against every row in your large table every time you run the query.
very slow solution, fast is required
– CR41G14
Nov 5 '12 at 21:36
If you insist on doingSelect name from test where dbo.AlphasOnly(name) = 'JoeBloggs'
then this will be slow even if the function takes 0 time. You still pay the cost of reading all the rows off the disk (assuming when you say large table you mean your database doesn't fit in memory). To make it fast you need to find a way of indexing it, which is why I suggest a computed column.
– Laurence
Nov 5 '12 at 22:45
add a comment |
Not tested, but you can do something like this:
Create Function dbo.AlphasOnly(@s as varchar(max)) Returns varchar(max) As
Begin
Declare @Pos int = 1
Declare @Ret varchar(max) = null
If @s Is Not Null
Begin
Set @Ret = ''
While @Pos <= Len(@s)
Begin
If SubString(@s, @Pos, 1) Like '[A-Za-z]'
Begin
Set @Ret = @Ret + SubString(@s, @Pos, 1)
End
Set @Pos = @Pos + 1
End
End
Return @Ret
End
The key is to use this as a computed column and index it. It doesn't really matter how fast you make this function if the database has to execute it against every row in your large table every time you run the query.
very slow solution, fast is required
– CR41G14
Nov 5 '12 at 21:36
If you insist on doingSelect name from test where dbo.AlphasOnly(name) = 'JoeBloggs'
then this will be slow even if the function takes 0 time. You still pay the cost of reading all the rows off the disk (assuming when you say large table you mean your database doesn't fit in memory). To make it fast you need to find a way of indexing it, which is why I suggest a computed column.
– Laurence
Nov 5 '12 at 22:45
add a comment |
Not tested, but you can do something like this:
Create Function dbo.AlphasOnly(@s as varchar(max)) Returns varchar(max) As
Begin
Declare @Pos int = 1
Declare @Ret varchar(max) = null
If @s Is Not Null
Begin
Set @Ret = ''
While @Pos <= Len(@s)
Begin
If SubString(@s, @Pos, 1) Like '[A-Za-z]'
Begin
Set @Ret = @Ret + SubString(@s, @Pos, 1)
End
Set @Pos = @Pos + 1
End
End
Return @Ret
End
The key is to use this as a computed column and index it. It doesn't really matter how fast you make this function if the database has to execute it against every row in your large table every time you run the query.
Not tested, but you can do something like this:
Create Function dbo.AlphasOnly(@s as varchar(max)) Returns varchar(max) As
Begin
Declare @Pos int = 1
Declare @Ret varchar(max) = null
If @s Is Not Null
Begin
Set @Ret = ''
While @Pos <= Len(@s)
Begin
If SubString(@s, @Pos, 1) Like '[A-Za-z]'
Begin
Set @Ret = @Ret + SubString(@s, @Pos, 1)
End
Set @Pos = @Pos + 1
End
End
Return @Ret
End
The key is to use this as a computed column and index it. It doesn't really matter how fast you make this function if the database has to execute it against every row in your large table every time you run the query.
answered Nov 5 '12 at 21:24
LaurenceLaurence
10.2k11931
10.2k11931
very slow solution, fast is required
– CR41G14
Nov 5 '12 at 21:36
If you insist on doingSelect name from test where dbo.AlphasOnly(name) = 'JoeBloggs'
then this will be slow even if the function takes 0 time. You still pay the cost of reading all the rows off the disk (assuming when you say large table you mean your database doesn't fit in memory). To make it fast you need to find a way of indexing it, which is why I suggest a computed column.
– Laurence
Nov 5 '12 at 22:45
add a comment |
very slow solution, fast is required
– CR41G14
Nov 5 '12 at 21:36
If you insist on doingSelect name from test where dbo.AlphasOnly(name) = 'JoeBloggs'
then this will be slow even if the function takes 0 time. You still pay the cost of reading all the rows off the disk (assuming when you say large table you mean your database doesn't fit in memory). To make it fast you need to find a way of indexing it, which is why I suggest a computed column.
– Laurence
Nov 5 '12 at 22:45
very slow solution, fast is required
– CR41G14
Nov 5 '12 at 21:36
very slow solution, fast is required
– CR41G14
Nov 5 '12 at 21:36
If you insist on doing
Select name from test where dbo.AlphasOnly(name) = 'JoeBloggs'
then this will be slow even if the function takes 0 time. You still pay the cost of reading all the rows off the disk (assuming when you say large table you mean your database doesn't fit in memory). To make it fast you need to find a way of indexing it, which is why I suggest a computed column.– Laurence
Nov 5 '12 at 22:45
If you insist on doing
Select name from test where dbo.AlphasOnly(name) = 'JoeBloggs'
then this will be slow even if the function takes 0 time. You still pay the cost of reading all the rows off the disk (assuming when you say large table you mean your database doesn't fit in memory). To make it fast you need to find a way of indexing it, which is why I suggest a computed column.– Laurence
Nov 5 '12 at 22:45
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13240298%2fremove-numbers-from-string-sql-server%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You say you can't change the data. Can you add a computed column to the existing table? Or add a new table with a foreign key and your computed value?
– Laurence
Nov 5 '12 at 21:04
@Laurence - Yea I suppose I can do this but can we do this in a temporary table in a stored procedure? If not then I can request this computed column, all i need is the function to do the replace. Thanks for your prompt response
– CR41G14
Nov 5 '12 at 21:07
I'd not recommend using regexps if performance matters. The way you use is obvious but still a good one!
– vyakhir
Nov 5 '12 at 21:13
@DmitryVyakhirev - There could be some rouge characters like ' " @ /, it is a messy database so will I need to do multiple REPLACES to get rid of every instance of a bad character
– CR41G14
Nov 5 '12 at 21:19