1

I have a date field called TimeStampUTC in 2016-08-01 03:00:00.000 format but I need to return it in 01/08/2016

I'm using the following query

SELECT   Convert(varchar(2),DATEPART(DAY, a.TimeStampUTC)) + '/'+ Convert(varchar(2),DATEPART(MONTH, a.TimeStampUTC)) + '/' + Convert(varchar(4),DATEPART(year, a.TimeStampUTC))   AS Date FROM table AS a   WHERE a.TimeStampUTC > '$startDate' AND a.TimeStampUTC <='$endDate'

Issue is it return as 1/8/2016

How to write the query to return it as 01/08/2016 way.

Zusee Weekin
  • 1,348
  • 2
  • 18
  • 41

4 Answers4

1

You can get different datetime formats by using CONVERT() function,

SELECT   
Convert(varchar(15),TimeStampUTC,103 )   AS Date 
FROM table AS a   
WHERE a.TimeStampUTC > '$startDate' AND a.TimeStampUTC <='$endDate'

Here you can find the different date format codes: http://www.w3schools.com/sql/func_convert.asp

Sankar
  • 6,908
  • 2
  • 30
  • 53
0

you can use DATE_FORMAT()

DATE_FORMAT(yourdate,'%d-%m-%Y')

for your reference: sql date formats

Kate Sams
  • 51
  • 4
  • SQL Server has no `DATE_FORMAT()` function, SQL Server 2012 and newer have `FORMAT()`, but the way you input the date format is different than what you have here. – Hart CO Aug 01 '16 at 04:38
0
declare @TimeStampUTC datetime='2016-08-01 03:00:00.000' 
        select CONVERT(VARCHAR(10),convert(date,@TimeStampUTC),103)
brahmareddy
  • 121
  • 8
0

Assuming you are looking for DD/MM/YYYY, you can try this:

select REPLACE(CONVERT(varchar(15), a.TimeStampUTC, 105),'-','/')
Ket
  • 83
  • 8
  • 1
    No need to replace dashes with slashes, can just use format code `103`. Also 103 and 105 both have day first, not month. – Hart CO Aug 01 '16 at 04:43
  • Yes, that works too ! Thank you Hart, i learnt something new as well :) – Ket Aug 01 '16 at 04:44