Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

achive a distinct with convert involved

Posted on 2013-01-04
14
Medium Priority
?
262 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:Ernesto
  • 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:Ernesto
ID: 38744954
has it

tsm
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38744976
and...
0
Industry Leaders: 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:Ernesto
ID: 38744987
any way to achive it?
0
 

Author Comment

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

Expert Comment

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

Author Comment

by:Ernesto
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:Ernesto
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 2000 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:Ernesto
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:Ernesto
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

824 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