Einzelnen Beitrag anzeigen

omata

Registriert seit: 26. Aug 2004
Ort: Nebel auf Amrum
3.154 Beiträge
 
Delphi 7 Enterprise
 
#13

Re: einzelnes Wort in SQL ändern

  Alt 10. Feb 2007, 16:59
So ein neuer Versuch.

In SQL bzw. einer Function auf dem DB-Server könnte das so aussehen...
SQL-Code:
CREATE FUNCTION func_myreplace (Source VARCHAR(1000),
                                NewWord VARCHAR(20),
                                WordIndex INT)
  RETURNS VARCHAR(1000)
BEGIN
  DECLARE i INT DEFAULT 0;
  DECLARE p INT DEFAULT 0;
  DECLARE g INT DEFAULT 0;
  DECLARE xSource VARCHAR(1000) DEFAULT '';
  IF WordIndex = 0 THEN
    SET xSource = CONCAT(NewWord, ' ', Source);
  END IF;
  WHILE p <= LENGTH(Source) AND WordIndex > 0 DO
    SET p = INSTR(Source, ' ');
    IF p = 0 THEN
      SET p = LENGTH(Source);
    END IF;
    WHILE (p < LENGTH(Source)) AND SUBSTRING(Source, p+1, 1) = ' ' DO
      SET p = p + 1;
    END WHILE;
    SET i = i + 1;
    IF WordIndex = i THEN
      SET xSource = CONCAT(xSource, NewWord);
      SET g = 1;
    ELSE
      SET xSource = CONCAT(xSource, SUBSTRING(Source, 1, p));
      SET p = p + 1;
    END IF;
    SET Source = SUBSTRING(Source, p, LENGTH(Source));
  END WHILE;
  IF WordIndex > 0 THEN
    IF WordIndex <> i+1 THEN
      SET xSource = CONCAT(xSource, Source);
    END IF;
    IF g = 0 THEN
      SET xSource = CONCAT(xSource, ' ', newWord);
    END IF;
  END IF;
  RETURN xSource;
END
Und der Aufruf...
SQL-Code:
UPDATE tabelle
SET spalte = func_myreplace(spalte, 'Neues Wort', 43)
WHERE ...
WordIndex = 0: Neues Wort wird vorne angehängt
WordIndex > Anzahl Wörter: Neues Wort wird hinten angehängt
0 < WordIndex <= Anzahl Wörter: Das n'te Wort wird durch das neue Wort ersetzt

Gruss
Thorsten
  Mit Zitat antworten Zitat