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 2008 Forums
 Transact-SQL (2008)
 ordering problem with numeric stored as nvarchar?

Author  Topic 

akpaga
Constraint Violating Yak Guru

331 Posts

Posted - 2014-03-11 : 13:16:54
Hi Friends,

I have a table with customerID field stored as nvarchar

So I was using the following query for my purpose which worked well

select distinct CAST(customerid as int) as custid
from Customer_Data
where Customer_Desktop is not null
order by CAST(customerid as int)

But now a new customer id is being stored as : 0333 and with my query the is leading 0 is being truncated...How can i now show the customerid with the leading 0 and same time order the query.

Thank You

sqlsaga
Yak Posting Veteran

93 Posts

Posted - 2014-03-11 : 13:31:42
Don't Convert it into INT.. that will solve the issue... If you have Distinct it will automatically use Order by so there is no need to an Order by exclusively again...

Visit www.sqlsaga.com for more t-sql snippets and BI related how to's.
Go to Top of Page

akpaga
Constraint Violating Yak Guru

331 Posts

Posted - 2014-03-11 : 13:40:43
Thank You SqlSaga for the response but that trick did not work in ordering.
Go to Top of Page

sqlsaga
Yak Posting Veteran

93 Posts

Posted - 2014-03-11 : 14:10:41
can you post some sample data and then let us know what the issue is?

Visit www.sqlsaga.com for more t-sql snippets and BI related how to's.
Go to Top of Page

akpaga
Constraint Violating Yak Guru

331 Posts

Posted - 2014-03-11 : 14:48:27
quote:
Originally posted by sqlsaga

can you post some sample data and then let us know what the issue is?

Visit www.sqlsaga.com for more t-sql snippets and BI related how to's.



This is how i resolved..The time crunch did not make me think properly..Glad i resolved on my own.

select CustomerID from (select distinct CustomerID
from Customerdata
where CustomerID is not null) t
order by cast( CustomerID as int)
Go to Top of Page

sqlsaga
Yak Posting Veteran

93 Posts

Posted - 2014-03-11 : 15:42:06
Why do you want to use a subquery... try like this directly in that case...

select distinct CustomerID
from Customerdata
where CustomerID is not null
order by cast( CustomerID as int)


Visit www.sqlsaga.com for more t-sql snippets and BI related how to's.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2014-03-17 : 08:12:41
or

select distinct CustomerID
from Customerdata
where CustomerID is not null
order by CustomerID+0


Madhivanan

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

- Advertisement -