Expand my Community achievements bar.

Join us on September 25th for a must-attend webinar featuring Adobe Experience Maker winner Anish Raul. Discover how leading enterprises are adopting AI into their workflows securely, responsibly, and at scale.

Query Service Dynamic variable column name

Avatar

Level 1

Hello,

 

I'm using the AEP Query Service, and I'm trying to create a column header with a dynamic name that includes today's date, like this: "Header+today's date". However, the query below is generating multiple errors:

 

SET @col_name= SELECT 'HEADER' || TO_CHAR(current_date, 'YYYYMMDD');
-- Eliminar la tabla si ya existe
DROP TABLE IF EXISTS SPSUnsubsample_timestamp;

-- Crear la tabla con la estructura deseada
CREATE TABLE SPSUnsubsample_timestamp AS
WITH data_rows AS (
  SELECT DISTINCT
    get_json_object(to_json(identityMap), '$.Email[0].id') AS user_email
  FROM ajo_email_tracking_experience_event_dataset
  WHERE eventType = 'message.tracking'
    AND get_json_object(to_json(_experience), '$.customerJourneyManagement.messageInteraction.interactionType') = 'click'
    AND LOWER(get_json_object(to_json(_experience), '$.customerJourneyManagement.messageInteraction.label')) LIKE '%unsubscribe%'
    AND get_json_object(to_json(_experience), '$.customerJourneyManagement.messageInteraction.trackingType') IN ('opt_out', 'direct_optout')
),
row_count AS (
  SELECT COUNT(*) AS total FROM data_rows
)
SELECT * FROM (
  -- HEADER primero
  SELECT 'HEADER' || TO_CHAR(current_date, 'YYYYMMDD') AS @col_name
  UNION ALL
  -- Correos electrónicos
  SELECT user_email FROM data_rows
  UNION ALL
  -- TRAILER al final
  SELECT 'TRAILER_' || LPAD(CAST(total AS STRING), 8, '0') FROM row_count
) final_result;

Is it possible to achieve this with the query service? If not, are there any workarounds or suggestions for how I could modify this approach to make it work?

Thank you!

 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Reply

Avatar

Level 2

hi, to work with the date you could try something like this :
substring(CAST(getdate() AS VARCHAR(20)),1,10)

the inner varchar needs a number but ignores it, so the substring works

 

also need string commands so use the concat:

the one below should work:

select concat('HEADER ',substring(CAST(getdate() AS VARCHAR(20)),1,10))

 

looks like you were writing Oracle sql there

 

cheers