Friday, February 17, 2012

Data type conversion

I have one column which contains numbers [but the column is in char format]
What will be the proper SQL query to sort that column ? If i use order by
clause, the records are sorted in characted method, i.e 40,50 not 40,41Arsalan,
Try:
ORDER BY LTRIM(RTRIM(column))
HTH
Jerry
"Arsalan" <arsalan_aslam@.hotmail.com> wrote in message
news:%23o3VP5czFHA.3836@.TK2MSFTNGP10.phx.gbl...
>I have one column which contains numbers [but the column is in char format]
> What will be the proper SQL query to sort that column ? If i use order by
> clause, the records are sorted in characted method, i.e 40,50 not 40,41
>|||Doesnt work.
LTRIM and RTRIM will remove the space, but doesnt sort properly.
MY records are like this
40
41
42
.
..
..
50
when i use order by clause, its sorts like this --> 40,50 [bec if character
field]. How do I sort the numeric part of the character field properly?
"Jerry Spivey" <jspivey@.vestas-awt.com> wrote in message
news:%23G6I3%23czFHA.3832@.TK2MSFTNGP10.phx.gbl...
> Arsalan,
> Try:
>
> ORDER BY LTRIM(RTRIM(column))
> HTH
> Jerry
> "Arsalan" <arsalan_aslam@.hotmail.com> wrote in message
> news:%23o3VP5czFHA.3836@.TK2MSFTNGP10.phx.gbl...
>|||create table #t(s char(4))
insert into #t(s) values('5')
insert into #t(s) values('50')
insert into #t(s) values('40')
insert into #t(s) values('41')
select s
from #t
order by s
select cast(s as int) p
from #t
order by p
drop table #t
Payson
Arsalan wrote:
> Doesnt work.
> LTRIM and RTRIM will remove the space, but doesnt sort properly.
> MY records are like this
> 40
> 41
> 42
> .
> ..
> ..
> 50
> when i use order by clause, its sorts like this --> 40,50 [bec if characte
r
> field]. How do I sort the numeric part of the character field properly?
>
> "Jerry Spivey" <jspivey@.vestas-awt.com> wrote in message
> news:%23G6I3%23czFHA.3832@.TK2MSFTNGP10.phx.gbl...|||order by cast(ltrim(rtrim(column as int)))
"Arsalan" <arsalan_aslam@.hotmail.com> wrote in message
news:OKxzKOdzFHA.3924@.TK2MSFTNGP14.phx.gbl...
> Doesnt work.
> LTRIM and RTRIM will remove the space, but doesnt sort properly.
> MY records are like this
> 40
> 41
> 42
> .
> ..
> ..
> 50
> when i use order by clause, its sorts like this --> 40,50 [bec if
> character field]. How do I sort the numeric part of the character field
> properly?
>
> "Jerry Spivey" <jspivey@.vestas-awt.com> wrote in message
> news:%23G6I3%23czFHA.3832@.TK2MSFTNGP10.phx.gbl...
>|||Arsalan,
Worked on my server. Try the examples listed in the following posts with
CAST --> INT.
HTH
Jerry
"Arsalan" <arsalan_aslam@.hotmail.com> wrote in message
news:OKxzKOdzFHA.3924@.TK2MSFTNGP14.phx.gbl...
> Doesnt work.
> LTRIM and RTRIM will remove the space, but doesnt sort properly.
> MY records are like this
> 40
> 41
> 42
> .
> ..
> ..
> 50
> when i use order by clause, its sorts like this --> 40,50 [bec if
> character field]. How do I sort the numeric part of the character field
> properly?
>
> "Jerry Spivey" <jspivey@.vestas-awt.com> wrote in message
> news:%23G6I3%23czFHA.3832@.TK2MSFTNGP10.phx.gbl...
>|||Thanks
"Jay" <msnews.microsoft.com> wrote in message
news:unWe6ddzFHA.3152@.TK2MSFTNGP10.phx.gbl...
> order by cast(ltrim(rtrim(column as int)))
>
> "Arsalan" <arsalan_aslam@.hotmail.com> wrote in message
> news:OKxzKOdzFHA.3924@.TK2MSFTNGP14.phx.gbl...
>

No comments:

Post a Comment