Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2015-01-31
2
Medium Priority
?
290 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:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 34

Accepted Solution

by:
Mike Eghtebas earned 2000 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:Murray Brown
ID: 40581437
Thanks very much
0

Featured Post

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

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

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…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

705 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