0

I have a column stocked as a nvarchar(50) containing dates of this format ('yyyy/mm/dd'), i'd like to convert it to date so i can do some dateadds to make further comparaisons.

Nothing that i've seen so far has been working (was a lot about casts and converts) and always resulted in stuff like that :

"Msg 242, Level 16, State 3, Line 1 The conversion of a varchar data type to a datetime data type resulted in an out-of-range value."

9
  • First database design mistake: I have a column stocked as a nvarchar(50) containing dates Change the column to be a date Commented Jul 19, 2017 at 9:48
  • mysql or sql server? Commented Jul 19, 2017 at 9:50
  • Use STR_TO_DATE Commented Jul 19, 2017 at 9:50
  • @DIe No SQLServer I think# Commented Jul 19, 2017 at 9:51
  • 1
    State what you tried, present sample data, without either let alone both we might be unable to help you out. Commented Jul 19, 2017 at 9:59

3 Answers 3

1

Most likely you have data that cannot be converted properly. You can use a query like below to see the problem data( this works in SQL server 2012 and above)

declare @tableName table(stocked nvarchar(50));
insert into @tableName values
('2017/12/31'),('2017/11/11'),('2017/11/13'),('2017/13/11');
SET DATEFORMAT ymd;  

select
stocked,
TRY_PARSE(stocked as datetime)
from @tableName
where TRY_PARSE(stocked as datetime) is NULL;
Sign up to request clarification or add additional context in comments.

Comments

0

Use

ISDATE ( expression )

Returns 1 if the expression is a valid date, time, or datetime value; otherwise, 0.

Comments

0

Basicly i had an excel table that i inserted into SQL server 2014, didn't manage do convert the data to date at the insertion but i guess it would be less of a pain working on the way of insertion than convert it later on

Comments

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.