4

I have 3 tables in the database that I'm working on. Out of 3, two of the tables have columns that include dates. When I checked the information schema of the columns I found that dates have the wrong data type. If you see the picture below, the highlighted columns should be stored as DATE data type.

enter image description here

So, I used the following query to change their data type from varchar to DATE:

ALTER TABLE [dbo].[Customer]
ALTER COLUMN DOB DATE;

ALTER TABLE [dbo].[Transactions]
ALTER COLUMN tran_date DATE;

The error that I get is:

Conversion failed when converting date and/or time from character string.

Please let me know how I can fix this error. Thanks!

2
  • That means some of your data cannot be implicitly converted to a date - one of the big reasons never to use a varchar in the first place. You have to correct the data before you can change the column datatype. Try using try_convert to find the bad data. Commented Dec 28, 2020 at 20:06
  • 1
    Hopefully this has taught you one of the many reasons why varchar is the wrong datatype choice for a date and time value. Commented Dec 28, 2020 at 20:12

3 Answers 3

1

What you can do is update the value using try_convert() first and then alter the column name. Note: This will set any invalid values to NULL.

update customer
    set dob = try_convert(date, dob);

alter table customer alter column dbo date;

If you want to see the bad values, then before you change the table, run:

select c.*
from customer c
where try_convert(date, dob) is null and dob is not null;

You may have other ideas on how to fix the values.

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

Comments

1

You can't change from varchar to date or time or datetime by altering the column. Why? Because SQL Server does not know if the field contains '1/1/2020' or 'My dog is cute'. You will have to rebuild the table with the proper data types and then CAST() or CONVERT() the values to a true date time.

Underneath the hood, this makes more sense. A char/varchar uses one byte per character. nchar/nvarchar uses 2 bytes per character. A datetime is a number, not a character. This means you need a routine that changes this into the correct number. If you want to get deeper, the number is the number of ticks (nanoseconds) since midnight on January 1, 0001 in the Gregorian Calendar. More info.

2 Comments

Well you can, but SQL Server will do an implicit conversion if it can, which might not be what one wants.
@Dale K It depends on how you are doing it as well. SSMS restricts you fairly well from shooting yourself in the foot since 2012, maybe 2008. Prior to that, you could do this type of thing, which could really f*** up your database. :D
0

You can use the ISNULL function in SQL to convert an empty string to a null DateTime value The NULLIF function is used to return NULL if the value in date_column is an empty string. The ISNULL function then takes the result of NULLIF and replaces it with NULL if it is still NULL.

UPDATE table_name
SET date_column = ISNULL(NULLIF(date_column, ''), NULL);

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.