Mardus Davel
Mardus Davel

Reputation: 77

Conversion Failed when converting date and/or time

I am running into an issue. I get the Conversion failed error on the code sniped below.

I can't seem to pinpoint why this is happening, in my Dev environment I do not get the error and everything runs smoothly.

  Dim PrdDate As Date
        SQL.ReturnRead = 0
        PrdDate = dtpDataInput.Value.Date
        MsgBox(PrdDate)
        SQL.ReadQuery("SELECT CAST(DATEDIFF(second, TimeDown, TimeUp) / 60 / 60 % 24  AS NVARCHAR(50)) + '.'
               + CAST(DATEDIFF(second, TimeDown, TimeUp) / 60 % 60 AS NVARCHAR(50)) as HrsDown From UDOData
               WHERE (cast(TimeDown AS DATE) = '" & PrdDate & "' 
               AND CAST(TimeUp AS DATE) = '" & PrdDate & "') and FleetNo = '" & FleetStr & "'")
        UDOHRS = SQL.ReturnRead

I have checked the regional settings on the live environment and all seems ok.

Thank you for everyone's help

Upvotes: 0

Views: 93

Answers (1)

Eduardo Molteni
Eduardo Molteni

Reputation: 39413

When formatting for SQL Server it's better to always format it as YYYYMMDD.

Just use

 WHERE (cast(TimeDown AS DATE) = '" & PrdDate.ToString("yyyyMMdd") & "' 

Upvotes: 2

Related Questions