Solved

Problem in selecting max of a varchar datatype value

Posted on 2010-11-17
10
314 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
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 142

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:HainKurt
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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 142

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:
HainKurt 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:HainKurt
HainKurt 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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

12 Experts available now in Live!

Get 1:1 Help Now