SQL select distinct substring where like muddleup howto
This will work for SQL Server. If you use something else you need to figure out the corresponding functions to left and charindex. Left could of course be replaced with a substring.
select distinct left(T.Animal, charindex(' ', T.Animal, 1)-1)
from YourTable as T
Result:
-------------------------
BIRD
CAT
DOG
FISH
In MySQL you would use left and locate.
(Code not tested)
select distinct left(T.Animal, locate(' ', T.Animal, 1)-1)
from YourTable as T
How to return distinct domain names from email address values in MySQL?
Just use group by
SELECT SUBSTRING_INDEX(user_email,'@',-1) as domain_name FROM user_email group by domain_name
How to select the first row for each group in MySQL?
rtribaldos mentioned that in younger database versions, window-functions could be used.
Here is a code which worked for me and was as fast as Martin Zwarík's substring_index-solution (in Mariadb 10.5.16):
SELECT group_col, order_col FROM (
SELECT group_col, order_col
, ROW_NUMBER() OVER(PARTITION BY group_col ORDER BY order_col) rnr
FROM some_table
WHERE <some_condition>
) i
WHERE rnr=1;
Related Topics
How to Use Wildcards in "In" MySQL Statement
Adodataset Deleting from Joined Table
Performance Difference Between Primary Key and Unique Clustered Index in SQL Server
Create Unique Index If Not Exists in Postgresql
Which Oracle Table Uses a Sequence
Query to Calculate Average Time Between Successive Events
Timestamp Conversion in Oracle for Yyyy-Mm-Dd Hh:Mm:Ss Format
Create a Trigger to Insert Records from a Table to Another One. Get Inserted Values in a Trigger
Most Efficient Method for Persisting Complex Types with Variable Schemas in Sql
Migrating Oracle Date Columns to Timestamp with Timezone
Oracle SQL Syntax - Check Multiple Columns for Is Not Null
Crosstab Query with Dynamic Columns in SQL Server 2005 Up
How to Set a Default Value for One Column in SQL Based on Another Column
Convert Varchar to Datetime in SQL Which Is Having Millisec
How to List All Stored Procedures in Informix
Group by Week, How to Get Empty Weeks