Rick
Rick

Reputation: 1569

Varchar date conversion in SQL

My date field has value format to be : Feb 15 2019. Is there a way to convert this to MMDDYYYY format?

Desired output: 02152019

Query I tried: SELECT convert(varchar, getdate(), 112) - this query is showing YYYYMMDD format :(

Any help?

Upvotes: 1

Views: 854

Answers (3)

Suraj Kumar
Suraj Kumar

Reputation: 5653

You can try this

Select convert(varchar(12), convert(date, 'Feb 15 2019'), 112)

or

SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 101), '/', '') AS [MMDDYYYY]

This will give output - 20190215.

You can pass different value like 101. For different output see Here.

Upvotes: 1

Alexander Volok
Alexander Volok

Reputation: 5940

select format( convert(date, 'Feb 15 2019'), 'MMddyyyy')

-- results to: 02152019


-- But again, your application is to take care about format!!! 

Upvotes: 4

Santosh
Santosh

Reputation: 886

Try this query,

select replace(convert(varchar, getdate(),101),'/','')

Upvotes: 2

Related Questions