如何修改查询,为结尾是特定字符(如F1)的结果追加指定字符(如HR)?
Solution to Append "HR" to Results Ending with "F1"
Hey there! Let's work through this problem step by step. You need to tweak your query so any result ending with "F1" gets "HR" added to the end, while leaving other results exactly as they are. Here's how to pull this off in most common SQL dialects:
Modified Query Example
Assuming your original query selects a column (let's call it target_column) from a table named your_table, here's the adjusted version:
SELECT CASE WHEN RIGHT(target_column, 2) = 'F1' THEN CONCAT(target_column, 'HR') ELSE target_column END AS modified_result FROM your_table;
Breakdown of the Logic
RIGHT(target_column, 2): This grabs the last 2 characters of the value intarget_columnto check if it ends with "F1".CASE WHEN ... THEN ... ELSE ... END: This conditional checks the ending characters. If they match "F1", it usesCONCATto append "HR"; otherwise, it returns the original value untouched.AS modified_result: Renames the output column to make it clear which values were adjusted (feel free to change this to a name that fits your use case).
Example Scenario
Current Query Results:
- ABC123
- XYZF1
- DEF456
- GHIJF1
Expected Results:
- ABC123
- XYZF1HR
- DEF456
- GHIJF1HR
If your original query includes more columns or complex filtering, you can just drop this CASE statement right into your select clause wherever the target column is being pulled.
内容的提问来源于stack exchange,提问作者Chris Kiriakidis
相关产品推荐
相关产品推荐

