Tuesday, March 12, 2013

VB.NET - Count String Occurrence Within String

Code to look for a count of the number of times a string appears within another string:



Dim intOccurence As Integer


 intOccurence = Regex.Matches(strStringToSearchWithin, Regex.Escape(strStringToSearchFor)).Count

Monday, October 22, 2012

SQL - Return Left of String from Specified Char


DECLARE @somestring as nvarchar(50) = 'test,value';


select left(@somestring,CHARINDEX(',',@somestring)-1)

SQL - Return Right of String from Specified Char


DECLARE @somestring as nvarchar(50) = 'test,value';


select right(@somestring,LEN(@somestring)-CHARINDEX(',',@somestring))

SQL - Check if Char Exists




DECLARE @somestring as nvarchar(50) = 'test,value';


select
[test] = CASE
  WHEN CHARINDEX(',', @somestring) > 0
    THEN 'exists'
    ELSE 'does not exist'
  END

Friday, October 12, 2012

VB.NET - Loop On XML Data

Sample XML Data:

<ChapterContent xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <Chapters>
                                <ChapterEntry><Number>1</Number><Time>70059</Time><Title>Joe</Title></ChapterEntry>
                                <ChapterEntry><Number>2</Number><Time>204172</Time><Title>Jim</Title></ChapterEntry>
                                <ChapterEntry><Number>3</Number><Time>220381</Time><Title>Harrison</Title></ChapterEntry>
                                <ChapterEntry><Number>4</Number><Time>348865</Time><Title>Billy</Title></ChapterEntry>
                                <ChapterEntry><Number>5</Number><Time>417046</Time><Title>Tom</Title></ChapterEntry>
                                <ChapterEntry><Number>6</Number><Time>458876</Time><Title>Chester</Title></ChapterEntry>
                                <ChapterEntry><Number>7</Number><Time>503876</Time><Title>Mike</Title></ChapterEntry>
                                <ChapterEntry><Number>8</Number><Time>562816</Time><Title>Donny</Title></ChapterEntry>
                                <ChapterEntry><Number>9</Number><Time>600409</Time><Title>Steve</Title></ChapterEntry>
                                <ChapterEntry><Number>10</Number><Time>649318</Time><Title>Jimbo</Title></ChapterEntry>
                                <ChapterEntry><Number>11</Number><Time>696459</Time><Title>Kane</Title></ChapterEntry>
                                <ChapterEntry><Number>12</Number><Time>775005</Time><Title>Sammy</Title></ChapterEntry>
                                <ChapterEntry><Number>13</Number><Time>843422</Time><Title>Eric</Title></ChapterEntry>
                                <ChapterEntry><Number>14</Number><Time>899541</Time><Title>Dre</Title></ChapterEntry>
                </Chapters>
</ChapterContent>



Save the XML file to a location and parse through it with VB code:

            Using reader As XmlReader = XmlReader.Create("C:\TEMP\test.xml")
                While reader.Read()
                    If reader.IsStartElement() Then
                        If reader.Name = "Time" Then
                            strTime = reader.ReadElementString("Time")
                            strBillNumber = reader.ReadElementString("Title")

                            'this will be code to insert into table
                            sbTemp.Append("Time = " & strTime & "   Name = " & strBillNumber & vbCrLf)
                        End If
                    End If
                End While
            End Using

Wednesday, August 29, 2012

ASP.NET - Display pdf From BLOB


Imports System.IO


Private Sub DisplayPdfFromBLOB(ByVal adt As DataTable)

        Dim blob() As Byte


        Try

            blob = CType(adt(0)("form_document"), Byte())
            Dim ms As New MemoryStream(blob)

            Response.ContentType = "application/pdf"
           ms.WriteTo(Response.OutputStream)
            Response.Flush()

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

Friday, August 3, 2012

SQL - Get Left Part of String From 2nd Space

If you have some text value and you need to get the left part of the string from the 2nd space, here is a technique I figured out that works.

declare @some_text as nvarchar(20) = 'through 123 some text';

select ltrim(rtrim(left(@some_text, charindex(' ', @some_text, charindex(' ', @some_text)+1))))


You could apply the same logic to go from the nth space in the occurrence, it would become more complex.