Solved

VB.net SQL Find first and last text items in a column

Posted on 2015-01-31
2
274 Views
Last Modified: 2015-01-31
Hi

I use the following VB.net code to find the maximum value in numeric and date columns
How do I find the maximum or minimum of an text column where for instance "Albert" would
be the minimum and "Zach" would be the maximum

 Function oMaximum_in_Column(ByVal oTableName As String, ByVal oColumnName As String) As String
        Try

            Dim oSQL As String

            oSQL = "Select Max([" & oColumnName & "]) As MyMax From [" & oTableName & "]"

            Dim cn As New SqlConnection(Globals.ThisAddIn.oRIGHT.lblConnectionString.Text)
            Dim cmd As New SqlCommand(oSQL, cn)
            cmd.CommandTimeout = My.Settings.Timeout
            cn.Open()

            oMaximum_in_Column = cmd.ExecuteScalar.ToString

        Catch ex As Exception
            oMaximum_in_Column = "N/A"
        End Try
    End Function
0
Comment
Question by:murbro
2 Comments
 
LVL 34

Accepted Solution

by:
Mike Eghtebas earned 500 total points
ID: 40581376
oSQL = "Select Max(t.[" & oColumnName & "]) As MyMax, Min(t.[" & oColumnName & "]) As MyMin From [" & oTableName & "] As t"

Dim rdr As SqlDataReader = oSQL.ExecuteReader()

If (rdr.HasRows) Then
   oMaximum_in_Column = rdr("MyMax").ToString
   oMinimum_in_Column = rdr("MyMin").ToString
Else
   oMaximum_in_Column = "no records exist"
   oMinimum_in_Column = "no records exist"
End

revised...
0
 

Author Closing Comment

by:murbro
ID: 40581437
Thanks very much
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question