Jump to content

Problem with search of MSSQL database using datetime


Recommended Posts

I am attempting to create a script which will poll a SQL database that is part of our internal server monitoring application. The results would then be pushed to the local growl client on my desktop. I am successful at querying the SQL database using the SQL UDF and can send growl notifications fine as well...

What I run into is "Syntax error converting datetime from character string." when I try to query the database with the value from a previous query. (My appologies for making this sound confusing)

For example, I query the database and return the results into a 2D array. I can display the results fine with "_ArrayDisplay". I then take the results from the query and using the next to last row from the results, get the value from the datetime column.

$tmpDate = String($aData[$iRows-1][1])
MsgBox(0,"Date",$tmpDate)

It is when I take the value in $tmpDate and attempt to search with it that I get the syntax error above.

$iRval = _SQL_GetTable2D(-1, "select * from dbo.AllpagesToday WHERE PageRecip = 'SERVER_OC1' OR PageRecip = 'SERVER_ONCALL' AND Date > '" & $tmpDate & "' ORDER BY Date;", $aData, $iRows, $iColumns)

I have also attempted to convert the date format from: 20120319090303 to 2012-03-19 09:03:03.000 to no avail.

My conversion pieced together from other posts:

Func _convertToDateTime($dtString)
Local $newdt
$newdt = StringRegExpReplace($dtString, "A(d{4})(d{2})(d{2})(d{2})(d{2})(d{2})(?:.*)","$1-$2-$3 $4:$5:$6")
MsgBox(0,"Convert Results",$newdt)
Return($newdt)
EndFunc

When I search using the converted datetime, it is as if the query ignored the datetime value and returns all values of the 2D array.

$iRval = _SQL_GetTable2D(-1, "select * from dbo.AllpagesToday WHERE PageRecip = 'SERVER_OC1' OR PageRecip = 'SERVER_ONCALL' AND Date > '" & _convertToDateTime($tmpDate) & "' ORDER BY Date;", $aData, $iRows, $iColumns)

Any suggestions? If more information is needed, let me know.

Edited by gritts
Link to post
Share on other sites

"select * from dbo.AllpagesToday WHERE PageRecip = 'SERVER_OC1' OR PageRecip = 'SERVER_ONCALL' AND Date > '"

This should be i think:

"select * from dbo.AllpagesToday WHERE (PageRecip = 'SERVER_OC1' OR PageRecip = 'SERVER_ONCALL') AND Date > '"

Test the SQL query somewhere outside outoit to see how/if it works.

Edited by Juvigy
Link to post
Share on other sites

Thank you Juvigy, I had thought about using parenthesis but did not try. Here is my resulting line in my script that works...

$iRval = _SQL_GetTable2D(-1, "select * from dbo.AllpagesToday WHERE (PageRecip = 'SERVER_OC1' OR PageRecip = 'SERVER_ONCALL') AND Date > '" & _convertToDateTime($tmpDate) & "' ORDER BY Date;", $aData, $iRows, $iColumns)
Edited by gritts
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Arual
      We have AutoIt code that performs a connection to an MS SQL database running SQL server 2012, tls 1.0. It has worked successfully for years.
      Today, our IT department migrated our database to a new server that is running SQL server 2016, tls 1.2. Now our connection string is no longer working.
      Here is the original that was working in SQL server 2012
      $sConnStr = "DRIVER={SQL Server};SERVER=servername,port;DATABASE=dbname;UID=user;PWD=pass" When that would run on the new server in SQL server 2016. We are getting the error [Microsoft][ODBC SQL Server Driver][DBNETLIB]SSL Security error
      We tried changing the connection string to 
      $sConnStr = "DRIVER={SQL Server Native Client 11.0};SERVER=servername,port;DATABASE=dbname;UID=user;PWD=pass" The new error we received is [Microsoft]
      TCP Provider: An existing connection was forcibly closed by the remote host. We can log in successfully using SSMS using the server name, port and user/pass.
      Any suggestions on what we need to change in order to get the connection to work again?
    • By nacerbaaziz
      goodmorning; autoit team
      please their are any youtube search way working?
      because i was using the get source and split it to get the result but know it does not working any way.
      is the youtube disabled that? and is their any other simple way to do that?
      i tested all examples found in this post but also it don't work
      https://www.autoitscript.com/forum/topic/123945-youtube-search/
      and here is the example that i use to and it don't work any more
      local $hSearchOpenHNDL, $hSearchConnect, $sSearchGet local $a_UrlsArray[1][5] local $b_ButtonsDisabled = false, $b_SearchBTNFocus = false, $b_SearchListFocus, $h_SearchFocusHND local $Return = "0" local $s_OpenStringY = "/feed/trending" local $ChannelUrl = "", $channelName = "" if Not ($a_YoutubeSearchArray[0][0] = 0) then GUICtrlSetData($searchInp, $s_youtubeSearchLastSearch) $s_OpenStringY = "/results?search_query=" & StringReplace(GUICtrlRead($searchInp), " ", "+") $a_UrlsArray = $a_YoutubeSearchArray for $i = 1 to $a_UrlsArray[0][0] _GUICtrlListBox_AddString($SearchList, $a_UrlsArray[$i][0] & $a_UrlsArray[$i][2] & $a_UrlsArray[$i][3]) next _GUICtrlListBox_SetCurSel($SearchList, $I_youtubeSearchLastIndex-1) GUICtrlSetState($SearchList, $GUI_focus) else if Ping("youtube.com", 1000) > 1 then $hSearchOpenHNDL = _WinHttpOpen('') if not (@Error) then $hSearchConnect = _WinHttpConnect($hSearchOpenHNDL, "youtube.com") if Not (@Error) then $sSearchGet = _WinHttpSimpleRequest($hSearchConnect, "get", $s_OpenStringY) if not (@Error) then local $a_strings = _StringBetween($sSearchGet, '<a href="/watch', "<ul") local $title = "" local $url = "" local $length = "" local $result = "" GUICtrlSetData($SearchList, "") ReDim $a_UrlsArray[1][5] for $i = 0 to UBound($a_strings)-1 $url = _StringBetween($a_strings[$i], "?", '"') if @error then ContinueLoop $url = "https://www.youtube.com/watch?" & $url[0] $title = _StringBetween($a_strings[$i], 'dir="', '</a>') if @error then ContinueLoop $title = $title[0] $title = StringRegExpReplace($title, '(.*\"\>)', "") if StringRegExp($a_strings[$i], '[0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}', 0) = 1 then $length = StringRegExp($a_strings[$i], '[0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}', 2) elseIf StringRegExp($a_strings[$i], '[0-9]{1,2}\:[0-9]{1,2}', 0) = 1 then $length = StringRegExp($a_strings[$i], '[0-9]{1,2}\:[0-9]{1,2}', 2) else $length = "" endIf if IsArray($length) then $length = ": (" & $length[0] & ")" else $length = "" endIf $ChannelUrl = stringRegexpReplace($a_strings[$i], '(^.*?<a.*?\"(\/user|\/channel))+', "$2") $channelName = stringRegexpReplace($ChannelUrl, '(.*?\".*?>)(.*</a>)+', "$2") $ChannelUrl = stringRegexpReplace($ChannelUrl, '(\".*)+', "") $channelName = stringRegexpReplace($channelName, '(</a>.*)+', "") $result &= $title & @crlf & $url & @crlf ReDim $a_UrlsArray[UBound($a_UrlsArray)+1][5] $a_UrlsArray[UBound($a_UrlsArray)-1][0] = $title $a_UrlsArray[UBound($a_UrlsArray)-1][1] = $url $a_UrlsArray[UBound($a_UrlsArray)-1][2] = $length if not ($channelName = "") then $a_UrlsArray[UBound($a_UrlsArray)-1][3] = ", (" & $channelName & ")" if not ($channelUrl = "") then $a_UrlsArray[UBound($a_UrlsArray)-1][4] = "https://www.youtube.com" & $channelUrl $a_UrlsArray[0][0] = UBound($a_UrlsArray)-1 _GUICtrlListBox_AddString($SearchList, $a_UrlsArray[UBound($a_UrlsArray)-1][0] & $length & $a_UrlsArray[UBound($a_UrlsArray)-1][3]) next endIf endIf endIf endIf endIf  
      i hope any one can help me
      thanks in advance
    • By hugomito
      Hi all,
      I am having issues connecting to an MSSQL DB from a MacBook. The following code works fine from Windows 7 and Windows 10, but it is not working from MacBook.
      #include <file.au3> $obj_ErrorHandling = ObjEvent ("AutoIt.Error", "f_ErrorHandling") Const $c_SCRIPTNAME = StringLeft(@ScriptName, StringLen(@ScriptName) - 4) Const $c_EAILogFile = @ScriptDir & "\" & $c_SCRIPTNAME & "_" & StringUpper (@UserName) & ".log" Const $c_SCRIPTLOGFILE = FileOpen ($c_EAILogFile, $FO_OVERWRITE) FileWrite ($c_SCRIPTLOGFILE, "[INFORMATION] Variables definition" & @CRLF) $txt_UserId = "User123" $txt_Password = "Password123" $txt_ProviderDatasource = "Provider=SQLOLEDB;Data Source=server.companyname.com\ss123; Initial Catalog=MyTools; User ID=" & $txt_UserId & "; Password=" & $txt_Password FileWrite ($c_SCRIPTLOGFILE, "[INFORMATION] Creating object" & @CRLF) $conn_Database = ObjCreate ("ADODB.Connection") FileWrite ($c_SCRIPTLOGFILE, "[INFORMATION] Initializing connection string" & @CRLF) $conn_Database.ConnectionString = $txt_ProviderDatasource FileWrite ($c_SCRIPTLOGFILE, "[INFORMATION] Connecting to Database" & @CRLF) $conn_Database.Open FileWrite ($c_SCRIPTLOGFILE, "[INFORMATION] Database connected" & @CRLF) Func f_ErrorHandling ($obj_ErrorHandling) $HexNumber = Hex ($obj_ErrorHandling.number, 8) FileWrite ($c_SCRIPTLOGFILE, "[INFORMATION] Error occured while connecting Database!" & @CRLF) MsgBox (0, $c_SCRIPTNAME, "We intercepted a COM Error !" & @CRLF & @CRLF & _ "Error Number: " & $obj_ErrorHandling.Number & @CRLF & _ "Error Description: " & $obj_ErrorHandling.description & @CRLF & _ "Error WinDescription:" & $obj_ErrorHandling.windescription & @CRLF & _ "Error Last DLLL Error: " & $obj_ErrorHandling.lastdllerror & @CRLF & _ "Error Script Line: " & $obj_ErrorHandling.scriptline & @CRLF & _ "Error Source: " & $obj_ErrorHandling.source) SetError(1) Exit (1) EndFunc After executing it in a MacBook, the log file contains:
      [INFORMATION] Variables definition
      [INFORMATION] Creating object
      [INFORMATION] Error occured while connecting Database!
      And the error message is attached to this post.
       
      Can anyone guide me how to resolve this issue?
      Thanks in Advance.

    • By nooneclose
      My program has to first search for names in Column D that do not match up with column C. I got that search to work using arrays. It was slow and I could not figure out how to delete them so I just manually put coded the names that do not belong. I found their cell location but I do not know how to store that location and delete it.
      This is what I have so far.
      Local $NameToDelete1[6]  = _Excel_RangeFind($OpenWorkbook, "Smith, Bill") _ArrayDisplay($NameToDelete1, "Excel UDF: _Excel_RangeFind Example 1", "", 0, "|", "Sheet|Name|Cell|Value|Formula|Comment") _Excel_RangeDelete($OpenWorkbook.ActiveSheet, $NameToDelete1[2], $xlShiftUp)  
      Please help, I wanted to have this program done yesterday but I did not see this problem until yesterday. 
×
×
  • Create New...