Conversion failed when converting the nvarchar - sql

strSQL = "SELECT cdlAction, " & _
"cdlSerial_Number, " & _
"cdlRemedyProcess " & _
"From tblCustodial " & _
"WHERE cdlRemedyProcess IS NULL AND LEN(cdlAction)=7 AND cdlAction<>'Dispose' AND LEFT(cdlAction,2) BETWEEN 5 AND 35 " & _
"ORDER BY cdlAction"
I keep getting a "Conversion failed when converting the nvarchar" at this sql string when I debug. I do not see what I am doing wrong here.

I think casting the left(cdlAction,2) to an int might do it. He cant compare a string to an int from its own. So cast(cdlAction AS INT) might work.

Related

VBA bad syntax in dlookup function with global variables

I am using 8 similar functions in my code and they all work, but this one is not working:
global_var_13=DLookup("[amount]", "[tabla amortizacion real]","[no contract]= get_global('global_var_10') and [ident]=get_global('global_var_11') and [cuota]= get_global('global_var_12')")
var_10 is a string, var_11, var_12, and var_13 are numeric.
The values are var_10= "GAF-27/2013", var_11=1, var_12=1
global_var_13 is returning NULL
Any thoughts about the syntax?
Try to concatenate the clause elements:
global_var_13 = DLookup("[amount]", "[tabla amortizacion real]", "[no contract] = '" & get_global("global_var_10") & "' and [ident] = " & get_global("global_var_11") & " and [cuota] = " & get_global("global_var_12") & "")

date + time from vb6 to SQL

I have this code in VB6
Dim datahorS As String
datahorS = Text21.text & " " & Text22.text
Label2.Caption = datahorS
SQL = " insert into TabNFe_x ( data_hora_ent) values " _
& "(" & "'" & datahorS & "'" & ")"
datahorS = 20/10/2016 13:54
and the command
insert into TabNFe_x ( data_hora_ent) values ('20/10/2016 13:54')
erro from SQL
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
what's i m making wrong.
Convert your string into a datetime
Select convert(datetime, '20/10/2016 13:54', 103)
Returns
2016-10-20 13:54:00.000

SQL Statement in Access

Ive been trying to get a query I ran in Access to run in VBA but I keep getting errors due to the number of exclamation marks I've been using. The statement I am using is
SQLstat = "SELECT tbl_Date_Check.DateofChecklist, tbl_Tasks.QuestionNumber,tbl_Tasks.Frequency, tbl_Tasks.Questions " _
& "FROM tbl_Tasks, tbl_Date_Check " _
& "WHERE (((tbl_Date_Check.DateofChecklist)=""" & [Forms]![Daily_Checker]![TxtDate] & """) And ((tbl_Tasks.Frequency) = """ & [Forms]![Daily_Checker]![ComFreq]"""))"
Any help would be great thanks
This can possibly be explained by the following SO question: What is the difference between single and double quotes in SQL?
This explains that you need to utilize single quotes '' to surround text in SQL in almost every instance. The fact that you are using double quotes "" may be what is causing the error.
I hope this helps.
-C§
It must read like this for dates:
SQLstat = "SELECT tbl_Date_Check.DateofChecklist, tbl_Tasks.QuestionNumber,tbl_Tasks.Frequency, tbl_Tasks.Questions " _
& "FROM tbl_Tasks, tbl_Date_Check " _
& "WHERE ((tbl_Date_Check.DateofChecklist = #" & Format([Forms]![Daily_Checker]![TxtDate], "yyyy\/mm\/dd") & "#) And (tbl_Tasks.Frequency = " & [Forms]![Daily_Checker]![ComFreq] & "))"

CAST - The data types datetime and datetime2 are incompatible in the add operator

I get the following error
Microsoft OLE DB Provider for SQL Server error '80040e14' The data
types datetime and datetime2 are incompatible in the add operator.
<%
Set cnnSimple = Server.CreateObject("ADODB.Connection")
cnnSimple.Open "Provider=SQLOLEDB.1;Initial Catalog=P;DataSource=S\SQLEXPRESS; Persist Security Info=True;User ID=xx;Password=xxxxxxx"
sql = "SELECT DISTINCT" & _
"'<option value=""' + CAST([date_required] AS DATETIME) + '"">' + " & _
"[date_required] + '</option>' " & _
"FROM " & _
"qryOutstandingPOLinesSource2 where supplier='" & suppliercode &"' and date_required < GETDATE()"
set rs = Server.CreateObject("ADODB.RecordSet")
rs.Open sql, cnnSimple, 0, 1, 1
if rs.eof then
no_rows3 = true
else
str = rs.GetString()
End If
%>
Is there a workaround ? I have tried casting as VARCHAR and that didn't work.
You need to simply cast your data to NVARCHAR, like so...
sql = "SELECT DISTINCT" & _
"'<option value=""' + CONVERT(NVARCHAR, [date_required]) + '"">' + " & _
"CONVERT(NVARCHAR, [date_required]) + '</option>' " & _
"FROM " & _
"qryOutstandingPOLinesSource2 where supplier='" & suppliercode &"' and date_required < GETDATE()"
Edited as per Lankmart's comment. The use of CONVERT instead of CAST will allow marginally better formatting abilities but is SQL Server specific, whereas you may find CAST is available in a wider range of T-SQL implementations.

datatype mismatch in criteria expression in MS Access

I am creating form in access in which i have to implement cascading combo boxes , data in lower combo box is dependent on its parent value selected by user. Here is form
on left side is structure of the table and on right side is form. Problem is I am getting error of data type mismatch , unable to understand why this is happening. In the afterupdate event of Diametre of Drill I am populating cutting speed . Whenever I press drop down of Cutting Speed "Datatype mismatch in criteria expression" occurs. Here is code of afterupdate event of Diametre of Drill
Private Sub cboDiameterDrilling_AfterUpdate()
cboCuttingSpeedDrilling.RowSource = "Select DISTINCT tblDrilling.cuttingSpeed " & _
"FROM tblDrilling " & _
`"WHERE tblDrilling.materials = '" & cboMaterialDrilling.Value & "' AND tblDrilling.diaOfDrill = '` `cboDiameterDrilling.Value ' " & _`
"ORDER BY tblDrilling.cuttingSpeed;"
End Sub
I think problem is in WHERE Clause . Any help would be greatly appreciated. Thank you
You've surrounded the reference to the object's value (cboDiameterDrilling.Value ) in single quotes.
AND tblDrilling.diaOfDrill = ' & cboDiameterDrilling.Value & "'"
Solution : AND tblDrilling.diaOfDrill = " & cboDiameterDrilling.Value & " " & _
I think you missed a ". Try:
Private Sub cboDiameterDrilling_AfterUpdate()
cboCuttingSpeedDrilling.RowSource = "Select DISTINCT tblDrilling.cuttingSpeed " & _
"FROM tblDrilling " & _
`"WHERE tblDrilling.materials = '" & cboMaterialDrilling.Value & "' AND tblDrilling.diaOfDrill = '" & cboDiameterDrilling.Value & "' " & _
"ORDER BY tblDrilling.cuttingSpeed;"
End Sub