Lutch
Lutch

Reputation: 119

Issue with Date format in SQL

I am currently using SQL Server 2008. I am extracting a column F254 value from a SQL query where it is returning the date format in MM/DD/YYYY (e.g. 8/17/2017).

I need the output to be in format YYYYMMDD (e.g. 20170817).

Note that the column F254 is of datatype char(10) and I cannot change the datatype.

I have tried below but the getting the needed output

  H.F254 AS Original_Date,  --> 8/17/2017
  CONVERT(VARCHAR(10), H.F254, 111) AS eg1, --> 8/17/2017
  REPLACE(CONVERT(VARCHAR(10), H.F254, 103), '/', '') AS eg2 -->8172017
  CONVERT(VARCHAR(9), H.F254, 112) AS eg3 --> 8/17/2017  

I have also checked the following Date Format but its not working

Upvotes: 2

Views: 114

Answers (1)

Søren Kongstad
Søren Kongstad

Reputation: 1440

I think you have to convert it to a date first!

select convert(varchar(10),cast(H.F254 as date),112)

Upvotes: 5

Related Questions