Solved

achive a distinct with convert involved

Posted on 2013-01-04
14
259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

751 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