I want to omit part of a string/field returned by a query. For example, for a string like 999-43875435
, I'd like to get the 43875435
part of it. But I wouldn't like to get it like SUBSTRING(field, 5, LEN(field)))
. I would like to subtract the part I want to omit, and have it return the rest of the string, something like OMIT(field, '999-', <something that would denote I want it to match the pattern at the beginning of the string>)
. Is there such a function like the one I describe?
CodePudding user response:
Here are som examples that I think you could do:
SELECT SUBSTRING(field, CHARINDEX('-', field) 1, LEN(field))
, RIGHT(field, LEN(field) - (CHARINDEX('999-', field) LEN('999-') - 1))
, REPLACE(field, '999-', '')
CodePudding user response:
If you always want the first 3 characters to be omitted you can concatenate the output with some asterixis
SELECT concat('***', SUBSTRING(phone, 4, 7)) AS Phone FROM table;
Result would look like this
***4587513, ***3215196, ***4163215, ***3213545, ***4873213, ***7321684, ***4684324...