How to format CASE date format - case

is my date format incorrect?
CASE WHEN ({custentity92} TO_DATE(‘1.1.16’, 'MM.DD.YYYY')) < 0
THEN ({jobprice}-{custentity_proj_unbilled}+{custentity94})
END

Related

SAS date formatting hours / minutes / seconds

Because my time data has some messy characters in it ( *, #, char, etc) I'm inputting the data in best.32 format and then using compress to remove the irrelevant char - time_1 = compress(Tim_original,'*#','l');
However, my data takes the form of mm:ss and hh:mm:ss and for some reason when I use time_1=input(time_1,time8.) to convert from the string to a num, it makes my mm into hours...! How do I covert my string to time/minutes and not have the minutes turned into hours with ":00" added at the end?
If your text only has one : then the informat TIME will take that to mean hh:mm and not mm:ss. You could just test your string and divide the result of the INPUT() function call by 60 to convert it.
data test;
input #1 timestr $8. ;
time1=input(timestr,time8.);
time2=input(timestr,time8.);
if countw(timestr,':') < 3 then time2=time2/60 ;
format time1 time2 time8.;
cards;
12:34
0:12:34
;

How to proper cast in sqllite

When i cast datetime in SQLLite, it truncates the string.
for example
select cast("2017-04-23 9:12:08 PM" as datetime) as dt
returns
2017
SQLite's CAST can only cast to the defined storage classes and can therefore only be used to cast to
NONE (blob), TEXT, REAL, INTEGER or NUMERIC.
However the normal rules for determing column-affinity are applied to the type so by coding CAST(value AS datetime) you are effectively using CAST(value AS NONE) (i.e. a BLOB).
CAST expressions
Therefore you can't effectively use CAST. However you simply use the DateTime functions against an appropriate value (accepted formats) as per Date And Time Functions e.g. :-
SELECT datetime("2017-04-23 09:12:08") as dt;
results in
2017-04-23 09:12:08
or to show date manipulation
select date(dt), dt FROM (
select datetime("2017-04-23 09:12:08") as dt
);
results in
2017-04-23
and
2017-04-23 09:12:08
However considering that your format isn't one of the accepted formats you could convert the value. This is more complex but it can be done. Here's an example that will perform the conversion (not substantially tested though) :-
SELECT
CASE WHEN (CAST(hour AS INTEGER) + CAST(adjustment AS INTEGER)) > 9 THEN
datepart||' '||CAST(CAST(hour AS INTEGER) + CAST(adjustment AS INTEGER) AS TEXT)||':'||mins_and_secs
ELSE
datepart||' 0'||CAST(CAST(hour AS INTEGER) + CAST(adjustment AS INTEGER) AS TEXT)||':'||mins_and_secs
END AS converted
FROM (
SELECT substr(ts,1,10) as datepart,
CASE WHEN instr(ts,"PM") THEN 12 ELSE 0 END AS adjustment,
CASE WHEN length(ts) = 21 THEN substr(ts,12,1) ELSE substr(ts,12,2) END AS hour,
CASE WHEN length(ts) = 21 THEN substr(ts,14,5) ELSE substr(ts,15,5) END AS mins_and_secs
FROM (
select("2017-04-23 9:12:08 PM") as ts
)
);
This would result in 2017-04-23 21:12:08.
Using select("2017-04-23 9:12:08 AM") results in 2017-04-23 09:12:08
Using select("2017-04-23 11:12:08 PM") results in 2017-04-23 23:12:08
Using select("2017-04-23 11:12:08 AM") results in 2017-04-23 11:12:08
The closest I could come up with is:
select date(datetime(strftime('%s','2017-04-23 09:12:08'), 'unixepoch'))
Result:
2017-04-23
The dateformat you have is not recognised by SQLite:
"2017-04-23 9:12:08 PM"
It does not conform to the Time string formats recognised:
A time string can be in any of the following formats:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
Date And Time Functions

Converting a datetime string (3/24/2017 10:00:00 PM) to (3-24-2017 22:00:00) hive i.e convert from 12 hour to 24 hour format

I have a datetime field in a hive table which is of data type string.
It looks as below:
datetime 3/24/2017 10:00:00 PM
Tried to convert it to the right format desired by hive and also tried removing the AM/PM to a 24 hour format but to no avail.
select from_unixtime(unix_timestamp(datetime,'mm-dd-yyyy HH:MM:SS')) from test_table
You can achieve this using below command:
select from_unixtime(unix_timestamp(datetime,'MM/dd/yyyy hh:mm:ss aa'),'MM-dd-yyyy HH:mm:ss') from test_table;
The format is MM-dd-yyyy HH:mm:ss aa
select from_unixtime(unix_timestamp(datetime,'MM-dd-yyyy HH:mm:ss aa')) from test_table;
With 'MM/dd/yyyy hh:mm:ss aa' does not work. You can archive this with:
FROM_UNIXTIME
(
(
CASE WHEN
datetime LIKE '%AM%'
THEN
UNIX_TIMESTAMP(datetime,'MM/dd/yyyy HH:mm:ss aa')
ELSE
(UNIX_TIMESTAMP(datetime,'MM/dd/yyyy HH:mm:ssaa') + (12 * 3600))
END
)
,'MM-dd-yyyy HH:mm:ss'
)

Teradata min between two dates

Hi i am relatively new to teradata. I have a row with 2 dates. I need to get the min between these tow dates and show it as third date. The dates are in YYYYMMDD format. Here are all the posiblities
example table
col1--date1--date2
123--20140802--20140619
124--20140802--0
124--0--20140802
125--0--0
I need my result set to be
col1--date1--date2--min_date
123--20140802--20140619--6/19/2014
124--20140802--0--8/2/2014(non zero will be min_date)
124--0--20140802--8/2/2014(non zero will be min_date)
125--0--0--?
This is what i could come up with
select col1, date1, date2,
case
when date1 <> 0 and date2 = 0 then cast((date1 - 19000000) as date)
when date1 = 0 and date2 <> 0 then cast((date2 - 19000000) as date)
when date1 = 0 and date2 = 0 then cast(null as date)
when date1 > date2
then
cast((date2 - 19000000) as date)
else
cast((date1 - 19000000) as date)
end as min_date
This does give me result, but query is slow. I wanted to know if there was better and efficient way of doing this, please let me know. I also need to show the min_date in another format as below
col1--date1--date2--min_date--min_month
123--20140802--20140619--6/19/2014--Jun, 2014
124--20140802--0--8/2/2014--Aug, 2014
124--0--20140802--8/2/2014--Aug, 2014
125--0--0--null--null
For min_month i know i can use this if i know which of the dates to use
cast(cast((date2 or date2 - 19000000) as date format 'MMM') as Char(3))
|| ', ' ||
cast(cast((date2 or date2 - 19000000) as date format 'YYYY') as Char(4))
Any help is highly appreciated.
Thanks in advance..
The CASE can be simplified to
CAST(CASE
WHEN date1 = 0 AND date2 = 0 THEN NULL
WHEN date1 > date2 THEN date1
ELSE date2
END - 19000000 AS DATE) AS min_date
Formatting the date:
min_date (FORMAT 'mmm,byyyy') (CHAR(9))

SAS convert string into datetime

I have a column in a table of string representing datetime like "01-Oct-2012 12:23:43.324" how can I cast this in a SAS datetime ?
The DATETIME informat will read that string
new_var=input(datestring,datetime24.);
format new_var datetime24.;
I don't think there is a single informat which will read that format of datetime... so split it into the date & time components then use the dhms function to create a datetime value.
data have ;
datestring = "01-Oct-2012 12:23:43.324" ;
run ;
data want ;
set have ;
dt = input(scan(datestring,1,' '),??date11.) ;
tm = input(scan(datestring,2,' '),??time14.) ;
dttm = dhms(dt,0,0,tm) ;
format dt date9. tm time14.3 dttm datetime24.3 ;
run ;

Resources