SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help me with the regular expression


Help me with the regular expression

Author
Message
mw_sql_developer
mw_sql_developer
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16648 Visits: 1310
Good Day!
I am looking for a way to test a string to make sure it only has numbers and/or a decimal point ( Not more than one decimal point )
So what I am really looking for is a way to test for decimal number ( and integers )
Any idea ?


declare @SearchVal varchar(10) = '1234.00'

select 'yes'
where
@SearchVal like '%[0-9][.][0-9]%'

Alan Burstein
Alan Burstein
SSC Guru
SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)

Group: General Forum Members
Points: 53598 Visits: 8954
WHERE @SearchVal NOT LIKE '%[^0-9.]%' 
AND LEN(@SearchVal) - LEN(REPLACE(@SearchVal,'.','')) < 2


-- Alan Burstein


Helpful links:

Best practices for getting help on SQLServerCentral -- Jeff Moden
How to Post Performance Problems -- Gail Shaw

Nasty fast set-based string manipulation functions:
For splitting strings try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL Server 2012+)
To split strings based on patterns try PatternSplitCM
Need to clean or transform a string? try NGrams, PatExclude8K, PatReplace8K, DigitsOnlyEE, or Translate8K

I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. -- Itzik Ben-Gan 2001

drew.allen
drew.allen
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62134 Visits: 16586
declare @SearchVal varchar(10) = '1234.00'

select 'yes'
where @SearchVal NOT LIKE '%[^0-9.]%'
AND @SearchVal NOT LIKE '%.%.%'


Drew

J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
ZZartin
ZZartin
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24872 Visits: 16869
Another option is to use TRY_CONVERT() if you want to make sure it'll actually convert to something in SQL server

select 'yes'
where TRY_CONVERT(decimal(29, 9), @SearchVal) IS NOT NULL

Alan Burstein
Alan Burstein
SSC Guru
SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)

Group: General Forum Members
Points: 53598 Visits: 8954
ZZartin - Wednesday, February 28, 2018 12:33 PM
Another option is to use TRY_CONVERT() if you want to make sure it'll actually convert to something in SQL server

select 'yes'
where TRY_CONVERT(decimal(29, 9), @SearchVal) IS NOT NULL

Good solution and would probably be fine. Note this though:

SELECT TRY_CONVERT(decimal(29, 9), $)


-- Alan Burstein


Helpful links:

Best practices for getting help on SQLServerCentral -- Jeff Moden
How to Post Performance Problems -- Gail Shaw

Nasty fast set-based string manipulation functions:
For splitting strings try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL Server 2012+)
To split strings based on patterns try PatternSplitCM
Need to clean or transform a string? try NGrams, PatExclude8K, PatReplace8K, DigitsOnlyEE, or Translate8K

I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. -- Itzik Ben-Gan 2001

drew.allen
drew.allen
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62134 Visits: 16586
Alan.B - Wednesday, February 28, 2018 12:42 PM
ZZartin - Wednesday, February 28, 2018 12:33 PM
Another option is to use TRY_CONVERT() if you want to make sure it'll actually convert to something in SQL server

select 'yes'
where TRY_CONVERT(decimal(29, 9), @SearchVal) IS NOT NULL

Good solution and would probably be fine. Note this though:

SELECT TRY_CONVERT(decimal(29, 9), $)

The pattern that he is trying to match excludes strings that represent negative numbers, which the TRY_CONVERT will not. Of course, it's easy to modify the WHERE clause to only return positive (or non-negative numbers) or to modify the pattern to match negative numbers.

Drew

J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
Alan Burstein
Alan Burstein
SSC Guru
SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)

Group: General Forum Members
Points: 53598 Visits: 8954
drew.allen - Wednesday, February 28, 2018 12:52 PM
Alan.B - Wednesday, February 28, 2018 12:42 PM
ZZartin - Wednesday, February 28, 2018 12:33 PM
Another option is to use TRY_CONVERT() if you want to make sure it'll actually convert to something in SQL server

select 'yes'
where TRY_CONVERT(decimal(29, 9), @SearchVal) IS NOT NULL

Good solution and would probably be fine. Note this though:

SELECT TRY_CONVERT(decimal(29, 9), $)

The pattern that he is trying to match excludes strings that represent negative numbers, which the TRY_CONVERT will not. Of course, it's easy to modify the WHERE clause to only return positive (or non-negative numbers) or to modify the pattern to match negative numbers.

Drew

Agreed.

I think your solution is the best. My code:
LEN(@SearchVal) - LEN(REPLACE(@SearchVal,'.',''))

is the fastest way I know to count individual characters but it's not necessary here as we're only trying to ensure that there are not two dots which is more efficiently handled with your code:
@SearchVal NOT LIKE '%.%.%'


-- Alan Burstein


Helpful links:

Best practices for getting help on SQLServerCentral -- Jeff Moden
How to Post Performance Problems -- Gail Shaw

Nasty fast set-based string manipulation functions:
For splitting strings try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL Server 2012+)
To split strings based on patterns try PatternSplitCM
Need to clean or transform a string? try NGrams, PatExclude8K, PatReplace8K, DigitsOnlyEE, or Translate8K

I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. -- Itzik Ben-Gan 2001

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search