| I'm glad there's growing competition, but GPT4 still outperforms. Here's a recent question I asked: Could you please provide me with a query for my PostgreSQL table? It consists of three columns: 'id,' 'teaser,' and 'full_text,' all of which are of type 'text.' Most 'full_text' entries are articles around 1000 words in length. I'd like to update the 'teaser' column for each row with the first approximately 200 words from the 'full_text' column. How can I achieve this? Phind: CREATE OR REPLACE FUNCTION get_first_n_words(text, integer)
RETURNS text AS $$
DECLARE
word_count integer := 0;
word_start integer := 1;
word_end integer;
BEGIN
WHILE word_count < \$2 AND word_start <= length(\$1) LOOP
word_end := position(' ' in \$1 from word_start) + 1;
IF word_end = 0 THEN
word_end := length(\$1) + 1;
END IF;
word_count := word_count + 1;
word_start := word_end;
END LOOP;
RETURN substring(\$1 from 1 for word_start - 1);
END;
$$ LANGUAGE plpgsql;
GPT4: UPDATE your_table_name
SET teaser = (
SELECT STRING_AGG(word, ' ')
FROM (
SELECT SPLIT_PART(full_text, ' ', i) AS word
FROM generate_series(1, 200) AS i
) AS words
WHERE word <> ''
)
WHERE full_text IS NOT NULL;
|