Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Extract Date from DateTime field as Date Time

Author  Topic 

hummy
Starting Member

32 Posts

Posted - 2008-06-30 : 15:39:14
Hi,

My website runs the following command against the sql server database.

select
convert(varchar,agent.[CUSTOM TABLE 1 Forms received on],103) [Forms Received On],
from
VRP_CUSTOM_TABLE_1 agent

The problem i have is that the results are displayed in a grid which can be sorted by simply clicking the grid. Now it seems becuase the results are retunred as varchar the dates are not sorted properly in the website, as you have some 2007 records before 2008and others after 2008. The date needs to be returned as a date in the format DD/MM/YYYY. The forms recieved on column is a DATETIME column.

Can anyone recommend a way of showing these results as a date time field but in the format DD/MM/YYYY.

I was looking on another site and found the following query but it doesn't seem to work for me.

cast(convert(varchar(100), mydatecol, 101) as datetime) AS mydatecol

Apparently, telling SQL Server to convert the datetime to a varchar and then back to a datetime does the trick.

Thanks in advance.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-30 : 15:42:45
Why don't you just return your column as is and then chop the time portion off at the application level?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

hummy
Starting Member

32 Posts

Posted - 2008-06-30 : 15:58:55
Yes...i would love to do that...except i don't know how to.
My developer has unfortunately left me in the lurch somewhat with unfinished work. I can see in the code the bit that has the sql statement so i was hoping i could just edit that.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-06-30 : 16:13:28
where is this "grid" that is displaying the data? Is this ASP.NET, ASP, VB, a report, or something else? That is where you should apply your formatting, not by converting all of your data to VARCHAR. Just about all programming languages and reporting tools have quick and easy "right-click to format" options where you can usually enter a format string like YYYY-MM-DD.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

hummy
Starting Member

32 Posts

Posted - 2008-06-30 : 16:24:30
Hi.
It's in ASP.NET and i can see the code from within Visual Studio 2005 as i'm no programmer and not used VS2005 before.
However how i find the properties of this Grid i do not know....
Perhaps you can point me the right direction.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-06-30 : 16:34:35
Sure thing!

See if this helps:

http://peterkellner.net/2006/05/24/how-to-set-a-date-format-in-gridview-using-aspnet-20using-htmlencode-property/

If not, show me the code for your ASP.NET page and I will set what I can do for you. But it is very dangerous in general for a non-programmer to play with application code, so be very careful! (note: I am available for consulting!)


- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

hummy
Starting Member

32 Posts

Posted - 2008-06-30 : 17:08:20
fantastic...thank you very much.
Go to Top of Page
   

- Advertisement -