Question

I have a table where I select customer purchase activity and it will display the date of purchase (along with the time) in ColumnA, in this format:

2012-08-06 10:06:42.223
YYYY-MM-DD HH:MM:SS

I want to display the date only, so I added a LEFT function like so:

LEFT(ColumnA, 11)

This will get rid of the time but now the date is displayed I will get:

Aug  06 2012

I want to display it in numeric format but as MM/DD/YYYY so it will look like:

08/06/2012
MM/DD/YYYY

Any ideas how I can do this in T-SQL?

Was it helpful?

Solution

You just need to convert it:

SELECT convert(char(10), getdate(), 101)

or from your table:

SELECT convert(char(10), yourDate, 101)
FROM yourTable

Here is a handy website with the conversions:

How to format datetime & date in Sql Server 2005

Or if you just want yourDate value without the time, you can always use which results in 2012-08-06 00:00:00.000:

declare @yourDate datetime
set @yourdate = '2012-08-06 10:06:42.223'

select DateAdd(day, DateDiff(day, 0, @yourdate), 0)

Or from yourTable:

select DateAdd(day, DateDiff(day, 0, yourDateField), 0)
from yourTable

OTHER TIPS

Try this:

SELECT CONVERT(VARCHAR(10), ColumnA, 101)
FROM MyTable
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top