简体   繁体   English

什么 T-SQL 查询会检索 Email 列的所有值,其中 First_Name 和 Last_ Name 列的值位于 Email 列中?

[英]What T-SQL query would retrieve all values of the Email column where values of First_Name and Last_ Name columns are in the Email column?

There's a table with 3 columns, 'First_Name', 'Last_Name', 'Email'.有一个包含 3 列的表,“First_Name”、“Last_Name”、“Email”。 Some values in Email column contain First_Name and Last_Name column values concatenated together. Email 列中的某些值包含串联在一起的 First_Name 和 Last_Name 列值。 Ie, Email = 'JohnSmith22@example.com' where First_Name = 'John', Last_Name = 'Smith'.即,电子邮件 = 'JohnSmith22@example.com',其中 First_Name = 'John',Last_Name = 'Smith'。

Other Email values for some rows are unrelated.某些行的其他电子邮件值不相关。 Ie First_Name = 'Steven', Last_Name = 'Paul', Email = 'random12345@example2.com'.即名字 = 'Steven',姓氏 = 'Paul',电子邮件 = 'random12345@example2.com'。 What query would retrieve only values of Email column, where First_Name and Last_Name are in the column?什么查询只会检索 Email 列的值,其中 First_Name 和 Last_Name 在列中?

You can check if the email starts with the concatenation of the first and last names, using like :您可以使用like来检查email是否以名字和姓氏的串联开头:

select t.*
from mytable t
where email like first_name + last_name + '%'

In case of the name field have a heterogeneous character case , you need to convert it to lower and check.如果 name 字段具有异类字符大小写,则需要将其转换为lower 并检查。 The other thing is sometimes the order of First and Last name in the email might be different so you need to consider this case as well.另一件事是有时电子邮件中名字和姓氏的顺序可能不同,因此您也需要考虑这种情况。

SELECT *
FROM theTable
WHERE lower(email) LIKE '%' + lower(name) + '%' + LOWER(lastname) + '%'
    OR lower(email) LIKE '%' + lower(lastname) + '%' + LOWER(name) + '%'

What query would retrieve only values of Email column, where First_Name and Last_Name are in the column?什么查询只会检索 Email 列的值,其中 First_Name 和 Last_Name 在列中?

If you want first and last name in the column -- but other characters as well -- I would suggest:如果您想要列中的名字和姓氏 - 但也需要其他字符 - 我建议:

where email like '%' + first_name + '%@%' and
      email like '%' + last_name + '%@%'

This checks that the first and last names are before the @ , in any order -- and with any other characters.这会检查名字和姓氏是否在@之前,以任何顺序 - 以及任何其他字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当 first_name 列为空时,sql 查询将值从 second_names 列移动到 firstn_name 列 - sql query to move values from second_names column to firstn_name column when first_name column is null 通过First_Name和/或Last_Name查询sql表 - query sql table By First_Name and/or Last_Name 将 first_name 和 last_name 列中的名称截断为 1 个字符的最有效方法是什么? - What's the most efficient way of truncating the names in a first_name and last_name column to 1 character? 查询以使用外键获取行值(first_name 和 last_name) - Query to fetch row values (first_name & last_name) using foreign key SQL查询以查找第一列中的值相同的两列中的重复项 - Sql Query to find duplicates in 2 columns where the values in first column are same 使用 Reg_exp 从电子邮件预言机中提取名字和姓氏 - Extracting First_name & Last_name from email oracle using Reg_exp 如何在 pl SQL 中将列值设置为列名,但我不知道列值是什么? - How to set column values as column name in pl SQL but I don't know what what will be the column values? T-SQL插入列名作为第一行 - T-SQL Insert Column Name as First Row 带有列名和第一行值的 Sql 映射 - Sql mapping with column name and first row values 在查询结果中将重复列值显示为空格,但t-sql中的第一行除外 - presenting repeating column values as blanks in query results except for the very first row in t-sql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM