Click here to Skip to main content
15,922,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hii ,,
this is one of my column
Convert(varchar(10),Paln_Date,103) + ' ' + Plan_Hour PlanTime

PlanTime
20/07/2013 8:00 AM
20/08/2013 8:00 AM
22/08/2013 8:00 AM
23/07/2013 8:00 AM
26/07/2013 8:00 AM
27/07/2013 8:00 AM
27/08/2013 8:00 AM
29/08/2013 8:00 AM
30/07/2013 8:00 AM

this is how i am getting result when i say order by plantime

but i want sort properly month wise ,year n date wise

expected result is something like this


06/12/2008
03/01/2009
01/02/2009
07/02/2009

plese suggest
Posted

Use CAST and CONVERT[^] functions ;)

SQL
SELECT CONVERT(VARCHAR(10), DateTimeField, 121) AS DateOnly
FROM TableName


SQL
SELECT CONVERT(VARCHAR(10), GETDATE(), 121) AS DateOnly
 
Share this answer
 
v2
Hi,


if you dont want to show time then use

Convert(varchar,Paln_Date,103) PlanTime
or use it only for sorting as
order by convert(varchar,PlanTime,103)



SQL
select * from TableName
order by  convert(varchar,PlanTime,103)


for more details on date format visit

SQL Server Functions that helps to convert date and time values to and from string literals and other date and time formats.[^]
 
Share this answer
 
v3
SQL
create proc GetApplications
@SortField nvarchar(20)
as
select * from Applications
order by
case @SortField
when 'FirstName' then cast (FirstName as sql_variant)
when 'LastName' then cast (LastName as sql_variant)
when 'ApplyDate' then cast (ApplyDate as sql_variant)
else cast (ID as sql_variant)
end
 
Share this answer
 
Comments
Maciej Los 31-Jul-13 4:45am    
Are you shure that is correct answer? OP wants to convert datetime data type field into date only (without time part).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900