Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1erveW-0000ep-C0 for pgsql-general@arkaria.postgresql.org; Sat, 03 Mar 2018 01:07:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1erveU-0001Jj-MO for pgsql-general@arkaria.postgresql.org; Sat, 03 Mar 2018 01:07:54 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1erveU-0001JZ-EN for pgsql-general@lists.postgresql.org; Sat, 03 Mar 2018 01:07:54 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1erveR-0001gZ-4G for pgsql-general@postgresql.org; Sat, 03 Mar 2018 01:07:53 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w2317llV015233; Fri, 2 Mar 2018 20:07:47 -0500 From: Tom Lane To: Adrian Klaver cc: Dale Seaburg , pgsql-general@postgresql.org Subject: Re: SQL syntax In-reply-to: <8be3c025-ebf8-59f4-1ab8-22206947e078@aklaver.com> References: <8be3c025-ebf8-59f4-1ab8-22206947e078@aklaver.com> Comments: In-reply-to Adrian Klaver message dated "Fri, 02 Mar 2018 16:41:35 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <15231.1520039267.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Fri, 02 Mar 2018 20:07:47 -0500 Message-ID: <15232.1520039267@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Adrian Klaver writes: > On 03/02/2018 04:36 PM, Dale Seaburg wrote: >> To finish off the WHERE clause, I need to look at the first 2 letters, >> like "D:".  My question is how do I specify in the WHERE clause, to look >> at the first 2 characters in the Image_Filename column?  What is the >> correct SQL syntax for looking at just a portion of a column? > SELECT "Image_Filename" FROM "Instruments" WHERE "ImageFilename" LIKE 'D:%'; Another way is to use the substring() function: SELECT "Image_Filename" FROM "Instruments" WHERE substring("ImageFilename", 1, 2) = 'D:'; or if you want to use the SQL committee's COBOLish syntax: SELECT "Image_Filename" FROM "Instruments" WHERE substring("ImageFilename" FROM 1 FOR 2) = 'D:'; Depending on what you're doing, either the pattern-match way or the substring way might be more convenient. The performance implications are different too, though that won't matter to you unless you're dealing with so much data that you want to create a specialized index to make queries of this form faster. regards, tom lane