MAK
MAK

Reputation: 7260

Convert date from dd-mm-yyyy to yyyy-mm-dd in SQL Server

I want to convert given date into the format YYYY-MM-DD.

Given date:

DECLARE @Date1 VARCHAR(50) = '30-01-2015'

Now I want to convert it into the 2015-01-30.

My try:

Try1:

SELECT CONVERT(VARCHAR(50),'30-01-2015',126)

Try2:

SELECT CONVERT(VARCHAR(50),'30-01-2015',120)

For both try the result remain same that is 30-01-2015.

Upvotes: 6

Views: 83400

Answers (4)

jamiel22
jamiel22

Reputation: 157

LEFT(CONVERT(VARCHAR(19), CONVERT(datetime, my_date, 3), 120),10)

if you want to remove the time. Also

CONVERT(VARCHAR(10), CONVERT(datetime, my_date, 3), 120)

Upvotes: 0

Rinku Choudhary
Rinku Choudhary

Reputation: 2171

Convert date from dd-mm-yyyy hh:mm:ss to yyyy-mm-dd hh:mm:ss in SQL Server

convert(datetime,'18-11-2019 00:00:00',105)   // will return 2019-11-18 00:00:00.000

Upvotes: 1

Giorgi Nakeuri
Giorgi Nakeuri

Reputation: 35780

Try this:

DECLARE @Date1 VARCHAR(50) = '30-01-2015'

SELECT CONVERT(VARCHAR(10), CONVERT(date, @Date1, 105), 23) 

Upvotes: 19

Mukesh Kalgude
Mukesh Kalgude

Reputation: 4844

Try this way

SELECT CONVERT(date,'30-01-2015',103)

Upvotes: 5

Related Questions