Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Convert Simple VBA to T SQL Function


Convert Simple VBA to T SQL Function

Author
Message
mbrady5
mbrady5
SSC-Enthusiastic
SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)

Group: General Forum Members
Points: 131 Visits: 759
Hello, I am an idiot when it comes to VBA. With that being said, I could use some help. There is some VBA I'd like to convert to a T SQL function and I am certain there are many people that could do such a thing quickly.

Thanks for any assistance.

Public Function GetType(strNumber As String) As String
Dim intPos

For intPos = 1 To Len(strNumber)
If IsNumeric(Mid(strNumber, intPos, 1)) Then
Exit For
End If
Next

GetType = Trim(Left(strNumber, intPos - 1))
End Function


Sean Lange
Sean Lange
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: 16680 Visits: 17038
mbrady5 (4/18/2014)
Hello, I am an idiot when it comes to VBA. With that being said, I could use some help. There is some VBA I'd like to convert to a T SQL function and I am certain there are many people that could do such a thing quickly.

Thanks for any assistance.

Public Function GetType(strNumber As String) As String
Dim intPos

For intPos = 1 To Len(strNumber)
If IsNumeric(Mid(strNumber, intPos, 1)) Then
Exit For
End If
Next

GetType = Trim(Left(strNumber, intPos - 1))
End Function



Does this just return the first x characters before it encounters a number?

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
mbrady5
mbrady5
SSC-Enthusiastic
SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)

Group: General Forum Members
Points: 131 Visits: 759
That is exactly what I am hoping to do.

Thank you for the quick response
Sean Lange
Sean Lange
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: 16680 Visits: 17038
You could do this a number of ways. You had a function from VBA. The direct translation there would be a scalar function. Those do not perform very well in t-sql.

If you need a scalar function you could do it along these lines.


declare @MyString varchar(20) = 'abc3def'
select left(@MyString, case when patindex('%[0-9]%', @MyString) = 0 then LEN(@MyString) else patindex('%[0-9]%', @MyString) - 1 end)



However, since in t-sql we rarely deal with one row at a time it would be better to do this as a set.


with MyValues as
(
select 'abc123def' as SomeValue union all
select '123abc' union all
select 'abcdef' union all
select 'ab12'
)

select left(SomeValue, case when patindex('%[0-9]%', SomeValue) = 0 then LEN(SomeValue) else patindex('%[0-9]%', SomeValue) - 1 end)
from MyValues



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
mbrady5
mbrady5
SSC-Enthusiastic
SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)

Group: General Forum Members
Points: 131 Visits: 759
Thank you so much. This did the trick
Sean Lange
Sean Lange
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: 16680 Visits: 17038
You're welcome. Glad that worked for you.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
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