Click here to Skip to main content
15,664,506 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, I need help with the question below. I wrote the script but it is giving me an error. There are three tables

Users:(UserId,EmailAddress, FirstName, LastName)
Products:(ProductId, ProductName)

Add three rows to the Downloads table: one row for user 1 and product 2; one for user 2 and product 1; and one for user 2 and product 2. Use the GETDATE function to insert the current date and time into the DownloadDate column.

Thank you

What I have tried:

The script I wrote is as follows:

Insert into Downloads (UserId, ProductID, DownloadDate)
('1',Products 2),GETDATE()),
('2',Products 1,GETDATE()),
('2',Products 2,GETDATE());

But I am getting the following error:
sg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value 'Products 2' to data type int.

How can I correct this?

Thank you
Updated 3-Aug-18 18:32pm

Simple: "Products 2" is not a integer! (Plus, there is a spurious close bracket in there) Even as a string, it would need to be a quoted value:
INSERT INTO Downloads (UserId, ProductID, DownloadDate) 
   ('1', 'Products 2',GETDATE()),
   ('2', 'Products 1',GETDATE()),
   ('2', 'Products 2',GETDATE());
But ... what datatype is ProductID, in both the Products and Downloads tables - they need to be the same, and the inference from the error message is that at least one of them is an INT while you are passing a VARCHAR (or NVARCHAR) value.

I'd have a close look at your table definitions and check what types you are trying to use: the quoted UserID kinda implies that's a string as well and it probably should be an integer, and not quoted in the INSERT statement.
Share this answer
Member 13937439 4-Aug-18 14:26pm    
Hi OriginalGriff, the order of the Downloads Table is

The ProductID in both the Products and Downloads Tables is set as an INT. One is a primary key and the other a foreign key. I hope this helps. I tried the code but it gave me the same error.

Thank you
OriginalGriff 5-Aug-18 3:38am    
Read what I said again.
The text "Products 2" is not an integer! Integers are 1, 2, 3, 796, 2147483648 - they do not contain the characters 'A' to 'Z', 'a' to 'z', spaces, dollar signs, or any punctuation or special characters...
--String values must be under single quotations('Products2') but not integers.
--IF your DownloadDate column datatype is varchar then use convert(Varchar(20),GETDATE(),101) 
Insert into Downloads (UserId, ProductID, DownloadDate) 

Share this answer
Member 13937439 4-Aug-18 14:25pm    
The solution you provided did not work. It gave me the same error message. In the DownloadS Table, the order of columns is as follows:

My DownloadDate Column datatype is DateTime, NOT NULL. I hope this helps.

Thank you
Santosh kumar Pithani 6-Aug-18 0:14am    
Hello, i already mentioned you that integer datatype doesn't allows string values.if your using varchar or char datatypes then make sure its length i.e varchar(500),char(50).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900