Filtrar posições de strings em SQL igual ao quot;copyquot
Olá pessoal, como vão?
Bem, estou precisando filtrar as três últimas posições de um campo como por ex. um código, via SQL. Ex.: Código ´102[b:23162a7d13]000[/b:23162a7d13]´; aqui eu quero filtrar as últimas três posições do código que são ´000´. Existe uma função right, mas não deu certo. Existe uma maneira de fazer isso via SQL?
Se puderem ajudar, sou grato desde já.
PS.: Meu colega programador sênior disse que no Fox ele utilizava a função [b:23162a7d13]right (campo, 3)[/b:23162a7d13]
Bem, estou precisando filtrar as três últimas posições de um campo como por ex. um código, via SQL. Ex.: Código ´102[b:23162a7d13]000[/b:23162a7d13]´; aqui eu quero filtrar as últimas três posições do código que são ´000´. Existe uma função right, mas não deu certo. Existe uma maneira de fazer isso via SQL?
Se puderem ajudar, sou grato desde já.
PS.: Meu colega programador sênior disse que no Fox ele utilizava a função [b:23162a7d13]right (campo, 3)[/b:23162a7d13]
Paulo Trajano
Curtidas 0
Respostas
Motta
16/01/2004
tente
SELECT SUBSTRING(Name FROM 2)
FROM Country
SELECT SUBSTRING(Name FROM 2)
FROM Country
GOSTEI 0
Paulo Trajano
16/01/2004
Ô Motta, deixa ver se eue entendi:
SELECT SUBSTRING([b:821b9fe830][color=blue:821b9fe830]Name FROM 2[/color:821b9fe830][/b:821b9fe830])
FROM Country
Vamos pegar as últimas duas posições de Name ou [b:821b9fe830]a partir [/b:821b9fe830]da segunda posição?
SELECT SUBSTRING([b:821b9fe830][color=blue:821b9fe830]Name FROM 2[/color:821b9fe830][/b:821b9fe830])
FROM Country
Vamos pegar as últimas duas posições de Name ou [b:821b9fe830]a partir [/b:821b9fe830]da segunda posição?
GOSTEI 0
Motta
16/01/2004
Peguei do manual de sql do Paradox, vc não falou o bd.
Creio ser comando de sql-ansi,
segue a página do manual (localsql.hlp)
Extracts a substring from a string.
SUBSTRING(column_reference FROM start_index [FOR length])
Description
Use SUBSTRING to extract a substring from a character value (column, literal, parameter, or caculated values), specified in the column reference.
FROM is the character position at which the extracted substring starts within the original string. The index for FROM is based on the first character in the source value being 1.
FOR is optional, and specifies the length of the extracted substring. If FOR is omitted, the substring goes from the position specified by FROM to the end of the string.
The example below, applied to the literal string ´ABCDE´ returns the value ´BCD´.
SELECT SUBSTRING(´ABCDE´ FROM 2 FOR 3) AS Sub
FROM Country
In the SELECT statement below only the second and subsequent characters of the NAME column are retrieved. For a column value of ´Belgium´ that would be a ´elgium´ result.
SELECT SUBSTRING(Name FROM 2)
FROM Country
When applied to retrieved data of a SELECT statement, the effect is transient and does not affect stored data. When applied to the update atoms of an UPDATE statement, the effect is persistent and permanently converts the case of the stored values.
Applicability
SUBSTRING can only be used with character columns or literals. To use on values of other data types, the values must first be converted to CHAR using the CAST function.
Note:the SUBSTRING function cannot be used with memo or BLOB columns.
Creio ser comando de sql-ansi,
segue a página do manual (localsql.hlp)
Extracts a substring from a string.
SUBSTRING(column_reference FROM start_index [FOR length])
Description
Use SUBSTRING to extract a substring from a character value (column, literal, parameter, or caculated values), specified in the column reference.
FROM is the character position at which the extracted substring starts within the original string. The index for FROM is based on the first character in the source value being 1.
FOR is optional, and specifies the length of the extracted substring. If FOR is omitted, the substring goes from the position specified by FROM to the end of the string.
The example below, applied to the literal string ´ABCDE´ returns the value ´BCD´.
SELECT SUBSTRING(´ABCDE´ FROM 2 FOR 3) AS Sub
FROM Country
In the SELECT statement below only the second and subsequent characters of the NAME column are retrieved. For a column value of ´Belgium´ that would be a ´elgium´ result.
SELECT SUBSTRING(Name FROM 2)
FROM Country
When applied to retrieved data of a SELECT statement, the effect is transient and does not affect stored data. When applied to the update atoms of an UPDATE statement, the effect is persistent and permanently converts the case of the stored values.
Applicability
SUBSTRING can only be used with character columns or literals. To use on values of other data types, the values must first be converted to CHAR using the CAST function.
Note:the SUBSTRING function cannot be used with memo or BLOB columns.
GOSTEI 0
Paulo Trajano
16/01/2004
Peguei do manual de sql do Paradox, vc não falou o bd.
Motta, usamos IB/FB.
Essa linha ajudou muito
SELECT SUBSTRING(´ABCDE´ FROM 2 FOR 3) AS Sub
então fizemos
SELECT SUBSTRING (CODIGOREFERENCIA FROM 5 FOR 3)
mas deu certo nos campos que tinha acima de 8 dígitos. No caso do número 12345678 foi retornado 678, ou seja a partit da 5ª posição. Até aí tudo bem. Mas queremos filtrar as 3 últimas posições independente do tamanho do campo (maior que 3 é claro). Tem alguma idéia?
GOSTEI 0
Paulo Trajano
16/01/2004
Peguei do manual de sql do Paradox, vc não falou o bd.
Motta, usamos IB/FB.
Essa linha ajudou muito
SELECT SUBSTRING(´ABCDE´ FROM 2 FOR 3) AS Sub
então fizemos
SELECT SUBSTRING (CODIGOREFERENCIA FROM 5 FOR 3)
mas deu certo nos campos que tinha acima de 8 dígitos. No caso do número 12345678 foi retornado 678, ou seja a partit da 5ª posição. Até aí tudo bem. Mas queremos filtrar as 3 últimas posições independente do tamanho do campo (maior que 3 é claro). Tem alguma idéia?
GOSTEI 0
Motta
16/01/2004
queremos filtrar as 3 últimas posições independente do tamanho do campo (maior que 3 é claro). Tem alguma idéia?
tente alguma function que retorno o tamanho da string
no oracle é [b]Length[/b] aí vc pega do final - 3
tente alguma function que retorno o tamanho da string
no oracle é [b]Length[/b] aí vc pega do final - 3
GOSTEI 0
Paulo Trajano
16/01/2004
Boa essa (length). No Interbase existe essa função mas quando executamos ele dá o erro justamente na palavra [b:341dc48322]length[/b:341dc48322]. Essa realmente é a função perfeita para pegar o tamanho total do campo e diinui-lo em 3 posições, mas não dá certo.
GOSTEI 0
Motta
16/01/2004
Trajano, tente o forum de ib, daqui pra frente acho que nao posso mais te ajudar muito
GOSTEI 0
Paulo Trajano
16/01/2004
Mesmo assim ajudou muito. Valeu Mottão!!
Abraço! :wink:
Abraço! :wink:
GOSTEI 0