Skip to content
Advertisement

Trying to Enter DateTime Data into Date Column, Date data is in two different formats

CREATE TABLE Customer_test 
(     
    customer_Id BIGINT NOT NULL,
    DOB DATE NULL DEFAULT '01-01-1970',    
    Gender NVARCHAR(250) NULL, 
    city_code NVARCHAR(250) NULL
)    

INSERT INTO CUSTOMER_test (customer_Id, DOB, Gender, city_code)  
VALUES (270181, '01/10/1970', 'F', 2),
       (268073, '01/11/1970', 'M', 1),
       (273216, '15-01-1970', 'F', 5)

I get this error:

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

Advertisement

Answer

You should convert varchar to Date along with REPLACE by this way

CONVERT(datetime, REPLACE('01/11/1970', '/', '-'), 103)

Live demo here

Output

customer_Id     DOB        Gender   city_code
270181      1970-10-01        F      2
268073      1970-11-01        M      1
273216      1970-01-15        F      5
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement