1

I am trying to convert dd.mm.yyyy to yyyy-mm-dd.

select convert(date,CAST(WEEK_DATE as nvarchar(220)), 120) 
from z_fact

Error

Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.

How can I resolve this?

4
  • What is the type of WEEK_DATE, and can you show us sample data? Commented Apr 16, 2018 at 3:08
  • ex: 15.03.18 (format is dd.mm.yy) Commented Apr 16, 2018 at 3:12
  • nvarchar is the type of week_date Commented Apr 16, 2018 at 3:13
  • The lesson you should take away from this is to store dates as DateTime, not strings. Commented Apr 16, 2018 at 3:28

2 Answers 2

3

Since your date is actually text, you must first convert it to a bona fide date using CONVERT. Then, use CONVERT on that date a second time to generate the text output you want.

SELECT CONVERT(varchar(20), CONVERT(datetime, '15.03.18', 4), 120);

Demo

Note that it is generally bad practice to store your dates as text. Hopefully you can use my answer to tidy up your table. For example, you could add a new datetime column new_dt and then update it using:

UPDATE yourTable
SET new_dt = CONVERT(datetime, old_dt, 4);

Don't worry about the internal format used by SQL Server. If you still need to display yyyy-mm-dd output, then use CONVERT again, as I did in my first query.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, i want to convert all the date in the week_date column from dd.mm.yyyy to yyyy-mm-dd
No. Don't worry about the internal format of the date, just store as dates, period.
@TimBiegeleisen... I would recommend to specify the length of varchar while conversations.
"Note that it is generally bad practice to store your dates as text." I think it's always a bad idea... Even if you are dealing with dates that are outside the scope of DateTime2 (A.D or after 9999), it's better to store the dates as numbers (Like SQL Server does internally) then as strings. Also, I wouldn't recommend datetime but datetime2 - Here's why. All that being said, still this is a solid answer so +1.
@ZoharPeled In some databases, such as SQLite, there is no formal date type, so text is the only option for storing dates. There, it is absolutely critical that the text dates be in ISO format, so that they will sort and compare properly. Not so relevant to SQL Server, but just wanted to mention it.
-1

You can try this:

declare @dt NVARCHAR(12) = '15.03.18' 
SELECT  CONVERT(DATE,@dt,3)
GO

3 Comments

Thanks, i want to convert all the date in the week_date from dd.mm.yyyy to yyyy-mm-dd
by mistake i would have clicked when copied your code.
I didn't downvote your answer but the 3 style in format expects / and not . as a date separator. You want to use 4 instead - convert(date, @dt, 4). Even if your code worked, it's only a partial answer at best.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.