Artificial Intelligence
Please fill the required field.

getting error for the below statement in sap datasphere, give me the correct code which should validate in sap datasphere CASE WHEN IS NULL(MNTH_END_STK_VAL_STK) WHERE (CONCAT (MNTH_END_STK_FSCL_YR, MNTH_END_STK_FSCL_MNTH) = (CURRENT_DATE, 'yyyyMM'


The error in your original SQL code stems from incorrect syntax and usage of the `CASE` statement along with the `IS NULL` function. The `WHERE` clause is not valid in a `CASE` statement. Instead, conditions inside a `CASE` statement should be specified using the `WHEN` clause. Here’s the corrected code that should work in SAP Datasphere: ```sql CASE WHEN MNTH_END_STK_VAL_STK IS NULL AND CONCAT(MNTH_END_STK_FSCL_YR, LPAD(MNTH_END_STK_FSCL_MNTH, 2, '0')) = TO_VARCHAR(CURRENT_DATE, 'yyyyMM') THEN 0 ELSE MNTH_END_STK_VAL_STK END ``` ### Key Adjustments Made: 1. **IS NULL**: The `IS NULL` check is moved into the `WHEN` condition properly. 2. **CONCAT() function**: The comparison now combines `MNTH_END_STK_FSCL_YR` and `MNTH_END_STK_FSCL_MNTH`. Note that we ensure monthly values are two digits long with `LPAD`. 3. **TO_VARCHAR**: The `CURRENT_DATE` is appropriately formatted using `TO_VARCHAR(CURRENT_DATE, 'yyyyMM')` to match the string format. This code snippet now checks if `MNTH_END_STK_VAL_STK` is `NULL` for the specific period defined by `MNTH_END_STK_FSCL_YR` and `MNTH_END_STK_FSCL_MNTH`, returning `0` if true, and the value of `MNTH_END_STK_VAL_STK` otherwise.