Solved

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

Posted on 2015-01-31
2
263 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 33

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

863 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now