LLI Wiki — My Digital Knowledge Base

Oracle : dessiner un sapin de Noël en SQL*Plus

Requête SQL amusante qui affiche un sapin d'astérisques avec DECODE, LPAD et ROWNUM.

Une requête Oracle amusante à coller dans SQL*Plus : elle dessine un sapin de Noël en astérisques, grâce à DECODE, SIGN, LPAD/RPAD et ROWNUM.

set heading off;
set pages 5000;
SELECT DECODE(SIGN(FLOOR(maxwidth / 2) - ROWNUM),
              1,
              LPAD(' ', FLOOR(maxwidth / 2) - (ROWNUM - 1)) ||
              RPAD('*', 2 * (ROWNUM - 1) + 1, ' *'),
              LPAD('* * *', FLOOR(maxwidth / 2) + 3))
  FROM all_objects, (SELECT 40 AS maxwidth FROM DUAL)
 WHERE ROWNUM < FLOOR(maxwidth / 2) + 5
union all select '|Merry Christmas!' from dual;

Fonctionnement

Résultat

Sortie SQL*Plus : sapin de Noël en astérisques

Mastodon LLIMastodon SocialFosstodon