need help with this query DB2 IBM
SELECT
ABALPH AS Kunde,
SDLITM AS Artikel,
SDDSC1 AS Beschreibung,
SDSOQS AS Menge, date(digits(decimal(SDIVD+1900000,7,0))) AS Invoice,
decimal(SDUPRC/10000,15,2) AS Einzelpreis,
decimal(SDAEXP/100,15,2) AS Gesamtpreis,
SDDOCO AS Dokument,
AIAC01 AS Region
Now my question is , how can I get the today date minus 1 day ?
Thank you so much
have test it with ADD_DAYS doesn´t work.
I do not see the point in your query where a date is referenced but your query seems incomlete anyways because join contions are missing.
In general you get todays date with current date and you get yesterday with current date - 1 day
Check out this query:
SELECT current date, current date - 1 day
FROM sysibm.sysdummy1
Per the DB2 documentation, ADD_DAYS is valid with negative numbers for previous days. See example 4 on that page. So usage like this should have worked:
ADD_DAYS(DATE(...), -1)
or
ADD_DAYS(CURRENT_DATE, -1)
You should post your specific attempted usage of it and the error that is reported when you try it.
Related
I'm trying to get the last week of data in a pipeline script but when I do this I get the above error.
The code I am using is:
where to_date(start_datetime, 'YYYYMMDDHH24MISS') >= date_trunc('week', current_date) - 7
and to_date(start_datetime, 'YYYYMMDDHH24MISS') < date_trunc('week', current_date)
I think the issue could be that the table doesn't ingest data for about 4 days after the fact (i.e if date is 12/12/22 then the most recent date is 08/12/22)
But I've tried it with getting the week that started 2 weeks ago but still get the same error.
Anyone know how to solve this?
Thanks
The problem is in the function to_date and your wrong input format.
If you have something like this in the start_datetime column 12/12/22, you need to specify another format
to_date('12/12/22', 'DD/MM/YY')
More about formats: https://docs.aws.amazon.com/redshift/latest/dg/r_FORMAT_strings.html
I'm working with Big Query and I need to get first day of week.
For example if date = '2022-08-26' I want to have '2022-08-22' where 22 is Monday.
Any solutions please ?
Thanks in advance.
To achieve this you'll want to use the DATE_TRUNC function as follows:
select date_trunc(date('2022-08-26'), WEEK(MONDAY))
You can change the parameter for WEEK to be any day of the week, default is SUNDAY.
Documentation can be found here:
https://cloud.google.com/bigquery/docs/reference/standard-sql/date_functions#date_trunc
I want to get the whole day of the week depend on the date, my query is working and getting the result that I want but when the date value is Sunday, result changes.
I'm starting the result from Mon to Sunday.
Examples below:
My Code:
SELECT UserID,Scdl_TkIN as TimeIn, Scdl_TkOut as [TimeOut]
FROM EmployeeTimekeeping
WHERE CONVERT(DATE,Scdl_TkIN) >= dateadd(day, 2-datepart(dw, '2022-04-23'),CONVERT(date,'2022-04-23'))
AND CONVERT(DATE,Scdl_TkIN) < dateadd(day, 9-datepart(dw, '2022-04-23'), CONVERT(date,'2022-04-23'))AND UserID ='15020009'
ORDER BY CONVERT(DATE,Scdl_TkIN)
1st display is correct, but when I change the value into '2022-04-24' , the result is now the second pic but I want the result still 1st pic.
If I got it right you want the whole week of data given a single date.
I'm not 100% sure about your date logic and I'd rather use the WEEK as a filter as it seems clearer, that said the issue you have is the value of SELECT ##DATEFIRST.
By default its value is 7, meaning that Sunday is considered the first day of the week, that's why you get that "unexpected" result.
here is my solution, but just setting SET DATEFIRST 1; should give you the expected result.
SET DATEFIRST 1;
SELECT
UserID
,Scdl_TkIN as TimeIn
,Scdl_TkOut as TimeOut
FROM EmployeeTimekeeping
WHERE
DATEPART(WEEK,Scdl_TkIN) = DATEPART(WEEK,'2022-04-23')
AND YEAR(Scdl_TkIN) = YEAR('2022-04-23')
AND UserID ='15020009'
ORDER BY
Scdl_TkIN
Note: if you decide to use WEEK for filtering you will have to choose between WEEK and ISO_WEEK
Edit: when using week you must also consider the year in the filter
I've been working on this problem for a while now. Basically I have a simple set of data with UserId, and TimeStamp. I want to know how many distinct UserId's appear each week, the catch is my week is measured in Sunday-Saturday, NOT Monday - Sunday, which is what Weekofyear() uses.
Right now I'm hardcoding each week and running the query:
SELECT
count(distinct UserId)
FROM data.table
where from_unixtime((CAST(timestamp as BIGINT)))
between TO_DATE("2016-06-05") AND TO_DATE("2016-06-12")
I'm trying to find a way to shift the timestamp back a day to trick weekofyear into thinking my Sunday is actually a Monday, but have not been successful. My latest futile attempt looked like:
SELECT
count(distinct UserId), weekofyear(date_sub(from_unixtime(CAST(timestamp as BIGINT)),1))
FROM table.data
where from_unixtime((CAST(timestamp as BIGINT)))
between TO_DATE("2016-06-01") AND TO_DATE("2016-06-30")
group by weekofyear(date_sub(from_unixtime(CAST(timestamp as BIGINT)),1))
This results in the same numbers as if I didn't subtract a day. I not sure why this isn't working. I feel like there should be a way to manage this. Right now if I wanted to pull all the data by week WHERE X is true, I'd have to manually do each week, that won't be sustainable. Any suggestions on how to work smarter?
Thank you.
Simple Solution
You can simply create your own formula instead of going with pre-defined function for "week of the year"
Advantage: you will be able to take any set of 7 days for a week.
In your case since you want the week should start from Sunday-Saturday we will just need the first date of sunday in a year
eg- In 2016, First Sunday is on '2016-01-03' which is 3rd of Jan'16
--assumption considering the timestamp column in the format 'yyyy-mm-dd'
SELECT
count(distinct UserId), lower(datediff(timestamp,'2016-01-03') / 7) + 1 as week_of_the_year
FROM table.data
where timestamp>='2016-01-03'
group by lower(datediff(timestamp,'2016-01-03') / 7) + 1;
I have this date in my history table with field dtEnd="2012-07-10 11:07:00"
and i want to retrieve July 10 and Last Tues both from this date separately.
Can anybody suggest a query for doing this?
Thanks.
While surfing, I came to know that it is not possible to get Month and date name instead you have to use your logic for that.
I got the solution, I am getting month and date with:
Select strftime('%m-%d','dtend')'Date' from history
And then I am extracting month and day number separately and then passing that month number to my own function with switch case to give month name.
That's it and I got my solution.