Showing posts with label returns. Show all posts
Showing posts with label returns. Show all posts

Friday, March 9, 2012

Script Component Task: function that returns last word in a string

I have a field called CustomerName Varchar 100 and I wish to write a function that can do the following in a script component task

create a function called CleanString (ByVal CustomerName as String) As String

CleanString Returns the last word of a Customer name if the CustomerName field contains more than one word or if the CustomerName field does not contain Corp or Ltd

ie parse 'Mr John Tools' and the function returns 'Tools'

ie parse 'TechnicalBooks' and the function returns 'TechnicalBooks'

ie parse 'Microsoft Corp' return 'Microsoft Corp'

ie parse 'Digidesign Ltd' return 'Digidesign Ltd'

Any idea of a regular expression or existing piece of existing code I can have

thanks in advance

dave

I believe you'd want something along the lines of:

\s*(\w+(\s+(Corp|Ltd))?)$

Public Function CleanString(ByVal CustomerName As String) As String

Dim re As New Regex("\s*(\w+(\s+(Corp|Ltd))?)$")

Dim m As Match

m = re.Match(CustomerName)

' insert sanity to make sure we had a match

' group 0 would be the full string

' group 1 is the one we want

Dim g As Group = m.Groups(1)

Dim cc As CaptureCollection = g.Captures

CleanString = cc(0).Value

End Function

|||

Matt,

Thanks the code almost worked--I had to rewrite it like this. However I need help to tweak the regular expression Regex("\s*(\w+(\s+(Corp|Ltd))?)$") Im getting the following results In/Out

Input: Digidesign Australasia Pty Ltd Output: Pty Ltd --wrong should be Ltd

Input: Ms S Aiten Output: Aiten --correct

Function CleanString(ByVal CustomerName As String) As String

m = re.Match(CustomerName)

' group 0 would be the full string

' group 1 is the one we want

CleanString = m.Groups(1).Value

End Function

'Called by

Public Class ScriptMain

Inherits UserComponent

Dim re As Regex = New Regex("\s*(\w+(\s+(Corp|Ltd))?)$")

Dim m As Match

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

If Len(Row.CUSTOMERNAME) > 0 Then

Output0Buffer.oCUSTOMERNAME = Row.CUSTOMERNAME.ToString

Output0Buffer.oSearchName = CleanString(Row.CUSTOMERNAME.ToString)

End If

|||I don't think you can easily do this with a single regex. You might want to pre-process the string to remove the Pty first, or have a special case that captures the words you want to the left and right of "Pty" whenever the string appears in your customer name.

Tuesday, February 21, 2012

scope_identity through ODBC

I have a problem retrieving the scope_identity through an ODBC connection (Excel for Example). It returns null.
Any idea?
Thanks
Being that it would need to be an insert statement, are you
sure the statement is successfully executing? You could
monitor this with Profiler.
-Sue
On Thu, 8 Apr 2004 04:46:05 -0700, George Keradinides
<anonymous@.discussions.microsoft.com> wrote:

>I have a problem retrieving the scope_identity through an ODBC connection (Excel for Example). It returns null.
>Any idea?
>Thanks
>

SCOPE_IDENTITY returns decimal from SQL command

Hi,

I have an sqlCommand return the ID from the last inserted row. I use SELECT SCOPE_IDENTITY within the SQL.

When I call the function it returns a Decimal instead of an integer??

So I want to convert this to an integer, however this is not liked by the compiler since the overloaded method for the decimal conversion is obviously a decimal, but the ExecuteScalar parameter is of type object.

iReturnVal = decimal.ToInt32(_sqlCommand.ExecuteScalar()); <-- compiler complains...

So I changed to this:

iReturnValue = decimal.ToInt32((decimal)_sqlCommand.ExecuteScalar());

Can someone tell me if this is the right way to do it? Seems messy code to me...

swaino:

iReturnVal = decimal.ToInt32(_sqlCommand.ExecuteScalar()); <-- compiler complains...

Decimal.toint32? I thought it was convert.toint32?

|||

It should already be an integer.

|||

Welldecimal. provides loads of conversion functions.

Also ExecuteScalardoes return an object not an integer..

SCOPE_IDENTITY and SqlDataSource

Hi folks;
I'm having trouble retrieving the SCOPE_IDENTITY() with an SqlDataSource, it returns a number that has nothing to do with the real identity.
It was always returning 102 or 137 when the real identities were something in between 5 to 10

Here is my code:

 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString%>" DeleteCommand="DELETE FROM [POD] WHERE [PODID] = @.PODID" InsertCommand="INSERT INTO [POD] ([CustomerID], [Airbill], [Shipper], [Consignee], [POD], [Date], [Time], [Pieces], [Weight], [Comments]) VALUES (@.CustomerID, @.Airbill, @.Shipper, @.Consignee, @.POD, @.Date, @.Time, @.Pieces, @.Weight, @.Comments); SELECT SCOPE_IDENTITY() AS @.newID" SelectCommand="SELECT * FROM [POD] ORDER BY [POD]" UpdateCommand="UPDATE [POD] SET [CustomerID] = @.CustomerID, [Airbill] = @.Airbill, [Shipper] = @.Shipper, [Consignee] = @.Consignee, [POD] = @.POD, [Date] = @.Date, [Time] = @.Time, [Pieces] = @.Pieces, [Weight] = @.Weight, [Comments] = @.Comments WHERE [PODID] = @.PODID"> <DeleteParameters> <asp:Parameter Name="PODID" Type="Int32" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="CustomerID" Type="Int32" /> <asp:Parameter Name="Airbill" Type="String" /> <asp:Parameter Name="Shipper" Type="String" /> <asp:Parameter Name="Consignee" Type="String" /> <asp:Parameter Name="POD" Type="String" /> <asp:Parameter Name="Date" Type="DateTime" /> <asp:Parameter Name="Time" Type="DateTime" /> <asp:Parameter Name="Pieces" Type="Int32" /> <asp:Parameter Name="Weight" Type="Decimal" /> <asp:Parameter Name="Comments" Type="String" /> <asp:Parameter Name="PODID" Type="Int32" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="newID" Type="Int32" Direction="ReturnValue" /> <asp:Parameter Name="CustomerID" Type="Int32" /> <asp:Parameter Name="Airbill" Type="String" DefaultValue=" " /> <asp:Parameter Name="Shipper" Type="String" DefaultValue=" " /> <asp:Parameter Name="Consignee" Type="String" DefaultValue=" " /> <asp:Parameter Name="POD" Type="String" /> <asp:Parameter Name="Date" Type="DateTime" /> <asp:Parameter Name="Time" Type="DateTime" /> <asp:Parameter Name="Pieces" Type="Int32" DefaultValue="0" /> <asp:Parameter Name="Weight" Type="Decimal" DefaultValue="0" /> <asp:Parameter Name="Comments" Type="String" DefaultValue=" " /> </InsertParameters> </asp:SqlDataSource>
 
Protected Sub SqlDataSource1_Inserted(ByVal senderAs Object,ByVal eAs System.Web.UI.WebControls.SqlDataSourceStatusEventArgs)Handles SqlDataSource1.Inserted' ** get the id and redirect to details pageDim idAs Integer = e.Command.Parameters("@.newID").Value Response.Redirect("pod_details.aspx?id=" & ID)End Sub
 
Does anybody know what am i doing wrong in here?

Change the direction of your "newID" Parameter from "ReturnValue" to "Output".

|||

Hi;
Thanks for your help.
I also had to change fromSELECT SCOPE_IDENTITY() AS @.newID to SET @.newID =SCOPE_IDENTITY(), or else it returns a Null value.
Yes