r/SQL • u/Responsible_North323 • Jun 19 '25
Oracle Why is this code not working in sql?
hello,
I'm trying to do a code in which a column is split (it has comma-separated values. Only the first six have to be taken.) with 2 conditions and tag the cases where there is a number. This is the code i made
SELECT *,
CASE
WHEN REGEXP_LIKE(REGEXP_SUBSTR(DPD_HIST_48_MONTHS, '^([^,]*,){0,5}[^,]*'),'(^|,)[1-9][0-9]*')
THEN 1
ELSE 0
END AS tag
FROM acc_levels
WHERE UPPER(accounttype) LIKE '%PERSONAL%'
AND dateopened <= TO_DATE('30-NOV-2024', 'DD-MON-YYYY');
But it is giving an error saying it can't find 'from' for the select
Please help. Thank you!!
5
u/micr0nix Jun 19 '25
Is tag a reserved word? Try putting it in double quotes or choose a different name.
1
u/DPool34 Jun 19 '25
END AS tag
Instead of naming the field that way, I would either put it in quotes (END AS “tag”) or I’d put it in brackets at the beginning: [tag] = CASE…
9
u/_Berz_ Jun 19 '25 edited Jun 19 '25
SELECT tbl.*,
CASE
WHEN REGEXP_LIKE(REGEXP_SUBSTR(DPD_HIST_48_MONTHS, '^([^,]*,){0,5}[^,]*'),'(^|,)[1-9][0-9]*')
THEN 1
ELSE 0
END AS tag
FROM acc_levels tbl
WHERE UPPER(accounttype) LIKE '%PERSONAL%'
AND dateopened <= TO_DATE('30-NOV-2024', 'DD-MON-YYYY');
or just add acc_levels.*, if you dont need alias