Fonctions pour manipuler les données dans SQL Server
Ana Voicu
Data Engineer
Définition
Syntaxe
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 |
Définition
Syntaxe
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 |
Définition
Semblable à CHARINDEX()
Retourne la position de départ d'un motif dans une expression
Syntaxe
PATINDEX ('%pattern%', expression, [location ])
| Caractère générique | Explication |
|---|---|
| % | Correspond à toute chaîne de toute longueur (y compris longueur zéro) |
| _ | Correspond à un seul caractère |
| [ ] | Correspond à tout caractère entre crochets [ ] (p. ex., [abc] correspond à 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 |
Fonctions pour manipuler les données dans SQL Server