parent nodes: SQLapuntes

Cadenas

Funciones de cadena

http://sqlserverpedia.com/wiki/Built-in_Functions_-_String_Functions

Tamaño de cadena

Repetir un carácter

Concatenar

Esto no funciona
Concatenar para que junte un '0' si el código de provincia es menor que 10
por ejemplo: '7' --> '07' pero el código siguiente... no funciona sigue dando '7' sin el cero
CASE WHEN PROVINCIAS.ID_PROVINCIAS < 10 THEN '0' + CAST(PROVINCIAS.ID_PROVINCIAS AS VARCHAR) ELSE PROVINCIAS.ID_PROVINCIAS END AS 'Provincia',
Así sí funciona (Josep Martínez):
El tipus retornat pels dos camins (THEN-ELSE) ha de ser el mateix, per tant, a l’else hauries d’afegir-hi un CAST.
CASE WHEN PROVINCIAS.ID_PROVINCIAS < 10 THEN '0' + CAST(PROVINCIAS.ID_PROVINCIAS AS VARCHAR) ELSE PROVINCIAS.ID_PROVINCIAS END AS 'Provincia',
S’ha de vigilar amb els CASE, sobretot si es vol utilitzar a la clàusula WHERE, perquè pots penalitzar força el rendiment.

Afegir ‘0’ al davant, potser t’interessaria més tenir un camp descripció o codi_visual, ara, això ja depèn de com tinguis l’aplicació.





Escapar un apóstrofe dentro de una cadena

    SELECT 'O' + '''' + 'CONELL'

    O'CONELL

Comparar cadenas

Note that

where string1=string2

will be case INsensitive if your database is set up to NOT be case-sensitive. (You can force the collation if you want a case SENSITIVE comparison in a non-case-sensitive environment).

You can also do:

where string1 LIKE string2

and "string2" can include the wildcard operators:

% (0 or more characters)
_ any single character (i.e. will NOT match an empty string)
[ABC0123] a single character, one of the set "ABC0123"
[0-9] a single digit
[^0-9] a single character which is not a digit

Further Regular Expressions are not currently supported - so you can't do:
[0-9]+[A-Za-z]+
for one or more digits followed by one or more letters

You can also use an ESCAPE character if you need to escape one of the reserved characters:

WHERE MyColumn LIKE '%\_FOO\_%' ESCAPE '\'

In a comparison if one of the objects is NULL then the result will be false. BOTH of these will return no rows:

SELECT	'no rows'
WHERE	NULL = 0

SELECT	'no rows'
WHERE	NULL <> 0
Al final no me funcionaba lo del COLLATE.
He optado por usar REPLACE para quitar acentos y caracteres extraños como los guiones:

Para reemplazar el guión hay que ponerlo entre corchetes:
	SELECT AI.ID_NOTARIO
	FROM AI_NOTARIOS AI, CUSTTABLE AX
	WHERE AX.DATAAREAID = 'CLM' AND AI.DATAAREAID='CLM' AND
--	AX.NAME COLLATE Modern_Spanish_100_CI_AI_WS = AI.NOM_NOTARIO COLLATE Modern_Spanish_100_CI_AI_WS -- NO ME FUNCIONA
	LEFT(AI.NOMBRE_LIMPIO,20) = 
	LEFT( REPLACE( REPLACE( REPLACE( REPLACE(  REPLACE( REPLACE(AX.NAME,'á','a'), 'é', 'e' ) ,'í','i'),'ó','o'), 'ú','u'), '[-]', ' ') ,20) 

COLLATE / COLLATION

Al trabajar con dos esquemas con distinta configuración de /collation/ dará un error como:
Cannot resolve the collation conflict between "Modern_Spanish_CI_AS" and "Modern_Spanish_CI_AI" in the equal to operation.

Para evitarlo, cambiar el sistema de /collation/ en uno de las dos partes de la comparación
LEFT JOIN MicrosoftDynamicsAX.dbo.AncNotaryRateSettleTable IND ON (TI.NUMERO_INDICE COLLATE Modern_Spanish_CI_AS) = IND.NOTARYRATEID 
Cambio el collation del campo de la tabla TI (turnos-índices) para que encaje con la tabla de índices (que es de Axapta)




WEBS DE CONSULTA

http://www.guillesql.es/Articulos/SQLServerFAQ_Intercalacion_Cambiar_Collation.aspx
http://msdn.microsoft.com/es-es/library/ms184391.aspx
http://msdn.microsoft.com/en-us/library/aa174903%28v=sql.80%29.aspx
http://msdn.microsoft.com/es-es/library/ms144260.aspx
http://msdn.microsoft.com/es-es/library/ms143515.aspx
http://msdn.microsoft.com/es-es/library/ms175194.aspx
http://msdn.microsoft.com/en-us/library/ms141038.aspx
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=69527
http://www.sqlserverya.com.ar/temarios/descripcion.php?cod=39&punto=33