[Solved] SQL Query Comparing Strings

Creating tables and queries
Post Reply
JohnNorris
Posts: 3
Joined: Thu May 07, 2020 1:36 am

[Solved] SQL Query Comparing Strings

Post by JohnNorris »

Hello,

A novice question on SQL in Base:

I would like to compare two strings in the same row and report the result. I think this is equivalent to "StrComp", but I am unable to get that to work.

For example,

Table
A B C
1 Bob Ivan
2 Jane Bob
3 Jose Jose

I'd like a report that somehow shows A3 has matching data in fields B and C, by flagging the row somehow. Something like?

SELECT A, StrComp("B","C")
FOR Table

I've been trying things and looking up ideas for awhile and finally admit I am stumped. I can make a report of all rows that have the matching data, but want a report with all rows with and without matches.

Thank you for your time,

John
Last edited by JohnNorris on Fri May 08, 2020 6:33 am, edited 2 times in total.
Open Office 4.1.7 on Windows 10
FJCC
Moderator
Posts: 9280
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: SQL Query Comparing Strings

Post by FJCC »

Will this meet your needs?

Code: Select all

SELECT "A", "B", "C"
FROM "Table_2"
WHERE "B" = "C"
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
robleyd
Moderator
Posts: 5087
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: SQL Query Comparing Strings

Post by robleyd »

I think OP wants an extra flag to indicate whether the row has a duplicate or not. My SQL is a bit rusty, but something along the lines of

Code: Select all

SELECT "A", IF ("B"="C",'Duplicate','Unique')  FROM table ....
If Hsql doesn't support IF perhaps CASE might work.
Cheers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.2.2; SlackBuild for 24.2.2 by Eric Hameleers
FJCC
Moderator
Posts: 9280
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: SQL Query Comparing Strings

Post by FJCC »

robleyd wrote:I think OP wants an extra flag to indicate whether the row has a duplicate or not. My SQL is a bit rusty, but something along the lines of

Code: Select all

SELECT "A", IF ("B"="C",'Duplicate','Unique')  FROM table ....
If Hsql doesn't support IF perhaps CASE might work.
I think that would be

Code: Select all

SELECT "A", CASE WHEN "B" = "C" THEN 'Duplicate' ELSE 'Unique' END AS "Flag"
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
JohnNorris
Posts: 3
Joined: Thu May 07, 2020 1:36 am

Re: SQL Query Comparing Strings

Post by JohnNorris »

FJCC wrote:Will this meet your needs?

Code: Select all

SELECT "A", "B", "C"
FROM "Table_2"
WHERE "B" = "C"
Thank you for the quick reply. That confirmed that I can compare text strings with the equal sign. I wasn't sure about that. I was thinking I would have to use "like" or something. Very helpful.
Open Office 4.1.7 on Windows 10
JohnNorris
Posts: 3
Joined: Thu May 07, 2020 1:36 am

Re: SQL Query Comparing Strings

Post by JohnNorris »

I think that would be

Code: Select all

SELECT "A", CASE WHEN "B" = "C" THEN 'Duplicate' ELSE 'Unique' END AS "Flag"
[/quote]

Thanks to you both. The above is pretty much what I wanted. I've confirmed that it works with my simple table.

The "CASE" function is not something I've explored before. An If/Then function is very powerful; so many thanks from me.

John
Open Office 4.1.7 on Windows 10
User avatar
robleyd
Moderator
Posts: 5087
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: [SOLVED] SQL Query Comparing Strings

Post by robleyd »

I assume you are using the embedded hsql V 1.8; if so you might find the hsql documentation for 1.8 a useful resource, particularly Section 9.
Cheers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.2.2; SlackBuild for 24.2.2 by Eric Hameleers
Post Reply