SQL converting datetime to shortdate

by on apr.20, 2009, under Developing

I’ve a table where I need to select the posts with the latest date. Problem is that when you get the latest date it also gets it by the hour, meaning that it will try to fetch everything older or equal to the date + time, which is nothing. There for, I need to make it a shortdate so I can fetch everything that was posted on that day.

The following code will set the variabel to the latest date on the table.
[sourcecode language=’sql’]
DECLARE @latestDate as datetime
SET @latestDate = (SELECT postDate from table where postDate = (Select max(postDate) from table))
This will return Jan 20 2009 6:45PM

Then all I need to add to my SELECT is
[sourcecode language=’sql’]
CONVERT(VARCHAR(10), @latestDate, 101)
This will return 01/20/2009


2 Comments for this entry

  • pradanang

    wow you really help me, thanks

  • developer

    I put the convert directly into my sql statement
    SELECT CONVERT(Varchar(11), DateColumnName), COLUMN2, COLUMN3
    WHERE ConditionHere

    I had to use 11 chars because it comes back like this:
    Jan 01 2009

7 Trackbacks / Pingbacks for this entry

Leave a Reply

You must be logged in to post a comment.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!