Solved

achive a distinct with convert involved

Posted on 2013-01-04
14
257 Views
Last Modified: 2013-01-08
hi all
how can achive this query?


select distinct fechap from cheque  order by Convert(Datetime, fechap, 103)desc

is a varchar that i need to convert in date to sort

tsm
0
Comment
Question by:edo60
  • 7
  • 4
  • 3
14 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38744946
And is it not working? You need a space between ")" and "desc"
0
 

Author Comment

by:edo60
ID: 38744954
has it

tsm
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38744976
and...
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

by:edo60
ID: 38744987
any way to achive it?
0
 

Author Comment

by:edo60
ID: 38745004
do not work
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38745006
You get any error?
0
 

Author Comment

by:edo60
ID: 38745007
Cuando se especifica SELECT DISTINCT, los elementos de ORDER BY deben aparecer en la lista de selección.

when an select distinct is specified, the elements of the order by must appear in the list
0
 

Author Comment

by:edo60
ID: 38745455
any advice?
tsm
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38745785
Try

select distinct Convert(Datetime, fechap, 103) from cheque  order by Convert(Datetime, fechap, 103) desc
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 38747717
edo60,

While most of us here have a lot of knowledge with SQL Server, none of us (to my knowledge) can read a crystal ball, so it is in your best interest to explain fully what you need as we are not sitting in front of your computer.  Quite frankly, stating "do not work" is no use whatsoever.  Just imagine taking your car to the mechanic telling them "do not work" and I suspect you should understand.

For clarity here is the same in Spanish:
Mientras hay muchos aquí que son muy capaces usando SQL Server, nadie (de que yo sepa) somos magos, por lo tanto por tu bien te conviene explicar completamente tus requerimientos ya que no estamos enfrente de tu computadora (ordenador).  Francamente diciendo "no funciona" no ayuda en lo más mínimo.  Imagina que lleves tu auto al mecánico y le dices" "no funciona" y creo que comprenderás.
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 38747725
So let's start from the beginning:  

What is the data type for "fechap"?  If it is varchar (or nvarchar) and has the format ddmmyyyy then I would suggest a minor change to the solution provided (no points please):
SELECT	DISTINCT
        CONVERT(Datetime, fechap, 103) fechap2
FROM    cheque
ORDER BY 
	fechap2 DESC

Open in new window

Or:
SELECT	DISTINCT
        CONVERT(Datetime, c.fechap, 103) fechap
FROM    cheque c
ORDER BY 
	fechap DESC

Open in new window

Or:
SELECT	DISTINCT
        CONVERT(Datetime, fechap, 103) fechap2
FROM    cheque
ORDER BY 
	1 DESC

Open in new window

0
 

Author Comment

by:edo60
ID: 38751260
Ok, im understand what you say, sorry for the language barrear, but you give me greate solution with

SELECT      DISTINCT
        CONVERT(Datetime, c.fechap, 103) fechap
FROM    cheque c
ORDER BY
      fechap DESC

it give me the result that i want, only i detail, is any way to put off the time?
the result is:

2013-01-04 00:00:00.000
2013-01-03 00:00:00.000
2013-01-02 00:00:00.000
2012-12-21 00:00:00.000
2012-12-19 00:00:00.000
2012-12-18 00:00:00.000
2012-12-13 00:00:00.000
2012-12-11 00:00:00.000

is the order that i need but is posible to put off 00:00:00.000?
tsm
regards
0
 

Author Closing Comment

by:edo60
ID: 38751660
tsm
regards
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 38757260
If you have SQL Server 2008 you can do this:
CONVERT(Date, c.fechap, 103) fechap

If not you can do this (just understand that it is now character data):
CONVERT(char(10), CONVERT(Datetime, c.fechap, 103), 120) fechap
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question