• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

distinct with date format from a varchar field

hi you experts
how can i achive sort from date in a distinct statement

SELECT distinct(id),estatus,fechagen,proyecto,reqnum,rev,tipoorden,tipmater,proveedor FROM requis ORDER BY  Convert(Datetime, fechagen, 103) asc

  • 2
3 Solutions
Why you need to convert the datetime column?

My this statement works well:

select distinct(RefNo), transactdate, totalamount from SalesTransactionTable order by transactdate
ErnestoAuthor Commented:
i need to make run the query i posted
what data type is the field "fechagen"?

regarding: "i need to make run the query i posted"

SELECT distinct(id)

"distinct" is not a function - so it does not accept a parameter

"id" is usually a unique identity for each row
if "id" is unique, then "distinct" is utterly redundant

Some Simple SQL Rules to Live By and look for "DISTINCT is *usually* bad"
A good rule of thumb -- if you need a distinct list of values in a single column or two, DISTINCT is the way to go.  But if your results "don't look right" or you see some duplicate rows and can't figure out why, do NOT just add DISTINCT to your SELECT to "fix it" !!  Step back, look at your joins, and re-write your query properly.  Even worse, I've seen people simply add DISTINCT to all their SELECT's right from the start, to preemptively "avoid duplicates".  Not good.  (Side note: If you are a DISTINCT abuser, try adding meaningful primary keys to your tables).
The effects of DISTINCT in a SQL query
Yes, I think in real life, Distinct is really seldom used. But if you really know that the statement result is what you want to achieve, try this:

SELECT distinct(id),Convert(Datetime, fechagen, 103) , estatus,fechagen,proyecto,reqnum,rev,tipoorden,tipmater,proveedor FROM requis ORDER BY  Convert(Datetime, fechagen, 103)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now