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)
 order of nvarchar(50) field

Author  Topic 

gjja
Starting Member

17 Posts

Posted - 2008-06-12 : 10:01:36
In my SQL 2005 database table Records, I have 3 fields, field1, field2, and field3 which are all nvarchar(50) fields. The value of field2 is something like this, MDB006-MD002-0004-3-2007. I would like to order this field but only use the 0004-3-2007 part of the field to order it. Is it possible to put the last 11 charachters (0004-3-2007) in another field and then order it using this new field?

ayamas
Aged Yak Warrior

552 Posts

Posted - 2008-06-12 : 10:05:00
Are the field lengths from MDB006-MD002 going to be constant?
Go to Top of Page

gjja
Starting Member

17 Posts

Posted - 2008-06-12 : 10:06:28
The last 11 characters will always be constant
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-12 : 10:06:53
you dont need seperate field for that. just use
SELECT *,RIGHT(field2,11) AS OrderField FROm Table ORDER BY OrderField
Go to Top of Page

ayamas
Aged Yak Warrior

552 Posts

Posted - 2008-06-12 : 10:15:04
No vishakh I think she meant
SELECT *,RIGHT(field2,11) AS OrderField FROm Table ORDER BY RIGHT(field2,11)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-12 : 10:21:52
quote:
Originally posted by ayamas

No vishakh I think she meant
SELECT *,RIGHT(field2,11) AS OrderField FROm Table ORDER BY RIGHT(field2,11)


No need of repeating RIGHT. you can use alias itself in ORDER BY
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-12 : 10:33:23
Also note that the returned value is still in nvarchar so it may not be displayed correctly
Can you post some sample data with expected result?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

gjja
Starting Member

17 Posts

Posted - 2008-06-12 : 13:28:09
Original Field | OrderField
MDB006-MD104-0125-6-2008|0125-6-2008
MDB006-MD104-0124-6-2008| 0124-6-2008
MDB006-MD014-0123-6-2008| 0123-6-2008
MDB006-MD014-0122-6-2008| 0122-6-2008
MDB006-MD104-0121-5-2008| 0121-5-2008
MDB006-MD104-0120-4-2008| 0120-4-2008
MDB006-MD104-0119-4-2008| 0119-4-2008
MDB006-MD104-0118-3-2008| 0118-3-2008
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2008-06-12 : 13:52:46
[code]
I Guess this should work in that case.

Select * From yourTable Order by Right(Column_Name,11)
[/code]

Chirag

http://www.chirikworld.com
Go to Top of Page
   

- Advertisement -