Functions for Manipulating Data in SQL Server
Ana Voicu
Data Engineer
Definition
Syntax
LEN(character_expression)
SELECT LEN('Do you know the length of this sentence?') AS length
|length|
|------|
|40 |
SELECT DISTINCT TOP 5
bean_origin,
LEN(bean_origin) AS length
FROM ratings;
|bean_origin |length|
|------------------------|------|
|Toscano Black |13 |
|Trinite |7 |
|Ocumare- Puerto Cabello |23 |
|Maracaibo- El Rosario |21 |
|Madagascar |10 |
Definition
Syntax
CHARINDEX (expression_to_find, expression_to_search [, start_location])
SELECT
CHARINDEX('chocolate', 'White chocolate is not real chocolate'),
CHARINDEX('chocolate', 'White chocolate is not real chocolate',10),
CHARINDEX('chocolates', 'White chocolate is not real chocolate');
|position beginning|position in string|position of non-existing exp|
|------------------|------------------|----------------------------|
|7 |29 |0 |
Definition
Similar to CHARINDEX()
Returns the starting position of a pattern in an expression
Syntax
PATINDEX ('%pattern%', expression, [location ])
Wildcard | Explanation |
---|---|
% | Match any string of any length (including zero length) |
_ | Match on a single character |
[ ] | Match on any character in the [ ] brackets (for example, [abc] would match on a, b, or c characters) |
SELECT
PATINDEX('%chocolate%', 'White chocolate is not real chocolate') AS position1,
PATINDEX('%ch_c%', 'White chocolate is not real chocolate') AS position2;
|position1|position2|
|---------|---------|
|7 |7 |
Functions for Manipulating Data in SQL Server