Thursday, March 22, 2012

datetime Parameter issue

I'm using a sproc to insert the time (Now()) into a datetime field. Somehow the time "seconds" are not making it into the field. For example, the variable grabs the value of Now() - "9/16/2005 01:58:15 AM" to insert into the field. But when I view the records, the datetime value is "2005-09-16 13:58:00.000".
What am I doing wrong?
Thanks for your help!
Lynnette
Here is a snippet of the code and the sproc:

Dim recDateAs DateTime = Now()
cmd.Parameters.Add(New SqlParameter("@.DELastChg", SqlDbType.DateTime)).Value = recDate

ALTER PROC usp_AddTmpFLSAfromPT
@.dtWdate datetime,
@.whereString as varchar(255),
@.DEuser as varchar(30),
@.DELastChg datetime

AS

DECLARE @.strSQL as varchar(2000)

SET @.strSQL =
'
INSERT INTO tmpFLSAEmpInfo
( Emp_Number,
PT_ID,
Emp_Division,
Emp_Dept,
--Emp_DeptInfo,
Emp_Supervisor,
Emp_Location,
Emp_Union,
Emp_SG,
Emp_Shift,
DEusername,
DELastChg,
WrkDate
)
SELECT
Employee2.Emp_Number,
Employee2.[ID],
Employee2.Division,
Employee2.Job_Dept_Code,
Employee2.Job_Supervisor,
Employee2.Loc_Name,
Employee2.[Union],
Employee2.Sched_Group,
Employee2.Shift,
''' + @.DEUser + ''',
''' + CONVERT(varchar, @.DELastChg) + ''',
''' + CONVERT(varchar, @.dtWdate) + '''

FROM Employee2
WHERE '
+ @.whereString

EXEC(@.strSQL)

You are prbly losing the seconds because of the conversion to varchar in your code above. Put in the size also. Perhaps something like this :CONVERT(varchar(20), @.dtWdate) might help.|||Thanks for the response! I tried your suggestion, but it had no effect - still losing the seconds. I tried using CONVERT(datetime, @.DELastChg, 131) but then I get an error that I cannot convert string to DATETIME. I am hopelessly stuck.
Any other suggestions?
Thanks!
Lynnette|||Changed it toCONVERT(varchar, @.DELastChg, 113) and it works!

No comments:

Post a Comment