Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HEllo Team,

I have the update query as follows:
SQL
UPDATE [JOB] WITH (ROWLOCK)   SET [CLEARANCESTATUS] = 'Y'
    ,[CLEARANCEDATE] = '13-03-2014 15:42:59'  WHERE CONTAINERNO = 'PSSU3141338' AND BOOKBILLNO = '123654'


I am getting error as :
Msg 242, Level 16, State 3, Line 1
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
The statement has been terminated.

will you guide me why is it so? I am confused.

Thanks
Harshal
Posted
Comments
R Harshal 14-Mar-14 6:07am    
I have the table :
CREATE TABLE [dbo].[JOB](
[BOOKBILLNO] [varchar](9),
[CONTAINERNO] [varchar](12),
[CLEARANCEDATE] [datetime] ,
[CLEARANCESTATUS] [varchar](1)
)

Try supplying the date in ISO 8601 format: "yyyy-MM-dd hh:mm:ss" - it's what SQL understands best:
SQL
UPDATE [JOB] WITH (ROWLOCK)   SET [CLEARANCESTATUS] = 'Y'
    ,[CLEARANCEDATE] = '2014-03-13 15:42:59'  WHERE CONTAINERNO = 'PSSU3141338' AND BOOKBILLNO = '123654'

Or better, pass it as a DateTime value via a parameterized query (as I suggested with your last question)
 
Share this answer
 
Comments
R Harshal 14-Mar-14 6:27am    
I am using parameterised query as follow:
if (customClearance.MessageType == "RRA01")
{
sQuery.Append("UPDATE [JOB] WITH (ROWLOCK)");
sQuery.Append(" SET [CLEARANCESTATUS] = 'Y' ");
sQuery.Append(" ,[CLEARANCEDATE] = '" + customClearance.ContainerDate + "' ");
sQuery.Append(" WHERE CONTAINERNO = '" + customClearance.ContainerNo + "'");
sQuery.Append(" AND BOOKBILLNO = '" + customClearance.BookBillNo.ToString() + "'");
}
OriginalGriff 14-Mar-14 6:31am    
Ant isn't a parameterized query - that's concatenating strings, which is a very dangerous way to do things: it leave you wide open to SQL Injection attacks which can damage or destroy your database. A parameterised query looks like this:
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand com = new SqlCommand("UPDATE myTable SET myColumn1=@C1, myColumn2=@C2 WHERE Id=@ID", con))
{
com.Parameters.AddWithValue("@ID", id);
com.Parameters.AddWithValue("@C1", myValueForColumn1);
com.Parameters.AddWithValue("@C2", myValueForColumn2);
com.ExecuteNonQuery();
}
}
go with solution1
else

SQL
alter table JOB alter column CLEARANCEDATE nvarchar(max)

SQL
UPDATE [JOB] WITH (ROWLOCK)   SET [CLEARANCESTATUS] = 'Y'
    ,[CLEARANCEDATE] = '2014-03-13 15:42:59'  WHERE CONTAINERNO = 'PSSU3141338' AND BOOKBILLNO = '123654'
 
Share this answer
 
Comments
R Harshal 14-Mar-14 6:28am    
I am using parameterised query as follow:
if (customClearance.MessageType == "RRA01")
{
sQuery.Append("UPDATE [JOB] WITH (ROWLOCK)");
sQuery.Append(" SET [CLEARANCESTATUS] = 'Y' ");
sQuery.Append(" ,[CLEARANCEDATE] = '" + customClearance.ContainerDate + "' ");
sQuery.Append(" WHERE CONTAINERNO = '" + customClearance.ContainerNo + "'");
sQuery.Append(" AND BOOKBILLNO = '" + customClearance.BookBillNo.ToString() + "'");
}

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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