@var is DateTime select cast(convert (varchar(8),@var,112) as Date)
how to solve Passing DateTime value to paramaterized query Error
-
29-09-2022 - |
Question
here is my query string
SELECT
Payment
,Balance
,PatientNo
FROM
[GP_DB].[dbo].[GP]
where GP.GPDate= (SELECT CONVERT(VARCHAR(24),@GPDate,103))
GPDate is a Date type column, not DateTime
and i pass parameter like this
cmd_select_treatment.Parameters.AddWithValue(
"@GPDate"
,Convert.ToDateTime(dateTimePicker1.Value));
but the following error occur
Conversion failed when converting date and/or time from character string.
Solution 2
OTHER TIPS
GPDate is a Date type column
If it's a DATE
column and your Convert.ToDateTime
call returns a DateTime
object then don't bother yourself with the CAST
.
WHERE GP.GPDate = @GPDate
If dateTimePicker1
might contain a time component and you don't care for that then simply discard it before using its value:
Convert.ToDateTime(dateTimePicker1.Value).Date
I Think you need to add picture clause in your date conversion like the example
to_date('18/01/2014 10:15:20','dd/mm/yyyy hh24:mi:ss');
this should solve your problem