Re: SQL syntax

From: Ken Tanzer <ken(dot)tanzer(at)gmail(dot)com>
To: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
Cc: Dale Seaburg <kg5lt(at)verizon(dot)net>, PG-General Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: Re: SQL syntax
Date: 2018-03-03 01:01:14
Message-ID: CAD3a31U5_sbFTAEpdDLZQu6pNg4LOhTJ6qf7ap0XSQiDFBVLRg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Mar 2, 2018 at 4:41 PM, Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
wrote:

> On 03/02/2018 04:36 PM, Dale Seaburg wrote:
>
>> My mind is drawing a blank. Not sure where to go to find the answer.
>> Here is the statement in C#:
>>
>> sSQL = "SELECT \"Image_Filename\" FROM \"Instruments\" WHERE ";
>>
>> 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:%';
>
> If you want case insensitive then ILIKE. For more info:
> https://www.postgresql.org/docs/10/static/functions-matching
> .html#FUNCTIONS-LIKE
>
>
It's not clear what kind of test you're trying to apply to those first two
characters. If you want to pull them out to use in an expression (e.g., if
you want to see if they are greater or less than something else), you can
use SUBSTRING or LEFT.

SELECT SUBSTRING('abc' FROM 1 FOR 2),LEFT('abc',2),LEFT('abc',2)<'aa'
AS is_less,LEFT('abc',2)>'aa' AS is_more;

substring | left | is_less | is_more
-----------+------+---------+---------
ab | ab | f | t
(1 row)

Cheers,
Ken

> --
>
AGENCY Software
A Free Software data system
By and for non-profits
*http://agency-software.org/ <http://agency-software.org/>*
*https://demo.agency-software.org/client
<https://demo.agency-software.org/client>*
ken(dot)tanzer(at)agency-software(dot)org
(253) 245-3801

Subscribe to the mailing list
<agency-general-request(at)lists(dot)sourceforge(dot)net?body=subscribe> to
learn more about AGENCY or
follow the discussion.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2018-03-03 01:07:47 Re: SQL syntax
Previous Message Adrian Klaver 2018-03-03 00:41:35 Re: SQL syntax