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.

Was it helpful?

Solution 2

@var is DateTime select cast(convert (varchar(8),@var,112) as Date)

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top