SQL converting datetime to shortdate
by Rizo 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))
[/sourcecode]
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)
[/sourcecode]
This will return 01/20/2009
2 Comments for this entry
6 Trackbacks / Pingbacks for this entry
-
หวยหุ้นดาวโจนส์
oktober 3rd, 2023 on 00:11… [Trackback]
[…] Read More Information here to that Topic: from-rizo.se/sql-converting-datetime-to-shortdate/ […]
-
superkaya88
november 18th, 2023 on 09:50… [Trackback]
[…] Find More on that Topic: from-rizo.se/sql-converting-datetime-to-shortdate/ […]
-
dk7
december 3rd, 2023 on 01:13… [Trackback]
[…] Information to that Topic: from-rizo.se/sql-converting-datetime-to-shortdate/ […]
-
ราคาบอลวันนี้
december 18th, 2023 on 01:19… [Trackback]
[…] Read More to that Topic: from-rizo.se/sql-converting-datetime-to-shortdate/ […]
-
ประตูสำเร็จรูป
december 23rd, 2023 on 01:20… [Trackback]
[…] Read More here to that Topic: from-rizo.se/sql-converting-datetime-to-shortdate/ […]
-
สีทนไฟ
april 19th, 2024 on 02:02… [Trackback]
[…] Find More on on that Topic: from-rizo.se/sql-converting-datetime-to-shortdate/ […]
juli 13th, 2009 on 17:15
wow you really help me, thanks
oktober 20th, 2009 on 21:40
I put the convert directly into my sql statement
SELECT CONVERT(Varchar(11), DateColumnName), COLUMN2, COLUMN3
FROM XYZ
WHERE ConditionHere
I had to use 11 chars because it comes back like this:
Jan 01 2009