how to use datetimepicker on Delphi 7 - mysql

How to use TDateTimePicker with time format (hour:minute:second)?
I tried this code:
Sql.text:= ('select * from namatabel where namafield between '+quotedstr(formatdatetime('yyyy/mm/dd', datetimepicker1.date))+' and '+quotedstr(formatdatetime('yyyy/mm/dd', datetimepicker2.date))+' and field order by field ASC');
Also this code:
Sql.text:= ('select * from tablename where fieldname between '+quotedstr(formatdatetime('yyyy/mm/dd hh:mm:ss', datetimepicker1.date))+' and '+quotedstr(formatdatetime('yyyy/mm/dd hh:mm:ss', datetimepicker2.date))+' and fieldname order by fieldname ASC');
Also this code:
Sql.text:= ('select * from tablename where fieldname between '+quotedstr(formatdatetime('yyyy/mm/dd hh:mm:ss', datetimepicker1.datetime))+' and '+quotedstr(formatdatetime('yyyy/mm/dd hh:mm:ss', datetimepicker2.datetime))+' and fieldname order by fieldname ASC');
but those wont work, help me to correct them.
I'm using SQLYog against a MySQL database.
UPDATE
#Jens Borrisholt i try this code before, but it doesnt work either
SQL.Add('SELECT meteran.kd_meter as no,kamar.nama,meteran.waktu,meteran.meter '+
'FROM meteran,kamar WHERE kamar.idkamar = meteran.idkamar AND meteran.waktu BETWEEN :tgl1 and :tgl2 and waktu group by waktu asc');
ParamByName('tgl1').AsString:=FormatDateTime('yyyy-mm-dd',DateTimePicker1.DateTime);
ParamByName('tgl2').AsString:=FormatDateTime('yyyy-mm-dd',DateTimePicker2.DateTime); end;
DBGrid2.DataSource.DataSet:=MyQuery2;

Your query is wrong
and fieldname is too much
shortened with data
NOT OK
Sql.text:= 'select * from tablename where fieldname between "2006-06-11"'+
'and "2006-06-19" and fieldname order by fieldname ASC';
OK
Sql.text:= 'select * from tablename where fieldname between "2006-06-11" '+
'and "2006-06-19" order by fieldname ASC';
example valid SQL with data
select * from auktionen where auktionende between '2006-06-11' and '2006-06-19' order by auktionende ASC;
All together with your code. without and waktu
BETWEEN :tgl1 and :tgl2 and waktu
MyQuery2.Close;
MyQuery2.SQL.Text := 'SELECT meteran.kd_meter as no,kamar.nama,meteran.waktu,meteran.meter '+
'FROM meteran,kamar WHERE kamar.idkamar = meteran.idkamar'+
' AND meteran.waktu BETWEEN :tgl1 and :tgl2'+
' group by waktu';
MyQuery2.ParamByName('tgl1').AsString:=FormatDateTime('yyyy-mm-dd',DateTimePicker1.Date);
MyQuery2.ParamByName('tgl2').AsString:=FormatDateTime('yyyy-mm-dd',DateTimePicker2.Date);
DBGrid2.DataSource := DataSource1;
MyQuery2.Open;

I mt sure what is your exact requirement.ill try to help you.
I)If you want to compare the date of the datetimepicker against a date of datetime field in table,
SQL.Add(format('select * from tablename where cast ([my_date_time_var] as date) between %s and %s ' ,[Quotedstr(FormatDateTime('MM/DD/YYYY',(datepicker1.Date))),Quotedstr(FormatDateTime('MM/DD/YYYY',(datepicker2.Date)))]))
II)If you want to compare the date time of the datetimepicker against a date time of datetime field in the table,
SQL.Add(format('select * from tablename where my_date_time_var between %s and %s ' ,[Quotedstr(FormatDateTime('MM/DD/YYYY HH:MM:SS',(datepicker1.DateTime))),Quotedstr(FormatDateTime('MM/DD/YYYY HH:MM:SS',(datepicker2.DateTime)))]));
III)If you want to compare the time of the datetimepicker against a time of datetime field in table,
SQL.Add(format('select * from tablename where cast ([my_date_time_var] as time) between %s and %s ' ,[Quotedstr(FormatDateTime('HH:MM:SS',(datepicker1.time))),Quotedstr(FormatDateTime('HH:MM:SS',(datepicker2.Time)))]))
Please let me know if your requirement is not the same

Related

OpenQuery Date Selection

I have the below openquery which works fine but now I want to select only transactions >= '2017-07-01' using gl_trans.gl_trans_date but cannot get the correct syntax. The backend database is Informix
I have tried many suggestions via google without success :(
SELECT *
FROM OpenQuery(PRONTO_L01, 'SELECT
gl_trans.gl_accountcode,
gl_trans.gl_amount,
gl_trans.gl_trans_date ,
gl_trans.gl_details
FROM gl_trans
WHERE gl_trans.gl_amount <> 0')
Modify query on WHERE clause & add new column condition with it as shown below. Try this:
SELECT *
FROM OpenQuery(PRONTO_L01, "SELECT
gl_trans.gl_accountcode,
gl_trans.gl_amount,
gl_trans.gl_trans_date ,
gl_trans.gl_details
FROM gl_trans
WHERE gl_trans.gl_amount <> 0 AND gl_trans.gl_trans_date >= '2017-07-01'")
Problem was with Informix date format being MM-DD-2017, resolved using DATE function as below
gl_trans.gl_trans_date >= DATE('07-01-2017)'"
I did something like that and it worked.
Declare #date DATETIME = '2017-07-01';
SELECT *
FROM OpenQuery(PRONTO_L01, 'SELECT
gl_trans.gl_accountcode,
gl_trans.gl_amount,
gl_trans.gl_trans_date ,
gl_trans.gl_details
FROM gl_trans
WHERE gl_trans.gl_trans_date >= ''''' +
CONVERT(CHAR(19), #date, 120) + N'''''''))

MySQL : Concat in where clause not working

I am facing a strange problem in mysql while searching multiple columns at once.
select * from postal_address where concat(address1, ' ', address2) = 'ABC DEF';
The above query returns data perfectly but the following doesn't return any data
select * from postal_address where concat(address1, ' ', address2) != 'ABC DEF';
I have other rows which have null values in the table for those columns.
Following queries should work
select * from postal_address where concat(address1, ' ', address2) NOT LIKE 'ABC DEF';
select * from postal_address where concat(address1, ' ', address2) <> 'ABC DEF';
In MySQL, <> symbol refers to Not Equal to
Hope it helps!

Need Correct Format for CONCAT sql query. Codeigniter Active Record

I am using codeigniter's active record, but i've realized I probably need a regular SQL query for what I'm trying to accomplish. Could someone please look at this code and give me the correct format. What am I doing wrong here?
I think it's obvious what I'm trying to accomplish just by reading the code, but if you need more than just the syntax i've used (obviously in error), I'm happy to provide an explanation of code.
$today = date('Y-m-d H:i:s');
$where = "type == pk_card AND (end_date > $today OR qty >= 1)";
$this->db->select('id, title, pk_card_set, pk_card_number');
$this->db->from('auctions');
$this->db->where($where);
$this->db->like("CONCAT(title, ' ', pk_card_number, ' ', pk_card_set)", $keyword);
$query = $this->db->get();
There are number of mistakes in your code the way you are trying to query through active record
type == pk_card -> mysql if you want to compare any value against your column you need single = sign not double == correct one is type = 'pk_card'
type == pk_card ->Again if you want to compare any string value against column you need to wrap it with standard single quotes ' like type = 'pk_card'
end_date > $today if you want to compare date then again you need warp date string in single quotes like end_date > '$today'
And now you can write your active record query as below
$today = date('Y-m-d H:i:s');
$keyword='Test';
$where = "(end_date > '$today' OR qty >= 1)";
$this->db->select("id, title, pk_card_set, pk_card_number ");
$this->db->from('auctions');
$this->db->where($where,null,FALSE);
$this->db->where('type','pk_card');
$this->db->like("CONCAT(title, ' ', pk_card_number, ' ', pk_card_set)", $keyword);
$query = $this->db->get();
this will generate a query similar to
SELECT
`id`,
`title`,
`pk_card_set`,
`pk_card_number`
FROM
(`auctions`)
WHERE (
end_date > '2014-08-10 12:47:06'
OR qty >= 1
)
AND `type` = 'pk_card'
AND CONCAT(
title,
' ',
pk_card_number,
' ',
pk_card_set
) LIKE '%Test%'
Active Record

MySQL, Concatenate two columns

There are two columns in a MySQL table: SUBJECT and YEAR.
I want to generate an alphanumeric unique number which holds the concatenated data from SUBJECT and YEAR.
How can I do this? Is it possible to use a simple operator like +?
You can use the CONCAT function like this:
SELECT CONCAT(`SUBJECT`, ' ', `YEAR`) FROM `table`
Update:
To get that result you can try this:
SET #rn := 0;
SELECT CONCAT(`SUBJECT`,'-',`YEAR`,'-',LPAD(#rn := #rn+1,3,'0'))
FROM `table`
You can use mysql built in CONCAT() for this.
SELECT CONCAT(`name`, ' ', `email`) as password_email FROM `table`;
change field name as your requirement
then the result is
and if you want to concat same field using other field which same then
SELECT filed1 as category,filed2 as item, GROUP_CONCAT(CAST(filed2 as CHAR)) as item_name FROM `table` group by filed1
then this is output
In php, we have two option to concatenate table columns.
First Option using Query
In query, CONCAT keyword used to concatenate two columns
SELECT CONCAT(`SUBJECT`,'_', `YEAR`) AS subject_year FROM `table_name`;
Second Option using symbol ( . )
After fetch the data from database table, assign the values to variable, then using ( . ) Symbol and concatenate the values
$subject = $row['SUBJECT'];
$year = $row['YEAR'];
$subject_year = $subject . "_" . $year;
Instead of underscore( _ ) , we will use the spaces, comma, letters,numbers..etc
In query, CONCAT_WS() function.
This function not only add multiple string values and makes them a single string value. It also let you define separator ( ” “, ” , “, ” – “,” _ “, etc.).
Syntax –
CONCAT_WS( SEPERATOR, column1, column2, ... )
Example
SELECT
topic,
CONCAT_WS( " ", subject, year ) AS subject_year
FROM table
I have two columns:
prenom and nom so to concatenate into a column with name chauffeur_sortant I used this script:
SELECT date as depart, retour, duree_mission, duree_utilisation, difference, observation, concat( tb_chaufeur_sortant.prenom, ' ', tb_chaufeur_sortant.nom) as chauffeur_sortant, concat(tb_chaufeur_entrant.prenom, ' ', tb_chaufeur_entrant.nom) as chauffeur_entrant
FROM tb_passation
INNER JOIN tb_vehicule
ON tb_vehicule.id = tb_passation.id_vehicule
INNER JOIN tb_chaufeur_sortant
ON tb_chaufeur_sortant.id = tb_passation.id_sortant
INNER JOIN tb_chaufeur_entrant
ON tb_chaufeur_entrant.id = tb_passation.id_entrant WHERE tb_vehicule.id = '';
$crud->set_relation('id','students','{first_name} {last_name}');
$crud->display_as('student_id','Students Name');

MySQL: Query for list of available options for SET

In particular table there exists a field of SET type with specific legal values:
personType SET('CUSTOMER','SUPPLIER','EMPLOYEE', 'CONTRACTOR') NOT NULL
Is there any way to query MySQL to get a list of the valid values? In the MySQL interpreter I would just run DESCRIBE someTable; however if there is a more direct method that one could use programmatically without lots of parsing it would be nice.
Thanks.
Now, this simply freaks out, but it is MySQL-only and it works!
SELECT TRIM("'" FROM SUBSTRING_INDEX(SUBSTRING_INDEX(
(SELECT TRIM(')' FROM SUBSTR(column_type, 5)) FROM information_schema.columns
WHERE table_name = 'some_table' AND column_name = 'some_column'),
',', #r:=#r+1), ',', -1)) AS item
FROM (SELECT #r:=0) deriv1,
(SELECT ID FROM information_schema.COLLATIONS) deriv2
HAVING #r <=
(SELECT LENGTH(column_type) - LENGTH(REPLACE(column_type, ',', ''))
FROM information_schema.columns
WHERE table_name = 'some_table' AND column_name = 'some_column');
Just replace "some_table" and "some_column" for your specific table/column, and see the magic!
You will see a weird usage of "information_schema.COLLATIONS" - this is because we need a table there - any table - containing at least N rows, where N is the number of elements in your set.
SELECT
column_type
FROM
information_schema.columns
WHERE
table_name = 'some_table'
AND
column_name = 'some_column';
Returns:
column_type
------------------
set('this','that')
The function below returns an array containing all available options for SET with some parsing but not "lots of parsing"... :)
function get_set_values($table_name, $field_name)
{
$sql = 'DESCRIBE ' . $table_name . ' ' . $field_name;
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
return str_getcsv( trim( substr( $row['Type'], 3 ), '()' ), ',', "'" );
}
Remember that in a set column you may have a combination of values or even an empty value (these are also valid).