Solved

achive a distinct with convert involved

Posted on 2013-01-04
14
254 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
 

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
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.

 

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

929 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now