Oracle: DatePart must be defined...? - oracle

Any ideas as to why the following line complains that DATEPART is not defined as an identifier?
IF DATEPART(v_ExpireTime, year) = 1899 THEN v_ExpireTime := NULL;
It returns:
Error(68,10): PLS-00201: identifier 'DATEPART' must be declared
EDIT: Never mind, I found the answer. I now do:
IF to_number(to_char(v_ExpireTime, 'YY')) = 1899 THEN v_ExpireTime := NULL;

As you've already discovered, DATEPART is a SQL Server function, not an Oracle one. The alternative you've shown, to_number(to_char(v_ExpireTime, 'YY')), will give you a two-digit value though, so if the year part of your date is 1899 it will only return 99, so you won't get a match. You can use the four-digit year date format model YYYY instead of YY.
Another option is to use the extract function:
IF extract(year from v_ExpireTime) = 1899 THEN ...

If I am correct it is because DatePart is not part of the PLSQL Library so it does not know what you are doing. The second method is for PLSQL.

Related

udf to replace the datediff function value error

I am trying to make a udf to replace the datediff function.
reason: the udf will end up in my very (close to 8192 limit) long formula.
I use the "dutch" vba therefor datediff is "datumverschil" (a long word)
I want to replace it with "dv"
but so far my dv udf is not working, and I cannot guess where the error
could be... the error says "error in value"...
this is what I have now:
Public Function dv(eerste As Date, tweede As Date, str As String) As Variant
dv = DateDiff(str, eerste, tweede)
End Function
who sees the error :) ? have a great sunday and thank you :) !
Im not sure about the language you are using, but normally each argument in a function has to go in order. Maybe this is your error?
In your code you have:
Public Function dv(eerste As Date, tweede As Date, str As String) As Variant
As you can probably see here, eerste goes first, tweede goes second, and str goes last.
So to pass the arguments through properly you would have to do:
dv(eerste, tweede, str)
Hope this solves your issue and have a nice day! :)
Edit:
I also noticed your function and your variable are named the same?
Again im not sure what language you are using, but it seems assigning DateDiff to the dv variable, you would be overwriting the function?
Perhaps you meant DateDiff = dv(eerste, tweede, str)

How to format time in SAS

I have a dataset with three columns : Start, Stop and Date
Observations in my Start and Stop are time type.
I have the following two values in my Start and Stop columns:
24:49:00 and 25:16:00
As there are both over 24 hours format.
I would like to convert those two values to the following:
24:49:00 to 00:49:00
and
25:16:00 to 01:16:00
How to do this in both SAS and proc sql ?
Thank you !
Do you need to convert them? Use the TIMEPART() function.
start_day=datepart(start);
start_time=timepart(start);
format start_time tod8.;
Or do you just want to display them that way?
format start stop tod8.;
Start/Stop time-24:00:00 like this:
data _null_;
start='25:16:14't;
point='24:00:00't;
_start=start-point;
put _start;
format _start time8.;
run;
SAS Time and DateTime values use seconds as their fundamental unit.
Thus you can use either modulus arithmetic or TIMEPART function to extract the less than 24 hour part of a > 24 hour time value.
data have;
start = '24:49:00't;
stop = '25:16:00't;
start_remainder = mod(start, '24:00't); * modulus arithmetic;
stop_remainder = mod(stop, '24:00't);
start_timepart = timepart(start); * TIMEPART function;
stop_timepart = timepart(stop);
format start: stop: time10.;
run;
After the computation do not expect start_remainder is less than stop_remainder to be always true.

Date format in VFP

Hi I have this query in VFP however I don't understand the meaning of it
REPLACE ALL varA WITH YEAR(varB)*100+MONTH(varB)
Can anyone explain why YEAR(varB) is multiply by 100?
Not at a PC to check but it looks like command will store yyyymm as a number. To achieve this, the command will store the year *100 (e.g. 202000) + month (e.g. 10 for October) = 202010. VarA stores a combination of date varB's year() and month () functions, both which return numbers.

How to write dates (MM/DD/YY) into a matrix (SAS)

I have following problem:
I need to write a begin and end date into a matrix. Where the matrix contains the yearly quarters (1-4) in the collumns and the rows are the year.
E.g.
Matrix:
Q1 Q2 Q3 Q4
2010
2011
Now the Date 01.01.2010 should be put in the first element and the date 09.20.2011 in the sixed element.
Thanks in advance.
You first have to consider that SAS does not actually have date/time/datetime variables. It just uses numeric variables formatted as date/time/datetime. The actual value being:
days since 1/1/1960 for dates
seconds since 00:00 for times
seconds since 1/1/1960 00:00 for datetimes
SAS does not even distinguish between integer and float numeric types. So a date value can contain a fractional part.
What you do or can do with a SAS numeric variable is completely up to you, and mostly depends on the format you apply. You could mistakenly format a variable containing a date value with a datetime format... or even with a currency format... SAS won't notice or complain.
You also have to consider that SAS does not even actually have matrixes and arrays. It does provide a way to simulate their use to read and write to dataset variables.
That said, SAS does provide a whole lot of formats and informats that allow you to implement date and time manipulation.
Assuming you are coding within a data step, and assuming the "dates" are in dataset numeric variables, then the PUT function can extract the datepart you need to calculate row, column of the matrix element to write to, like so:
DATA table;
ARRAY dm{2,4} dm_r1c1-dm_r1c4 dm_r2c1-dm_r2c4;
beg_row = PUT(beg_date, YEAR4.)-2009;
end_row = PUT(end_date, YEAR4.)-2009;
beg_col = PUT(beg_date, QTR1.);
end_col = PUT(end_date, QTR1.);
dm{beg_row,beg_col} = beg_date;
dm{end_row,end_col} = end_date;
RUN;
... or if you are using a one-dimensional array:
DATA table;
ARRAY da{8} da_1-da_8;
beg_index = 4 * (PUT(beg_date, YEAR4.)-2010) + PUT(beg_date, QTR1.);
end_index = 4 * (PUT(end_date, YEAR4.)-2010) + PUT(end_date, QTR1.);
da{beg_index} = beg_date;
da{end_index} = end_date;
RUN;

Error Type mismatch

Why am I getting this Type Mismatch error?
Code:
Dim data as Date
data = CDate(Format(31, "00") & "/" & Format("1/9/2013", "mm/yyyy"))
When I try this with strings of course it works perfect.
Obs: It works if I use a different day than 31 like 28 for exemple... But Why this error occurs only with the day 31.
It looks like you are trying to get the last day of the month. If that is the case, try this...
Dim data As Date
Dim OriginalDate As Date
OriginalDate = DateSerial(2013, 12, 20)
data = DateSerial(Year(OriginalDate), Month(OriginalDate) + 1, 0)
This code basically gets the first day of the next month and then subtracts one day. The nice thing about using the DateSerial function is that you can give it "invalid" values, For example, if you use Year = 2013 and Month = 13, you will get January 2014.
I found the answer, and it's stupid -.-" Month 9 = Agost = 30 days and not 31... I feel so stupid now ;x
You have to watch out for dates because depending on what region your computer is in (Control Panel / Region and Language) sometimes your date of Format("1/9/2013", "mm/yyyy")) can be interpreted as September 1, 2013 or as January 9, 2013
If you can, use DateSerial to specifically hook in the month/day numbers without relying on the format output.

Resources