在SQL中,CHARINDEX函數用于查找一個字符串在另一個字符串中第一次出現的位置。下面是CHARINDEX函數的一些靈活運用技巧:
SELECT CHARINDEX('search_string', column_name) AS position
FROM table_name;
SELECT SUBSTRING(column_name, CHARINDEX('search_string', column_name) + LEN('search_string'), LEN(column_name)) AS substring
FROM table_name;
SELECT CASE WHEN CHARINDEX('search_string', column_name) > 0 THEN 'Found' ELSE 'Not Found' END AS result
FROM table_name;
SELECT column_name
FROM table_name
WHERE CHARINDEX('search_string', column_name) > 0;
SELECT LEN(column_name) - CHARINDEX('search_string', REVERSE(column_name)) + 1 AS position
FROM table_name;
這些技巧可以幫助你更靈活地使用CHARINDEX函數來實現各種字符串操作和條件判斷。