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

Hi all, need assistance with a simple table created but unable to insert values into it due to the above error

CREATE TABLE Ingredient(
ingredientCode CHAR(10) PRIMARY KEY NOT NULL,
name VARCHAR(20),
ingreType VARCHAR(20),
description VARCHAR(30),
dateLastStockTake DATE,
stockLevelCurrent VARCHAR(20),
stockLevelLastStock VARCHAR(20),
suggestedStockLevel VARCHAR(20)
)

INSERT INTO Ingredient VALUES ('I001','Bacon', 'Flour', 'BaconRashers', '27/4/2015', 'Low', 'Med', 'High')

--always received this error: Conversion failed when converting date and/or time from character string

  • Moved by Bob BeaucheminMVP Sunday, April 26, 2015 5:45 PM Moved to a relevant forum
April 26th, 2015 3:19pm

If you can't change the system data format, you may do it in the string using CONVERT:

https://msdn.microsoft.com/en-us/library/ms187928.aspx

INSERT INTO [Ingredient]([ingredientCode], [name], [ingreType], [description], [dateLastStockTake], [stockLevelCurrent], [stockLevelLastStock], [suggestedStockLevel])
SELECT
t.[ingredientCode],
t.[name],
t.[ingreType],
t.[description],
CONVERT(DATE, t.[dateLastStockTake], 103),  -- British/French Standard
t.[stockLevelCurrent],
t.[stockLevelLastStock],
t.[suggestedStockLevel]
FROM (VALUES
('I001','Bacon', 'Flour', 'BaconRashers', '27/4/2015', 'Low', 'Med', 'High')
) t([ingredientCode], [name], [ingreType], [description], [dateLastStockTake], [stockLevelCurrent], [stockLevelLastStock], [suggestedStockLevel]);

Hope this helps!

Free Windows Admin Tool Kit Click here and download it now
April 27th, 2015 2:45pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics