OpenQuery Date Selection - mysql

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'''''''))

Related

MS Access - Data type mismatch - simple integer comparison

I just cannot wrap my head around why I am getting this error?
"Data type mismatch in criteria expression."
SELECT * INTO Updated FROM (
SELECT a.*,
date() AS [Today],
DateValue([Last_Updated]) as [LastUpDateDate],
int(datediff("d", date(), DateValue([Last_Updated]))) as [mydatediff]
FROM Prior AS a
WHERE a.[Last_Updated] is not null
) t
WHERE t.[mydatediff] >= 1;
I just want a simple comparison of two integers and I keep getting the type mismatch error.
If Last_Updated is text, it probably holds some invalid date expressions.
Try using IsDate to filter those out:
SELECT * INTO Updated
FROM
( SELECT
a.*,
Date() AS [Today],
DateValue([Last_Updated]) AS [LastUpDateDate],
DateDiff("d", Date(), DateValue([Last_Updated])) AS [mydatediff]
FROM
Prior AS a
WHERE
IsDate(a.[Last_Updated])
) t
WHERE
t.[mydatediff] >= 1;
Alternatively, filter the subquery:
SELECT
t.*,
Date() AS [Today],
DateValue(t.[Last_Updated]) AS [LastUpDateDate],
DateDiff("d", Date(), DateValue(t.[Last_Updated])) AS [mydatediff]
INTO
Updated
FROM
( SELECT *
FROM Prior
WHERE IsDate([Last_Updated])
) t
WHERE
DateDiff("d", Date(), DateValue(t.[Last_Updated])) >= 1;
Solution
The trick is to force the subquery to finalise before retrieving its records. That can be done with DISTINCT:
SELECT *
FROM (
SELECT DISTINCT
Prior.Id,
DateValue([Last_Updated]) AS LastUpDateDate,
DateDiff("d",Date(),DateValue([Last_Updated])) AS mydatediff
FROM
[Prior]
WHERE
IsDate([Last_Updated]) <> False
) AS T
WHERE T.mydatediff >= 1;
Sample:
Output:

Validating age using a function in MySQL

so i'm having some issues using a function in SQL where i calculate the age given a certain date. The thing is that i need to validate with the current date and the date of birth if it's already the year or not.
For example the date i have in a register is 1994-11-15 and when consulting the information with
select EmployeeID Num_Emloyee, concat(FirstName, " . ", LastName) Name_Employee, Title Puesto, fn_Age(BirthDate) Edad, fn_Age(HireDate) WorkYears
from employees;
It returns 24, however if i only consult with select the function it returns 23, the correct answer.
At the moment this is the function i'm using to validate the age is this:
create function fn_Age(dateVal date)
returns int
begin
declare age int;
if day(now()) and month(now()) >= day(dateVal) and month(dateVal) then
set age=year(now())-year(dateVal);
else
set age=(year(now())-year(dateVal)) - 1;
end if;
return age;
end
Is there anything i'm not considering in the function?
day(now()) and month(now()) >= day(dateVal) and month(dateVal)
This logic doesn't make sense. I don't know if an if supports tuples in MySQL. If so, you can do:
(month(now()), date(now())) >= ( month(dateval), day(dateval) )
(this works in a MySQL WHERE clause.)
You can also do:
month(now()) * 100 date(now()) >= month(dateval) * 100 + day(dateval)
You can also use timestampdiff-function
select timestampdiff(year, '1994-11-15', now());

how to use datetimepicker on Delphi 7

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

Age Calculation duplicate column using sql server 2008 R2

I have login table with DOB Column. I need duplicate column for age. But i can't do this. I can convert DOB column convert to Age for alone
`SELECT FLOOR(DATEDIFF(DAY,'10/10/1990' , getdate()) / 365.25)` works fine.
But I need to convert whole column.
If I'm Using
SELECT FLOOR(DATEDIFF(DAY,Select DOB From login_tbl , getdate()) / 365.25) like this,
It's throwing error. How can I get it?
Thankyou
Your approach will get wrong result in such cases below:
declare #now date
set #now = '11/10/2014'
select
FLOOR(DATEDIFF(DAY,'11/10/2013' , #now) / 365.25) -- should be 1 but will get 0
, FLOOR(DATEDIFF(DAY,'11/10/2012' , #now) / 365.25) -- should be 2 but will get 1
, DATEDIFF(DAY,'11/10/2013' , #now)
, DATEDIFF(DAY,'11/10/2012' , #now)
My suggestion is :
declare #now date
set #now = '11/10/2014'
select (convert(int,convert(varchar(8), #now ,112))
- convert(int,convert(varchar(8),convert(date,'11/10/2013'),112) ) )/10000
You could see my explain in this answer.
So for your login_tbl you could:
select
DOB, (convert(int,convert(varchar(8), DOB ,112))
- convert(int,convert(varchar(8),convert(date,'11/10/2013'),112) ) )/10000 as AGE
from
login_tbl
select trunc((trunc(sysdate) - to_date('16-mar-2010', 'dd-mon-yyyy'))/ (365.23076923074))
from dual

How to get a particular date format ('dd-MMM-yyyy') in SELECT query SQL Server 2008 R2

I am using CONVERT(data_type(length),expression,style) function to change the format of a date in a SELECT query.
Declare #dt nvarchar(20)
Select #dt = Convert(nvarchar(20), SalesDate, 113) FROM SalesTable
The format I need is 'dd-MMM-yyyy' (eg. '05-Jul-2013') but I could not find the proper style number (eg. 113) for this particular format. Can any one help me with that please?
Try this:
Declare #dt NVARCHAR(20)
Select
#dt = REPLACE(CONVERT(CHAR(15), SalesDate, 106),' ',' - ')
FROM SalesTable
select CONVERT(NVARCHAR, SYSDATETIME(), 106) AS [DD-MON-YYYY]
or else
select REPLACE(CONVERT(NVARCHAR,GETDATE(), 106), ' ', '-')
both works fine
It doesn't look like DD-MMM-YYYY is supported by default (at least, with dash as separator). However, using the AS clause, you should be able to do something like:
SELECT CONVERT(VARCHAR(11), SYSDATETIME(), 106) AS [DD-MON-YYYY]
See here: http://www.sql-server-helper.com/sql-server-2008/sql-server-2008-date-format.aspx
I Think this is the best way to do it.
REPLACE(CONVERT(NVARCHAR,CAST(WeekEnding AS DATETIME), 106), ' ', '-')
Because you do not have to use varchar(11) or varchar(10) that can make problem in future.
SELECT Convert(varchar(10),CONVERT(date,'columnname',105),105) as "end";
OR
SELECT CONVERT(VARCHAR(10), CAST(event_enddate AS DATE), 105) AS [end];
will return the particular date in the format of 'dd-mm-yyyy'
The result would be like this..
04-07-2016
select convert(varchar(11), transfer_date, 106)
got me my desired result of date formatted as 07 Mar 2018
My column transfer_date is a datetime type column and I am using SQL Server 2017 on azure
select FORMAT(getdate(), 'dd-MMM-yyyy') as DateToday
Other answers here seem to return columns that are quite wide.
This answer returns a varchar(11) which is all that is required for a date in dd-Mon-yyyy format.
'SalesDate' = CONVERT(VARCHAR(11),REPLACE(CONVERT(VARCHAR(11),SalesDate, 106), ' ', '-')),
Try also:
select CONVERT(VARCHAR(11),REPLACE(CONVERT(VARCHAR(11),getdate(), 106), ' ', '-'))
As of today, this gives a result as per: 22-Jul-2021