Funções para manipular dados no SQL Server
Ana Voicu
Data Engineer
Definição
Sintaxe
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 |
Definição
Sintaxe
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 |
Definição
Semelhante a CHARINDEX()
Retorna a posição inicial de um padrão em uma expressão
Sintaxe
PATINDEX ('%pattern%', expression, [location ])
| Coringa | Explicação |
|---|---|
| % | Corresponde a qualquer sequência de qualquer tamanho (inclusive vazia) |
| _ | Corresponde a um único caractere |
| [ ] | Corresponde a qualquer caractere dentro dos colchetes [ ] (por exemplo, [abc] corresponde a a, b ou c) |
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 |
Funções para manipular dados no SQL Server