What is best way to get last indexof character in SQL 2008
A little tricky, but you could do something like:
REVERSE(SUBSTRING(REVERSE([field]),0,CHARINDEX('[char]',REVERSE([field]))))
Is there a LastIndexOf in SQL Server?
If you want everything after the last _
, then use:
select right(db_name(), charindex('_', reverse(db_name()) + '_') - 1)
If you want everything before, then use left()
:
select left(db_name(), len(db_name()) - charindex('_', reverse(db_name()) + '_'))
Find index of last occurrence of a sub-string using T-SQL
You are limited to small list of functions for text data type.
All I can suggest is start with PATINDEX
, but work backwards from DATALENGTH-1, DATALENGTH-2, DATALENGTH-3
etc until you get a result or end up at zero (DATALENGTH-DATALENGTH)
This really is something that SQL Server 2000
simply can't handle.
Edit for other answers : REVERSE is not on the list of functions that can be used with text data in SQL Server 2000
Using LastIndexOf and SubString in mssql
SELECT Id,
RIGHT(Code, CHARINDEX('.', REVERSE('.' + Code)) - 1) AS [Result],
Code
FROM YourTable
Find last occurence of a character within a string
For those people who love monster 1-liners.
For MS2012 - Using IIF
:
SELECT IIF(CHARINDEX('/', REVERSE(FieldA)) = 1,REVERSE(SUBSTRING(REVERSE(FieldA),2,CHARINDEX('/', SUBSTRING(REVERSE(FieldA), 2, LEN(FieldA))) - 1)),REVERSE(SUBSTRING(REVERSE(FieldA),1,CHARINDEX('/', REVERSE(FieldA)) - 1)))
FROM (
SELECT '/index.cfm/fuseaction/content.page/nodeID/18156F6E-88E3-42FC-BDCB-DA6CBAD14EFD/' AS FieldA
UNION ALL
SELECT '/index.cfm/fuseaction/content.page/nodeID/18156F6E-88E3-42FC-BDCB-DA6CBAD14EFD'
UNION ALL
SELECT NULL
) TableA
For MS2008 Here is the same code in a CASE
SELECT A.FieldA
, CASE
WHEN CHARINDEX('/', REVERSE(FieldA)) = 1 THEN REVERSE(SUBSTRING(REVERSE(FieldA),2,CHARINDEX('/', SUBSTRING(REVERSE(FieldA), 2, LEN(FieldA))) - 1))
ELSE REVERSE(SUBSTRING(REVERSE(FieldA),1,CHARINDEX('/', REVERSE(FieldA)) - 1))
END
FROM (
SELECT '/index.cfm/fuseaction/content.page/nodeID/18156F6E-88E3-42FC-BDCB-DA6CBAD14EFD/' AS FieldA
UNION ALL
SELECT '/index.cfm/fuseaction/content.page/nodeID/18156F6E-88E3-42FC-BDCB-DA6CBAD14EFD'
) a
How do I get the index from the last occurence of a character in a string sql?
You can;
;with T(f) as (
select '' union
select 'xxx' union
select 'xxx (yyy)' union
select 'xxx (yyy) (zzz)'
)
select
right(f, patindex('%(%', reverse(f) + '('))
from T
>>
(No column name)
xxx
(yyy)
(zzz)
Remove the +'(' to get blanks for no parens.
IndexOf function in T-SQL
CHARINDEX is what you are looking for
select CHARINDEX('@', 'someone@somewhere.com')
-----------
8
(1 row(s) affected)
-or-
select CHARINDEX('c', 'abcde')
-----------
3
(1 row(s) affected)
Getting rid of characters in a string
You can do :
select col, replace(col, right(col, charindex('/', reverse(col) + '/' ) -1), '')
from table t;
This assumes Folders
always contains filenames.
Fastest way to find string by substring in SQL?
If you want to use less space than Randy's answer and there is considerable repetition in your data, you can create an N-Ary tree data structure where each edge is the next character and hang each string and trailing substring in your data on it.
You number the nodes in depth first order. Then you can create a table with up to 255 rows for each of your records, with the Id of your record, and the node id in your tree that matches the string or trailing substring. Then when you do a search, you find the node id that represents the string you are searching for (and all trailing substrings) and do a range search.
Related Topics
How to Check Existence of User-Define Table Type in SQL Server 2008
How to Create a Closure Table Using Data from an Adjacency List
Query Across Multiple Databases on Same Server
SQL Select Max(Date) and Corresponding Value
How to Perform a Bitwise Group Function
SQL Server Bulk Insert CSV with Data Having Comma
How to Pass a Comma Separated List to a Stored Procedure
Why Should I Capitalize My SQL Keywords
Delete with "Join" in Oracle SQL Query
Guid Primary /Foreign Key Dilemma SQL Server
How to Catch a Query Exception in Laravel to See If It Fails
"Merge" Style Operation with Literal Values
How to Escape Ampersand in Toad
How to Use Non-Aggregate Columns with Group By
What Is the Purpose of Putting an 'N' in Front of Function Parameters in Tsql