table has DEPNO DATE it contains 2 records as :
1 2004-05-02
2 03-APR-04
I want to process above 2 records dates in in yyyy-mm-dd format in hive
You can try in following way:
select TO_DATE(from_unixtime(UNIX_TIMESTAMP(recordDate, 'yyyy-mm-dd'))) from table_name
Refer Hive Date Functions for more details
couple of options
1. Use UDF to take the column and return what ever format you want.
2. Use case when statement like
select CASE WHEN length(col) = 10 then unix_timestamp(col, 'yyyy-MM-dd')
else unix_timestamp(col, 'yy-MMM-dd') from whatever.
Related
I have this query
SELECT TblSales.ProductCode, TblSales.ProductName, TblSales.QtySold, Right([Zdate],7) AS [Mn/Yr]
FROM TblSales
WHERE (((TblSales.zDate) Between [Forms]![FrmSales]![From] And [Forms]![FrmSales]![FinalTo]))
GROUP BY TblSales.ProductCode, TblSales.ProductName, TblSales.QtySold, Right([Zdate],7);
I need this result to be like these columns (ProductCode-ProductName-Sum Of QtySold in First month from the given date - Second month - Third month - and so on)
Example : If the two dates were #1-1-2018# To #31-3-2018# -These dates can be changed due to [Forms]![FrmSales]![From] And [Forms]![FrmSales]![FinalTo]-
Columns:
ProductCode -ProductName -Jan-2018 -Feb-2018- March-2018
Rows:
A1-Computer-2000-2500-3000
Previous Qty is the SumOfQtySold in every month between the two dates,Thanks in advance.
Edit #1 :
I couldn’t make a crosstab query and this message popup Crosstab Error
You can use a crosstab query to transpose row data into columns. Something like this:
PARAMETERS StartDate DateTime, EndDate DateTime;
TRANSFORM NZ(Sum(tblSales.QtySold), 0) AS SumOfQtySold
SELECT tblSales.ProductCode, tblSales.ProductName
FROM tblSales
WHERE (((tblSales.zDate) Between [StartDate] And [EndDate]))
GROUP BY tblSales.ProductCode, tblSales.ProductName
PIVOT Format([tblSales].zDate,"mmm-yyyy");
Is there a way to find the number of days in a month in DB2. For example I have a datetime field which I display as Jan-2020, Feb-2020 and so on. Based on this field I need to fetch the number of days for that month. The output should be something like below table,
I'm using the below query
select reportdate, TO_CHAR(reportdate, 'Mon-YYYY') as textmonth from mytable
Expected output
ReportDate textMonth No of Days
1-1-2020 08:00 Jan-2020 31
1-2-2020 09:00 Feb-2020 29
12-03-2020 07:00 Mar-2020 31
Try this:
/*
WITH MYTABLE (reportdate) AS
(
VALUES
TIMESTAMP('2020-01-01 08:00:00')
, TIMESTAMP('2020-02-01 09:00:00')
, TIMESTAMP('2020-03-12 07:00:00')
)
*/
SELECT reportdate, textMonth, DAYS(D + 1 MONTH) - DAYS(D) AS NO_OF_DAYS
FROM
(
SELECT
reportdate, TO_CHAR(reportdate, 'Mon-YYYY') textMonth
, DATE(TO_DATE('01-' || TO_CHAR(reportdate, 'Mon-YYYY'), 'dd-Mon-yyyy')) D
FROM MYTABLE
);
Db2 has the function DAYS_TO_END_OF_MONTH and several others which you could use. Based on your month input, construct the first day of the month. This should be something like 2020-01-01 for Jan-2020 or 2020-02-01 for Feb-2020. Follow the link for several other conversion functions which allow you to transform between formats and to perform date arithmetics.
convert your column to a proper date and try this: day(last_day(date_column))
I'm trying to select records from a DB2 Iseries system where the date field is greater than the first of this year.
However, the date fields I'm selecting from are actually PACKED fields, not true dates.
I'm trying to convert them to YYYY-MM-DD format and get everything greater than '2018-01-01' but no matter what I try it says it's invalid.
Currently trying this:
SELECT *
FROM table1
WHERE val = 145
AND to_date(char(dateShp), 'YYYY-MM-DD') >= '2018-01-01';
it says expression not valid using format string specified.
Any ideas?
char(dateshp) is going to return a string like '20180319'
So your format string should not include the dashes.. 'YYYYMMDD'
example:
select to_date(char(20180101), 'YYYYMMDD')
from sysibm.sysdummy1;
So your code should be
SELECT *
FROM table1
WHERE val = 145
AND to_date(char(dateShp), 'YYYYMMDD') >= '2018-01-01';
Charles gave you a solution that converts the Packed date to a date field, and if you are comparing to another date field, this is a good solution. But if you are comparing to a constant value or another numeric field, you could just use something like this:
select *
from table1
where val = 145
and dateShp >= 20180101;
I have a SELECT statement that I am trying to convert from Netezza SQL to Impala SQL. The output looks something like 140612, which is a date that is obtained by subtracting 7 from the current date and then pulling out the monday of that week.
I need to have this readable for Impala, then format it, then turn it into a string.
The query is :
TO_CHAR(next_day(DATE(a.date)-7, 'Monday'), 'YYMMDD') AS START_DATE
Assuming a.date is a timestamp, and T is the day of the week (1 = Sunday, 7 = Saturday; for your example above, Monday = 2, so T = 2) you should be able to use use
date_add(a.date, 7 - pmod(dayofweek(a.date) - T, 7));
in place of next_day in the above query. Check out the documentation on Impala's built-in date and time functions for more detail.
select MONTH (birth_date) ...
....
..
However this gives numeric values.
I need Jan , Sep ...
How can I do this ? Case birth_date when 1 then 'jan' when 2 then ... is too long
Any other efficient ways ?
You use several functions that provide the information that you need:
VARCHAR_FORMAT - http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0007110.html
SUBSTR - http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0000854.html
LCASE - http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0052924.html
Example
Select LCASE(SUBSTR(VARCHAR_FORMAT(birth_date,'Month'),1,4))
From ...
This works with Timestamp or date
If the values of that column are not a time stamp you will have to do a CASE statement. However, if it is a timestamp than maybe you can use the MONTHNAME function