To fetch based on filter by expression component in ETL - ab-initio

I have filter condition based on date. where it needs fetch records between given dates.
in filter by expression I gave as below
the field is date datetype and format is YYYYMMDD
fieldname >= '20020502' and fieldname <= '20050430'
but records are not passed next component.
Did I gave the condition righty?

I have another method which always works....
Use date_diff function for the same....
Condition will be provided as below :
Date_diff(date1,fieldname).days >0 & & date_diff(date2,fieldname).days<0

Try typecasting the dates.
(date("YYYYMMDD"))fieldname => '20020502' and (date("YYYYMMDD"))fieldname <= '20050430'

The first point you need to check here is what is the data type of the field.
If the field is a date or datetime type then use directly the functions like date_diff() or so.
If the field is of type string or decimal then a typecasting to date or datetime is necessary before you use the functions like date_diff().
Thanks
Arijit

As you mentioned that
field is already in date datatype with YYYYMMDD format
You only have to typecast the right-hand-side of your expression.
my_date >= (date("YYYYMMDD")) "20150102" && my_date <= (date("YYYYMMDD")) "20150105"
Considered input data and output is:
20150101
20150102 --> with the above condition goes to output
20150103 --> with the above condition goes to output
20150104 --> with the above condition goes to output
20150105 --> with the above condition goes to output
20150106

Related

Select only date form datetime field in where condition

I have stored this kind of format 2022-02-06 18:40:00 in my trans_reminder_date. I want to use only date in where condition but with this condition i am not able to fetch data
$today = date('Y-m-d');
SELECT * FROM sales_detail
WHERE trans_reminder_date = '".$today."'
AND trans_reminder_date != ''
ORDER BY sales_detail_id DESC";
If when your filter parameter is a string:
select * from sales_detail
where cast(trans_reminder_date as date) = cast('2020-03-22' as date)
if you want to use the current date for filtering then MySQL has a function that getting only the current date without time.
select * from sales_detail
where cast(trans_reminder_date as date) = curdate()
On MySQL for converting other types to another, you can use a cast
P.S.
Starting with MySQL 8.0.13 we have now an easiest way to create functional indexes. When you are using cast(updated_at as date) then DB will not use index for column updated_at. You must create a functional index for best performance.
The following query will give you all the result for the current date. By doing this there won't be any need to cast values.
SELECT
*
FROM
sales_detail
WHERE
trans_reminder_date >= curdate()
AND trans_reminder_date < curdate() + INTERVAL '1' DAY
ORDER BY
sales_detail_id DESC;
Using Cast function to change datetime type to date example
Cast(column_name as date)
Or
You using convert function change datetime type to date
CONVERT(column_name, date);
CONVERT(expression, datatype);
OR,
CONVERT(expression USING character_set);
character_set: It specifies the desired character set in which we want to be converted.
Data type : It specifies the desired data type in which we want to be converted.
Expression : It is a specified value going to be converted into another specific datatype.

SQL query to parse a string and compare the value with today's date

I have a data field with values: 2020Q3, 2020Q4, 2021Q1, 2021Q2, etc and I need to compare if Today's date > the data field value in SQL. For example, Today's date > 2020Q3 should return true. Today's date > 2020Q4 should return false.
I tried string tokenizer to parse and compare but no luck. Please advise. Thanks!
If you have a value like 'YYYY"Q"Q', you can get the start date of the quarter using:
select date(concat(left(yyyyq, 4), '-01-01')) + interval 3 * (right(yyyyq, 1) - 1) month
from (select '2020Q3' as yyyyq) t
And then add three months for the end date. I'm not quite sure how you are interpreting >, but that information should give you what you need for the comparisons.

How can i do a count of two columns in mysql?

i want to do a count of two columns in mysql. One of the columns is a string but another is a date like 06/08/2017 and when i do my query i get 0 results.
SELECT count(*) FROM `castigos` WHERE inicio_normal=05/06/2017 AND cod_emplazamiento=1
I have entries of that data but its dont show me anything. Maybe the type of data in the date is wrong?
What should i do?
Add the date field to your select and group by it. Otherwise mysql extensions doesn't recognize you want to group by the date and will aggregrate all the results into 1 column. And since you are getting 0 count, you're where clause must not be working.
Your date format seems malformed. usually YYYY/MM/DD format (standard format);
or specify a format using SELECT STR_TO_DATE('17/09/2010','%d/%m/%Y');
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.
the below uses the implicit casting and default date format to convert the string date to a valid date.
SELECT inicio_normal, count(*)
FROM `castigos`
WHERE inicio_normal='2017/05/06'
AND cod_emplazamiento=1
GROUP BY inicio_normal
Otherwise its doing math and comparing that date to the number stored for the date.
Understand dates should be stored in a date datatype and when you query dates you're passing in a string that is being cast to a date datatype for comparison. So you need to use the standard format, or cast your string to a date so the db engine knows how to convert your format to a date.
Try this :
SELECT count(*) FROM `castigos` WHERE inicio_normal="05/06/2017" AND cod_emplazamiento=1 GROUP BY inicio_normal
WHERE inicio_normal=05/06/2017
If you divide 3 by 6 then by 2017 you get a very small value indeed. OTOH if you reformat this as a date (e.g. 20170605, if you gave us a European formatted date - dd/mm/yyyy) then your query will find the rows you showed us.

SQL Where Date is greater than X

I am trying to run the query;
SELECT PO_PurchaseOrderDetail.ItemCodeDesc,
PO_PurchaseOrderDetail.QuantityOrdered,
PO_PurchaseOrderDetail.QuantityReceived,
PO_PurchaseOrderDetail.UnitCost,
PO_PurchaseOrderDetail.JT158_WTSalesOrderNo,
PO_PurchaseOrderDetail.PurchaseOrderNo,
PO_PurchaseOrderHeader.PurchaseName,
PO_PurchaseOrderHeader.PurchaseOrderDate
FROM PO_PurchaseOrderDetail PO_PurchaseOrderDetail, PO_PurchaseOrderHeader PO_PurchaseOrderHeader
WHERE (PO_PurchaseOrderDetail.PurchaseOrderNo=PO_PurchaseOrderHeader.PurchaseOrderNo)
AND ***(PO_PurchaseOrderHeader.PurchaseOrderDate > '2013-12-31')***
ORDER BY PO_PurchaseOrderDetail.JT158_WTSalesOrderNo, PO_PurchaseOrderDetail.PurchaseOrderNo
My problem is, that the date is stored in the YYYY-MM-DD format. I need to only show data after a certain date. So, because 2013-12-31 is a string rather than a number, the operator > doesn't work. Any ideas?
Try to CAST string as DATE in following:
P.S. if PurchaseOrderDate is not DATE datatype you could also CAST It: CAST(PO_PurchaseOrderHeader.PurchaseOrderDate AS DATE)
SELECT PO_PurchaseOrderDetail.ItemCodeDesc,
PO_PurchaseOrderDetail.QuantityOrdered,
PO_PurchaseOrderDetail.QuantityReceived,
PO_PurchaseOrderDetail.UnitCost,
PO_PurchaseOrderDetail.JT158_WTSalesOrderNo,
PO_PurchaseOrderDetail.PurchaseOrderNo,
PO_PurchaseOrderHeader.PurchaseName,
PO_PurchaseOrderHeader.PurchaseOrderDate
FROM PO_PurchaseOrderDetail PO_PurchaseOrderDetail,
PO_PurchaseOrderHeader PO_PurchaseOrderHeader
WHERE PO_PurchaseOrderDetail.PurchaseOrderNo = PO_PurchaseOrderHeader.PurchaseOrderNo AND
PO_PurchaseOrderHeader.PurchaseOrderDate > CAST('2013-12-31' AS DATE)
ORDER BY PO_PurchaseOrderDetail.JT158_WTSalesOrderNo,
PO_PurchaseOrderDetail.PurchaseOrderNo

MySQL varchar timestamp column stored two different ways

I have a table with device data, one of the columns created_ts -> varchar(30)
The problem: this data in this column contains both linux timestamps and varchars for example:
1381148885
and
2012-09-17 22:13:17
How can I query this column for all records with created_ts > 2013-10-01
I'd opt for distinguishing between the string formats (either 'YYYY-MM-DD' or unix timestamp integer) by checking for a dash character.
I'd consider explicitly converting both of those formats to the DATE datatype, using an appropriate conversion. I'd compare the resulting DATE value with the date literal.
Something like this:
WHERE IF(LOCATE('-',t.created_ts,5), -- which format (yyyy-mm-dd or integer)
STR_TO_DATE(t.created_ts,'%Y-%m-%d %T'), -- convert yyyy-mm-dd string to date
FROM_UNIXTIME(t.created_ts) -- convert string as integer to date
) >= '2013-10-01' -- compare to date literal
Another option would be to convert the string column and the date literal to integer values, and do an integer comparison. (Again, two different conversions for the string column, depending on the format.)
NOTE: I included the hh:mm:ss portion in the conversion with the %T.
When no time component is supplied, the time components is assumed to be midnight (zeros) 00:00:00, and that comes into play depending on whether or not we want to consider
'2013-10-01 07:34:55' > '2013-10-01 00:00:00'
OP query has a greater than comparison. I used a greater than or equal to comparison.
This could all be adjusted, depending on the requirements. We want to be aware that if we aren't careful, some values will get "rounded down" to the previous midnight, and then when we do a greater than comparison, what we're really getting is equivalent to >= '2013-10-02'.
My preference is to make it more explicit. It makes it easier for the reader to understand what the query is actually doing.
UPDATE
I had the arguments in the LOCATE function backwards... the string to search for should be the first argument, the string to be searched is second. That's been corrected in the query above.
Something like this:
select * from yourTable
where created_ts > '2013-10-01'
or from_unixtime(created_ts) > '2013-10-01';