Solved

Problem in selecting max of a varchar datatype value

Posted on 2010-11-17
10
318 Views
Last Modified: 2012-05-10
Hi,

I have a table in sqlserver 2008 with a auto generated column as varchar. these are the sample values
max1
max2
max3
.
.
.
.
max98
max99
max100

when i try to select the max value of the above column, it give only as max99 but it should give us max100.

please let me know an solution to find out max of a varchar datatype value

thanks,
Rajesh
0
Comment
Question by:rajeshmarappan
[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
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34155845
that column should really be splitted into 2 columns ...

apart from that:
select max( cast( substring(yourfield, 4, 100) as bigint) )
  from yourtable

Open in new window

0
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 34155916
or if they all start with 'max'

select max(cast(replace(col,'max','') as int)) from yourTable
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34155941
You need one more column to indicate the number.

For current data, if 'max' is hardcoded, you can try this query as well

select 'max' + cast( max( cast( right(yourcolumn, len(yourcolumn) - 3) as int)) as varchar(25)) from yourtable

Open in new window


Regards
Raj
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 34156257
Rajesh,

>>please let me know an solution to find out max of a varchar datatype value

Actually, in a text comparison, 'max99' > 'max100'.

Just as 'max2' > 'max100'

:)

Patrick
0
 

Author Comment

by:rajeshmarappan
ID: 34156314
Hi,

Above solutions are working fine. but we need the same solution for linq query where we cant use CAST. please let me know.

Regards,
Rajesh
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34156354
not sure if that can be done in LINQ ...

so basically, I come back to my initial post:

that column should really be splitted into 2 columns ...
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34156451
Right.
Things will be easy, if you have one more integer column so that you can find the maximum value

Raj
0
 
LVL 51

Accepted Solution

by:
Huseyin KAHRAMAN earned 250 total points
ID: 34156591
you can use one of these (not tested)

col.Remove(0,3) * 1

col.Replace('max','') * 1

col.Substring(3) * 1

so it removes first 3 letters 'max', then multiplies it with 1 makes it (or should make number) then sort it...

check here for string functions: http://msdn.microsoft.com/en-us/vbasic/bb737939.aspx#strsubst
0
 
LVL 51

Assisted Solution

by:Huseyin KAHRAMAN
Huseyin KAHRAMAN earned 250 total points
ID: 34156648
or do this

('0000000' + col.Replace('max','000')).Substring(10-col.Length)

the idea is convert

max1     : 0000000001
max23   : 0000000023
max100 : 0000000100

so sorting will be correct without casting ;)

0
 

Author Closing Comment

by:rajeshmarappan
ID: 34164725
Partially Solved my problem
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
partitioning database after decade growth 8 56
Need to find substring in SQL 5 41
Subquery in the where statement always shows up null in the result 5 42
sql query 5 44
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

756 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