I have the following case statement:
, ADMIT_DATE_TIME = CASE
WHEN PE.HSP_ACCOUNT_ID IS NOT NULL THEN CASE
WHEN HA.ACCT_BASECLS_HA_C = 1 THEN A3.IP_ADMIT_DATE_TIME
ELSE HA.ADM_DATE_TIME
END
ELSE PE.EFFECTIVE_DATE_DT
END /* Export in YYYYMMDD HH:MM format FYI - No time component in EFFECTIVE_DATE_DT */
What I am expecting is that when the field PE.HSP_ACCOUNT_ID IS NOT NULL then I should see PE.EFFECTIVE_DATE_DT. However this is not happening. PE.EFFECTIVE_DATE_DT does have a value however the result I am seeing is NULL.
Am I missing something?
Any help is appreciated.
Acording to what you write the statement should be:
, ADMIT_DATE_TIME = CASE
WHEN PE.HSP_ACCOUNT_ID IS NULL THEN CASE
WHEN HA.ACCT_BASECLS_HA_C = 1 THEN A3.IP_ADMIT_DATE_TIME
ELSE HA.ADM_DATE_TIME
END
ELSE PE.EFFECTIVE_DATE_DT
END /* Export in YYYYMMDD HH:MM format FYI - No time component in EFFECTIVE_DATE_DT */
when PE.HSP_ACCOUNT_ID is not null shows PE.EFFECTIVE_DATE_DT
Now the statement is going into the next case, so what it shows to you is
A3.IP_ADMIT_DATE_TIME OR HA.ADM_DATE_TIME
that i think is null.
Related
Is it possible, and if so, how can the following change be achieved?
Given the table:
param_tab
param_id serial
value integer
anothervalue integer
update_date TIMESTAMP
I would like to do something similar to this:
UPDATE param_tab pt
CASE WHEN CONDITION THEN pt.value = 14, pt.anothervalue = 20 END
pt.update_date = someTimestamp;
So update_date is always updated and value and anothervalue only in case of some condition
Use the CASE statement in the correct place:
UPDATE param_tab pt
SET value = CASE WHEN condition THEN 14 ELSE pt.value END,
anothervalue = CASE WHEN condition THEN 20 ELSE pt.anothervalue END,
update_date = someTimestamp;
i have date column in the format yyyyMMdd. i want to check whether the date is valid or not.
in informatica, the function is available as CASE WHEN IS_DATE(TO_CHAR(DT),'YYYYMMDD') = 0 THEN TO_DATE('99991231','YYYYMMDD') ELSE TO_DATE(TO_CHAR(DT),'YYYYMMDD') END AS EFF_DT
as in hive alternative to 'is_date' function is not available, how to achieve the same in hive.
Use regexp:
case when regexp_extract(date_column,'(19|20)[0-9]{2}(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])',0) = ''
then 'not valid date'
else 'valid date'
end
Edit regular expression according to your date requirements.
You can use a macro:
create temporary macro isDate(s string)
case when regexp_extract(s,'(19|20)[0-9]{2}(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])',0) = ''
then false
else true
end;
Then use it in your SQL:
select * from table where isDate(date_col);
For yyyy-MM-dd format you can use cast(date_col as date):
create temporary macro isDate(s string)
case cast(s as date) is not null then true else false end
Assume you date format yyyy-MM-dd i.e 2018-07-20
To get all the invalid
Select required_column_name from table_name where cast(date_column_name as date) is NULL;
To get all the valid
Select required_column_name from table_name where cast(date_column_name as date) is not NULL;
I have a simple CASE statement where in my ELSE block numeric column I want to display as '-'.
But it gives me an error
Arithmetic overflow error converting varchar to data type numeric.
How would I do that?
I want it like this:
SELECT
CASE WHEN ChargeName = 'Premium' THEN CompanyCommissionPercentage ELSE '-' END AS CompanyCommissionPercentage
,CASE WHEN ChargeName = 'Premium' THEN RemitterCommissionPercentage ELSE '-' END AS RemitterCommissionPercentage
,CASE WHEN ChargeName = 'Premium' THEN RemitterCommission ELSE '-'END AS RemitterCommission
,CASE WHEN ChargeName = 'Premium' THEN GrossCommission ELSE '-'END AS GrossCommission
FROM #tmpAccountsPayable
`SELECT
CASE WHEN ChargeName = 'Premium' THEN CAST(CompanyCommissionPercentage as varchar(10)) ELSE CAST('-' as varchar(10)) END AS CompanyCommissionPercentage
FROM #tmpAccountsPayable`
Here's a picture of where in excel you can set formatting to a -
You may be better off passing in a zero instead of the dash and allowing excel formatting to make the 0 a - (assuming of course the output is going into excel)
Notice below in E20 the 0 value is converted to a - when accounting format (comma format) is used.
Also notice how a regular dash is left aligned while the accounting - is indented.
Hi i want to add 01/01/1970 to a column ,datatype of last_hit_time_gmt is bigint ,when i run the below query i am getting data type
last_hit_gmmt
does not match a defined datatype name.
select
distinct STG.OMN_APND_KEY,
STG.last_hit_time_gmt,
IIF(STG.last_hit_time_gmt <>0,ADD_TO_DATE(TO_DATE('01/01/1970', 'DD/MM/YYYY'),'SS',cast(STG.last_hit_time_gmt as DATE ),NULL)
from EDW_STAGE_CDM_SRC.STG_OMNITUREDATA STG
WHERE
UPPER(STG_OMNITUREDATA.EVAR41) IN
('CONS_SUPP: CONSUMER','STORE','PURCHASE') and
STG.OMN_APND_KEY='61855975'
please help me..
The query and data type is incompatible with Teradata.
As stated in the comments you may want to use "CASE" instead of "IFF". The general format is
CASE WHEN *condition* THEN *result_if_true*
ELSE *result_if_false*
END as *ColumnName*
editing based on comment response
So in your query example the case statement can be used like...
select distinct STG.OMN_APND_KEY
,STG.last_hit_time_gmt
,CASE WHEN STG.last_hit_time_gmt = 0 THEN NULL
ELSE DATE '1970-01-01'
END AS YourColName
FROM EDW_STAGE_CDM_SRC.STG_OMNITUREDATA STG
WHERE UPPER(STG_OMNITUREDATA.EVAR41) IN
('CONS_SUPP: CONSUMER','STORE','PURCHASE') and
STG.OMN_APND_KEY='61855975'
Also, if you are merely just trying to update the field STG.last_hit_time_gmt, why not just use two simple UPDATE statements?
UPDATE EDW_STAGE_CDM_SRC.STG_OMNITUREDATA
SET STG.last_hit_time_gmt = DATE '1970-01-01'
WHERE STG.last_hit_time_gmt <> 0
AND UPPER(STG_OMNITUREDATA.EVAR41) IN
('CONS_SUPP: CONSUMER','STORE','PURCHASE')
AND STG.OMN_APND_KEY='61855975';
UPDATE EDW_STAGE_CDM_SRC.STG_OMNITUREDATA
SET STG.last_hit_time_gmt = NULL
WHERE STG.last_hit_time_gmt = 0
AND UPPER(STG_OMNITUREDATA.EVAR41) IN
('CONS_SUPP: CONSUMER','STORE','PURCHASE')
AND STG.OMN_APND_KEY='61855975';
I was trying to make a function to work in db2:
CREATE FUNCTION TO_DATE8(DATE_STRING numeric(8,0))
RETURNS DATE
LANGUAGE SQL
IF DATE_STRING > 0 THEN
// ERROR ->
RETURN DATE ( TO_DATE ( SUBSTR ( DATE_STRING , 1 , 8 ) , 'YYYYMMDD' ) )
ELSE
RETURN DATE ( TO_DATE ( '00000000' , 'YYYYMMDD' ) )
END IF
END
ERROR: DATE IS NOT VALID
What to do?
The form of the procedure required seems to be like this (at least on the iSeries version):
CREATE FUNCTION TO_DATE8(DATE_STRING numeric(8,0))
RETURNS DATE
LANGUAGE SQL
BEGIN
RETURN(CASE WHEN DATE_STRING > 0 THEN DATE(SUBSTR(DATE_STRING, 1, 4) || '-' ||
SUBSTR(DATE_STRING, 5, 2) || '-' ||
SUBSTR(DATE_STRING, 7, 2))
ELSE DATE('0001-01-01')
END);
END
However:
Your procedure is misnamed (reading from a date-8, not to it).
Your DATE_STRING is not a string (or even a char), it's numeric. Please rename it to something that does not include the datatype (dateToConvert works)
You seem to want to return something that is not a valid date (all 0s). I'm returning *loval here, although it's possible it should actually be null.
I didn't put in enough checks for a valid date - this will blow up really easily.
If at all possible, the database should be changed to contain actual dates, not a numeric value. Disk is (relative to programmer/architect headaches) cheap.
You may also find a calendar file helpful, if the 8-digit numeric was one of the included columns.
For the benifit of others, this can be done in one line rather than a function:
CASE WHEN MYDATE = 0 THEN NULL ELSE DATE(INSERT(INSERT(LEFT(CHAR(MYDATE),8),5,0,'-'),8,0,'-')) END
MYDATE was 8 packed in my case.