Jump to content

Convert Scientific number in array


Recommended Posts

Hey All!

Anyone know how to convert one of my columns to date from scientific value by chance in my RDL column?

Thanks!

[0]|DEST|HOUSEBILL|WGT|STATUS|CONSOL|RDL
[1]|ABE |H230145016  |414|Ready|TRUCK       |1.14041730900001e+017
[2]|ATL |H230145006  |511|Ready|TRUCK       |1.14041730900001e+017
 

Link to comment
Share on other sites

To be more specific.  I want to grab the value in my array that's coming from my sql query and convert, and replace with "mm-dd-yy hh:mm" format so I can put into a listview on my gui.

For some reason I had some logic to do this in my sqlquery it is usually a decimal and the function I have is convering into scientific.

Thanks!

This function below is bringing query results into my array.  It's taking a decimal from my query and converting in my RDL column.

Func QueryToArray($p_connection, $p_query)
 Local $i, $j, $l_objQuery, $l_result
 $l_objQuery = ObjCreate("ADODB.Recordset")
 if @error = 1 then return ''
 $l_objQuery.Open($p_query, $p_connection)
 if $l_objQuery.State = 0 then return ''
 Dim $l_result[1][$l_objQuery.Fields.Count]
 For $j = 0 to $l_objQuery.Fields.Count - 1
  $l_result[0][$j] = $l_objQuery.Fields($j).Name
 Next
 $i = 1
 if $l_objQuery.EOF then return $l_result
    Do
        For $j = 0 To $l_objQuery.Fields.Count - 1
   if $i < UBound($l_result) Then
    $l_result[$i][$j] = $l_objQuery.Fields.Item($j).value
   Else
    ReDim $l_result[Ubound($l_result, 1) + 1][$l_objQuery.Fields.Count]
    $l_result[$i][$j] = $l_objQuery.Fields.Item($j).value
   EndIf
        Next
        $l_objQuery.MoveNext
        $i = $i + 1
    Until $l_objQuery.EOF
 return $l_result
EndFunc


$l_objConn = ObjCreate("ADODB.Connection")
$l_objConn.Open("DRIVER={IBM DB2 ODBC DRIVER};HOSTNAME=" & $p_branch & "." & $p_branch & ".ei;PORT=50000;DATABASE=" & $p_branch & ";PROTOCOL=TCPIP;UID=" & $p_user & ";PWD=" & $p_password)
if @error Then
    MsgBox(0, "ERROR", "Failed to connect to the database")
    Exit
Else
    MsgBox(0, "Success!", "Connection TCS.gtwy")
EndIf

;$rbills_query = LoadSQLFile("ready_bills.sql")
$results = QueryToArray($l_objConn, $rbills_query)
_ArrayDisplay($results)
$l_objConn.Close ; ==> Close the database
Edited by dar100111
Link to comment
Share on other sites

If you paste the value returned here, into excel, it can convert to date time...maybe it's the same for sql?

$string = StringRegExpReplace(1.14041730900001e+017,"[^\d]","")
ConsoleWrite($string & @CRLF)
$string = StringRegExpReplace($string,"(\d{7})(.*)","$1.$2")
ConsoleWrite($string & @CRLF)

1140417.30900001017

Prior to the decimal is the count of days, from some start date...after is 24 * the remainder, to determin the hour/min/sec/etc.

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Whatever the decimal turns out to be, this can do it:

$Total = 24 * .30900001017
$aTotal = StringSplit($Total, ".")
$iHour = $aTotal[1]
$iTotal2 = 60 * ("." & $aTotal[2])
$aTotal2 = StringSplit($iTotal2, ".")
$iMinute = $aTotal2[1]
$iTotal3 = 60 * ("." & $aTotal2[2])
$aTotal3 = StringSplit($iTotal3, ".")
$iSecond = $aTotal3[1]

ConsoleWrite($iHour & ":" & $iMinute & ":" & $iSecond & "." & $aTotal3[2] & @CRLF  )
Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

2014/04/17 not the current day.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

How about:

$string = StringRegExpReplace(1.14041730900001e+017,"[^\d]","")
$string = StringRegExpReplace($string,"(\d{7})(.*)","$1.$2")
$aDays = StringSplit($string, ".")
$iDays = $aDays[1]+19000000

$iDays = StringRegExpReplace($iDays,"(\d{4})(\d{2})(\d{2})","$2/$3/$1")


$Total = 24 * ("." & $aDays[2])
$aTotal = StringSplit($Total, ".")
$iHour = $aTotal[1]
$iTotal2 = 60 * ("." & $aTotal[2])
$aTotal2 = StringSplit($iTotal2, ".")
$iMinute = $aTotal2[1]
$iTotal3 = 60 * ("." & $aTotal2[2])
$aTotal3 = StringSplit($iTotal3, ".")
$iSecond = $aTotal3[1]

ConsoleWrite($iDays & " " & $iHour & ":" & $iMinute & ":" & $iSecond & "." & $aTotal3[2] & @CRLF  )

output:

04/17/2014 7:24:57.600878688

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Rather than gambling about how to knead this surprising large value (1.14041730900001e+017 = 114041730900001000) by removing the scientific notation exponent separator and guessing how it could be offset to magically produce a date close to current date, it would be much, really much more serious to ask ourselves where this format comes from and search which precise timestamp it represents.

The OP gave us some insight: he's grabbing his data from an IBM DB2 database. It's unlikely that this represents a julian timestamp even if we arbitrarily shift the decimal point: julianday('now') = 2456758.80446287, quite different. Then datetime(1140417.30900001) = -1590-04-03 19:24:57 and it's difficult to believe that today's applications deal with events from 1590 BC. Other standard DB2 timestamp formats use YMD human-readable strings, modulo variations in MDY, DMY, etc. Obviously offending value is not that form.

 

Notes to dar100111:

It would be enlightnening to see the query used.

Streamline your code using something like this:

Func QueryToArray($p_connection, $p_query)
    Local $l_objQuery = ObjCreate("ADODB.Recordset")
    If @error = 1 Then Return ''
    $l_objQuery.Open($p_query, $p_connection)
    If $l_objQuery.State = 0 Then Return ''

    Dim $l_result[1][$l_objQuery.Fields.Count]
    For $j = 0 To $l_objQuery.Fields.Count - 1
        $l_result[0][$j] = $l_objQuery.Fields($j).Name
    Next

    Local $l_rows = $l_objQuery.GetRows()
    _ArrayConcatenate($l_result, $l_rows)   ; requires the latest beta, else use code below
;~  ReDim $l_result[UBound($l_rows) + 1][UBound($l_result, 2)]
;~  For $i = 1 To UBound($l_result) - 1
;~      For $j = 0 To UBound($l_result, 2) - 1
;~          $l_result[$i][$j] = $l_rows[$i - 1][$j]
;~      Next
;~  Next
    Return $l_result
EndFunc   ;==>QueryToArray

It will be much faster.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...