Aniversariantes do mês? pesquisa SQL

Delphi

02/08/2003

Gostaria de escolher um mês, em um combobox por exemplo, e relacionar todas as pessoas que fazem aniversário neste mês.
OBS: minha tabela é paradox, e o campo DATANASCIMENTO=ALPHA, não é DATE.
Estou usando assim, alguém pode corrigis este código para mim?

with FDados.Query1 do
Begin
Close;
SQL.Clear;
Sql.Add(´Select *´);
Sql.Add(´from Fieis´);
SQL.Add(´where extract(month from DATANASCIMENTO) = :Mes´);
ParamByName(´Mes´).AsInteger := StrToInt(Edit1.Text);
Open;
end;


Luiz.gv

Luiz.gv

Curtidas 0

Respostas

Ajmsistemas

Ajmsistemas

02/08/2003

Gostaria de escolher um mês, em um combobox por exemplo, e relacionar todas as pessoas que fazem aniversário neste mês. OBS: minha tabela é paradox, e o campo DATANASCIMENTO=ALPHA, não é DATE. Estou usando assim, alguém pode corrigis este código para mim? with FDados.Query1 do Begin Close; SQL.Clear; Sql.Add(´Select *´); Sql.Add(´from Fieis´); SQL.Add(´where extract(month from DATANASCIMENTO) = :Mes´); ParamByName(´Mes´).AsInteger := StrToInt(Edit1.Text); Open; end;



Amigo seu código esta correto porém no banco de dados esta errado... mude de Alpha p/Date e seu problema será resolvido...

Andeson de Jesus.


GOSTEI 0
Aroldo Zanela

Aroldo Zanela

02/08/2003

Colega,

Se a coluna é caractere, o mês está representado em alguma posição fixa na estruturada da data. Portanto, você não pode utilizar função de manipulação de data, como extract/month, neste caso, terá que utilizar função de manipulação de strings, como substring.

Supondo que o formato seja dd/mm/aaaa (sem templates), pode ser efetuado da seguinte forma em banco de dados locais (Paradox e xBase):

with FDados.Query1 do 
Begin 
Close; 
SQL.Clear; 
Sql.Add(´Select *´); 
Sql.Add(´from Fieis´); 
SQL.Add(´where SUBSTRING(DATANASCIMENTO FROM 3 FOR 2) = :Mes´); 
ParamByName(´Mes´).AsString := Edit1.Text; 
Open; 
end;


[list:814fa24023]
community.borland.com

Article #16153: SQL: Using the SUBSTRING Function

Technical Information Database

TI1153D.txt SQL: Using the SUBSTRING Function
Category :Database Programming
Platform :All
Product :Delphi All

Description:
The SQL function SUBSTRING can be used in Delphi applications that include
local SQL queries, but is not supported for InterBase (IB) or the Local
InterBase Server (LIBS) tables. What follows is the syntax for the
SUBSTRING function, examples of its use in local SQL queries, and an
alternative that will return the same results for IB/LIBS tables.

The syntax for the SUBSTRING function is:

SUBSTRING( FROM [, FOR ])

Where:

is the name of the column in the table from which the sub-
string is to be extracted.

is the point in the column value from which the sub-string to
be extracted will start.

is the length of the sub-string to be extracted.

Using these values, the use of the SUBSTRING function below would return
the second, third, and fourth characters from a column named COMPANY:

SUBSTRING(COMPANY FROM 2 FOR 3)

The SUBSTRING function can be used either in the field list for a SELECT
query or in the WHERE clause of a query to allow for comparing a value
with a specific sub-set of a column. The SUBSTRING function can only be
used with String type columns (the CHAR type in SQL parlance). Here is an
example of the SUBSTRING function used in a columns list in a SELECT
query (using the sample Paradox table CUSTOMER.DB):

SELECT (SUBSTRING(C.´COMPANY´ FROM 1 FOR 3)) AS SS
FROM ´CUSTOMER.DB´ C

This SQL query extracts the first three characters from the COMPANY
column, returning them as the calculated column named SS. Now, an example
of the SUBSTRING function used in the WHERE clause of an SQL query (using
the same sample table):

SELECT C.´COMPANY´
FROM ´CUSTOMER.DB´ C
WHERE SUBSTRING(C.´COMPANY´ FROM 2 FOR 2) = ´an´

This query returns all rows from the table where the second and third
characters in the COMPANY column are ´ar´.

As the SUBSTRING function is not supported at all by IB or LIBS databases,
it is not possible to have a sub-string operation in the column list of
a query (exception: IB can do sub-strings via User-Defined Functions).
But through use of the LIKE operator and the accompanying character
substitution marker, it is possible to effect a sub-string in a WHERE
clause. For example, using the sample table EMPLOYEE (in the EMPLOYEE.GDB
database):

SELECT LAST_NAME, FIRST_NAME
FROM EMPLOYEE
WHERE LAST_NAME LIKE ´_an¬´

This SQL query would return all rows in the table where the second and
third characters of the LAST_NAME column are ´an´, similar to the
previous example for the Paradox table. While IB and LIBS databases
would require this method for performing sub-string comparisons in the
WHERE clause of a query and cannot use the SUBSTRING function, Paradox and
dBASE tables (i.e., local SQL) can use either method.



Reference:


7/16/98 4:34:03 PM

Last Modified: 01-SEP-99
[/list:u:814fa24023]


GOSTEI 0
Luiz.gv

Luiz.gv

02/08/2003

Agradeço a você, Aroldo Zanela, sua dica me ajudou muito.
Finalizando, tenho tabela paradox, campo DATANASCIMENTO=ALPHA, formato de data= DD/MM/AA.

E desta forma consegui pegar o mês:


with FDados.Query1 do
Begin
Close;
SQL.Clear;
Sql.Add(´Select *´);
Sql.Add(´from Fieis´);
SQL.Add(´where SUBSTRING(DATANASCIMENTO FROM 4 FOR 2) = :Mes´);
ParamByName(´Mes´).AsString := Edit1.Text;
Open;
end;


GOSTEI 0
POSTAR