Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to Find MAX value from the text field

Author  Topic 

sadi
Starting Member

1 Post

Posted - 2010-04-13 : 00:45:03


Hi-

I have a CYC_CD field which is VARCHAR. The values of this field indicate the cycle run code For Example 0109M(January 09 run), 1209M (December 09 run) so on so forth. When I convert it to an integer and use MAX function it gives me incorrect result becauase technically 0310M is the Max run and not 0409M .

Is there a way where I can pick up the MAX value in the text field. In my case pick up 0310M record and not 0409M record ?

Thanks SM

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-13 : 00:47:28
You really need to fix your table design so that you can use aggregate functions correctly. You are going to need to parse it into a date/time field and then do MAX. The performance of the query is going to suffer though. I hope this table will be small for this reason.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2010-04-13 : 01:23:19
Here your Query but its lack in performance when the table is large

select max(cast(cast(year(getdate())as varchar(4))+left(CYC_CD,len(CYC_CD)-1) as datetime)) from table_name

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page
   

- Advertisement -