Question
· Mar 12, 2019

String to date

I am trying to convert a string to date but can not get it to work I have  function that I would like to take in a date string and covert it to date object

here is the ezample so far can not get it to work any help appreciated 

 

set p="12/03/2019"
 
w $System.SQL.TODATE(p,"YYYY-MM-DD")
 
<ILLEGAL VALUE>todate+32^%qarfunc

if I try this still get the wrong value returned

set p="12/03/2019" 
w $ZDATE(p,3)
1841-01-12
Discussion (6)2
Log in or sign up to continue

plain variables don't really have a type, so it's worth knowing what you want to use your date for (storing as %Date, %PosixTime, %Timestamp,...). If you're starting from a string and want to convert to IRIS' internal date format, use the $zdateh() function and use 4 as the second argument for European date format. See the docs for that function for more info.

Hi Thembelani,

              First we  convert date to horolog.

             set p="12/03/2019"

            set horval=$zdateh(p)  //now we got horolog value is 65350

         After that , we use $zdate function and convert the date format.

          w $zdate(horval,3) // now we got 2019-12-03

Thanks,

Ponnumani G

If you need to reorder a given set of characters (as above, from "12/03/2019" to "2019-03-12") then the $translate(...) function, used in "backward-mode" is your best friend.

set p="12/03/2019"
write $tr("abcd-ef-gh","ef/gh/abcd",p) --> 2019-12-03

Your gain: 50% of speed.

Nota bene

(1) $translate(inputdata, fromChars, toChars) --> outputdata
(2) $translate(toSequence, fromSequence, inputdata) --> outputdata

Where (1) is the Forward-Mode (the regular usage) and (2) the Backward-Mode

$System.SQL.TODATE() returns a %Date (+$Horolog) value.

The second argument to TODATE() describes the input format of the string-date value.  In your example, you passed in a MM/DD/YYYY value but told TODATE the format was YYYY-MM-DD.

set p="12/03/2019"  

write $System.SQL.TODATE(p,"DD/MM/YYYY")

Would have returned a logical %Date value