Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1is4GZ-0005bs-OS for pgsql-general@arkaria.postgresql.org; Thu, 16 Jan 2020 12:28:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1is4GY-0007f1-Gy for pgsql-general@arkaria.postgresql.org; Thu, 16 Jan 2020 12:28:50 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1is4GY-0007eu-42 for pgsql-general@lists.postgresql.org; Thu, 16 Jan 2020 12:28:50 +0000 Received: from mailbackend.panix.com ([166.84.1.89]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1is4GR-00082L-9W for pgsql-general@lists.postgresql.org; Thu, 16 Jan 2020 12:28:48 +0000 Received: from panix5.panix.com (panix5.panix.com [166.84.1.5]) by mailbackend.panix.com (Postfix) with ESMTP id 47z3QK5Q5nz1nwy for ; Thu, 16 Jan 2020 07:28:41 -0500 (EST) Received: by panix5.panix.com (Postfix, from userid 17622) id 47z3QK3sqDzfYh; Thu, 16 Jan 2020 07:28:41 -0500 (EST) Date: Thu, 16 Jan 2020 07:28:41 -0500 From: stan To: pgsql-general@lists.postgresql.org Subject: Can I do this? Message-ID: <20200116122841.GA25728@panix.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Editor: gVim List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk I am trying to create a function to automatically create a reference value when a record is inserted into a table. I want the reference value to consist of the user that is doing the insert, plus a couple of dates, plus a sequence number, where the sequence number will increment every time a given user inserts a record. because this sequence number is user specific, my first thought is not to use a set of sequences for it, but to do this by selecting the maximum sequence number that user has entered in the past. So, I have a function that gets all the data, and concatenates it into a string with the exception of the sequence. For that component, I have the following test select that works. SELECT NULLIF(regexp_replace(report_no, '\D','','g'), '')::numeric AS result FROM expense_report_instance WHERE /* NEW.project_key */ 123 = project_key; But, when I add the requisite MAX clause, I get a syntax error. SELECT MAX(NULLIF(regexp_replace(report_no, '\D','','g'), '')::numeric FROM expense_report_instance WHERE /* NEW.project_key */ 123 = project_key); Is there a way I can make this work? -- "They that would give up essential liberty for temporary safety deserve neither liberty nor safety." -- Benjamin Franklin