Convert date from dd-mm-yyyy to yyyy-mm-dd in SQL Server
Asked Answered
M

4

6

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.

Mickeymicki answered 28/10, 2015 at 7:11 Comment(2)
Because this is string not a date.Maurey
@Igor, Oh! Got it. Thank you so much.Mickeymicki
P
19

Try this:

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

SELECT CONVERT(VARCHAR(10), CONVERT(date, @Date1, 105), 23) 
Proficiency answered 28/10, 2015 at 7:18 Comment(3)
How to convert '31-12-99' to '9999-12-31' i.e. YYYY-MM-DD in SQL serverColumbium
SELECT CONVERT(VARCHAR(10), CONVERT(date, @Date1, 5), 23)Sonneteer
@Giorgi-Nakeuri Do we have equivalent function in MySql?Potboy
F
5

Try this way

SELECT CONVERT(date,'30-01-2015',103)
Firstnighter answered 28/10, 2015 at 7:17 Comment(3)
One note on this. This is not formatting. It is just casting to date type.Proficiency
@GiorgiNakeuri, Exactly! And because of that reason I'm going to accept your answer.Mickeymicki
How to convert '31-12-99' to '9999-12-31' i.e. YYYY-MM-DD in SQL serverColumbium
G
1

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
Glasswort answered 19/11, 2019 at 9:31 Comment(0)
P
0
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)
Possession answered 9/3, 2023 at 13:20 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.