How to get Date part only using a SQL query from SQL server

liunx

Guest
Hi,

I need to take records from a table group by Date type field.
As every record have different values in the field, it doesn't took the values properly.

Ex:

val1: 2005-01-12 16:47:08.780
val2: 2005-01-12 17:04:25.110

The above two records having same date but the time is only different.
I need a query to return only one record for above two records group by date.

Suggest me.

SivaYour post is a little hard to follow; here's the question I think I'm supposed to be answering. You have a bunch of records in a database that are date/time stamped. You'd like to query the database and return only the date field, and only unique dates. E.G, you have records such as:intIDintQtydtOrdered
126/13/2003 5:14:00 PM
216/15/2003 5:51:00 AM
316/16/2003 5:26:00 PM
426/16/2003 5:26:00 PM
536/17/2003 6:16:00 AM
626/18/2003 3:37:00 PM
726/18/2003 3:38:00 PM
846/19/2003 5:09:00 PM
916/21/2003 2:23:00 AM
1016/21/2003 2:23:00 AMand would like to return6/13/2003
6/15/2003
6/16/2003
6/16/2003
6/17/2003
6/18/2003
6/18/2003
6/19/2003
6/21/2003
6/21/2003If this is the case, it's more of a SQL problem than a .NET problem so the question probably ought to have been posted there.
Anyway, here's a solution:SELECT DISTINCT CONVERT(varchar, dtOrdered, 101) AS theDate
FROM myOrdersTableThe general syntax of this command is: CONVERT ( data_type [ ( length ) ] , expression [ , style ] ). The 101 style returns the date in US format. If you want a different format use this table:Style IDStyle Type
0 or 100 mon dd yyyy hh:miAM (or PM)
101 mm/dd/yy
102 yy.mm.dd
103 dd/mm/yy
104 dd.mm.yy
105 dd-mm-yy
106 dd mon yy
107 Mon dd, yy
108 hh:mm:ss
9 or 109 mon dd yyyy hh:mi:ss:mmmAM (or PM)
110 mm-dd-yy
111 yy/mm/dd
112 yymmdd
13 or 113 dd mon yyyy hh:mm:ss:mmm(24h)
114 hh:mi:ss:mmm(24h)
20 or 120 yyyy-mm-dd hh:mi:ss(24h)
21 or 121 yyyy-mm-dd hh:mi:ss.mmm(24h)
126 yyyy-mm-dd Thh:mm:ss.mmm(no spaces)
130 dd mon yyyy hh:mi:ss:mmmAM
131 dd/mm/yy hh:mi:ss:mmmAMIf that doesn't do the trick you can always force it to look how you want: CONVERT(varchar, MONTH(dtOrdered)) + '/' + CONVERT(varchar, DAY(dtOrdered)) + '/' + CONVERT(varchar, YEAR(dtOrdered)) AS theDate
 
Back
Top