Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UBR0O-00073b-Sx for pgsql-sql@arkaria.postgresql.org; Fri, 01 Mar 2013 14:32:13 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UBR0O-0005Kg-8h for pgsql-sql@arkaria.postgresql.org; Fri, 01 Mar 2013 14:32:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UBR0N-0005Kb-MV for pgsql-sql@postgresql.org; Fri, 01 Mar 2013 14:32:11 +0000 Received: from archon2012.plus.com ([80.229.41.160] helo=mail.archonet.com) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UBR0L-00084Z-Aj for pgsql-sql@postgresql.org; Fri, 01 Mar 2013 14:32:10 +0000 Received: from [192.168.1.13] (ariel.office.archonet.com [192.168.1.13]) by mail.archonet.com (Postfix) with ESMTP id 2BA0A2C10D9; Fri, 1 Mar 2013 14:32:07 +0000 (GMT) Message-ID: <5130BBE7.3030201@archonet.com> Date: Fri, 01 Mar 2013 14:32:07 +0000 From: Richard Huxton User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.12) Gecko/20130116 Icedove/10.0.12 MIME-Version: 1.0 To: Marko Rihtar CC: pgsql-sql@postgresql.org Subject: Re: Concatenating bytea types... References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org On 28/02/13 10:21, Marko Rihtar wrote: > Hi all, > > i have a little problem. > I'm trying to rewrite one procedure from mysql that involves bytes > concatenation. > This is my snippet from postgres code: You seem to be mixing up escape and hex literal formatting along with decode(). The following should help. BEGIN; CREATE FUNCTION to_hex_pair(int) RETURNS text AS $$ SELECT right('0' || to_hex($1), 2); $$ LANGUAGE sql; CREATE FUNCTION f_concat_bytea1(bytea, bytea) RETURNS text AS $$ BEGIN RETURN $1 || $2; END; $$ LANGUAGE plpgsql; CREATE FUNCTION f_concat_bytea2(bytea, bytea) RETURNS bytea AS $$ DECLARE cv1 bytea; BEGIN cv1 := '\x01'::bytea; RETURN $1 || cv1 || $2; END; $$ LANGUAGE plpgsql; CREATE FUNCTION f_concat_bytea3(text, text) RETURNS bytea AS $$ DECLARE cv1 text; BEGIN cv1 := '01'; RETURN decode($1 || cv1 || $2, 'hex'); END; $$ LANGUAGE plpgsql; SELECT '\x000b'::bytea AS want_this, decode('000b','hex') AS or_this1, decode('\000\013','escape') AS or_this2; SELECT f_concat_bytea1('\x00', '\x0b'); SELECT f_concat_bytea2('\x00', '\x0b'); SELECT f_concat_bytea3('00', '0b'); ROLLBACK; -- Richard Huxton Archonet Ltd -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql