date + time from vb6 to SQL - 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

Related

How do I concatenate a variable of the type Date/Time in my Insert statement

This runs when the form loads:
Private Sub Form_Load()
TempVars("F_ID") = txtF_SNr.Value
TempVars("Status") = txtStatus.Value
TempVars("seit") = txtSeit.Value
TempVars("bis") = txtBis.Value
TempVars("von") = txtVon.Value
TempVars("an") = txtAn.Value
TempVars("Bemerkung") = txtBemerkung.Value
End Sub
txtSeit is the name of the text field in my form, bound to the column [seit] with the type of Date/Time in my table.
sql = "Insert into tblStatusFahrzeugeHistory (F_ID, Status, seit, bis, von, an, Bemerkung, Datenbank_Nutzer, Eintrag_erstellt_am, Eintrag_erstellt_um)" & _
"Select tblStatusFahrzeuge.F_ID, tblStatusFahrzeuge.Status, tblStatusFahrzeuge.seit, tblStatusFahrzeuge.bis, tblStatusFahrzeuge.von, tblStatusFahrzeuge.an, tblStatusFahrzeuge.Bemerkung, tblStatusFahrzeuge.Datenbank_Nutzer, tblStatusFahrzeuge.Eintrag_erstellt_am, tblStatusFahrzeuge.Eintrag_erstellt_um " & _
"From tblStatusFahrzeuge Where ( " & txtSeit.Value & " <> seit AND [F_ID] = '" & Me.txtF_SNr.Value & "') "
The sql statement above gives me the error:
runtime error: '3075' syntax error in number in query expression...
How do I concatenate txtSeit.Value correctly? (Sorry im a beginner!)

SQL Syntax to Insert Dates and Times into a Database

In my project, I need to convert the following function, which creates a string to insert into a database, into the correct syntax for inserting a datetime datatype... Does anyone know a way I can do this?
Public Shared Function sqlString(str As String) As String
Return "'" & str.Replace("'", "''") & "'"
End Function
sql = "INSERT INTO tblModules(ModuleID, ModuleName, NumberUsers, License, Username, ContractID,datetime) VALUES(" & modID & ", " & sqlString(modname) & ", " & numusers & "," & sqlString(license) & ", " & sqlString(username) & ", " & contractID & ","&sqlDateTime(DateTime.Now)&")"
try this
In the end, I stored the values as either a date or a time by calling the following functions respectively, during the INSERT statement
Public Shared Function sqlDate(dt As DateTime) As String
If dt.Date = Nothing Then
Return dt.ToString("")
Else
Return dt.ToString("yyyy-MM-dd")
End If
End Function
Public Shared Function sqlTime(dt As DateTime) As String
Return dt.ToString("hhmmss")
End Function

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.

Convert date time (dd/MM/yyyy)

I have a column (datetime) in a Datagridview called DBO (date of birth) and it is converted with MM/dd/yyyy and I want to change it to this style (dd/MM/yyyy). I made this code and I get a error like " The conversion of the string " dd / mm / yyyy" for the ' Integer' type is not valid.
this is the sql string
myCommand = "UPDATE DoctorBasic SET " & _
"DOB = convert(datetime, '" & Convert_Null(DGV1(9, 0).Value.ToString("dd/MM/yyyy"), #1/1/1900#) & "', 103) " & _
"WHERE DoctorId = " & DGV1(0, 0).Value
DGV1 = datagriview
convert_null is a sub that I created to not get a error when the field is null and I want to made some changes and save it.
I don't understand what is wrong.. and what is that integer type in the error.

Conversion failed when converting the nvarchar

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.