views:

113

answers:

1

I have a stored procedure that will give the latest records i.e., order by added date this is my procedure....

  select distinct top 5 videos.videoid,videos.videotitle,videos.videoname,
   convert(varchar,videos.posteddate,106) as  posteddate,videos.approvedstatus,
   videos.videoimage,(ISNULL(videos.views,0.0)) as [views],videos.privacy,
    (isnull(videos.rating,0.0)) as rating,videos.userid,
  users.userid,users.username from videos  left outer  join users on
videos.userid=users.userid
   where videos.approvedstatus='Y' and videos.privacy='P'
 order by  posteddate desc

but it is not giving latest records

when i execute the query

select * from videos order by posteddate desc

it is giving exact records. but stored procedure is not giving exact records. can u help me, thank you.


+4  A: 

Use ORDER BY videos.posteddate

  select distinct top 5
     videos.videoid,
     videos.videotitle,
     videos.videoname,
     convert(varchar,videos.posteddate,106) as  posteddate,
     videos.approvedstatus,
     videos.videoimage,
     (ISNULL(videos.views,0.0)) as [views],
     videos.privacy,
     (isnull(videos.rating,0.0)) as rating,
     videos.userid,
     users.userid,users.username
  from
     videos
     left outer join
     users on videos.userid=users.userid
  where
     videos.approvedstatus='Y' and videos.privacy='P'
  order by
     videos.posteddate desc

Your original is the equivalant of ORDER BY convert(varchar,videos.posteddate,106) DESC

So you are sorting by "dd mon yyyy" string, not the desired actual datetime (yyyy-mm-dd hh etc)

I guess this is SQL Server 2000: from memory SQL Server 2005 will not accept this ambiguity

gbn
ok Mr.gbn thank you for response i will check it and inform you
Surya sasidhar
i am using sql 2005
Surya sasidhar
ya i got it Mr. gbn i simple remove .. convert(varchar,videos.posteddate,106) as posteddate, and i change like this videos.posteddate and now it is giving correct i mean latest videos
Surya sasidhar
@Surya sasidhar: please accept my answer then
gbn
+1, Mr. gbn fine work!
KM