Date Concatenation in the script - qliksense

I have a Calendar extension retrieve the date format like (DD-MM-YY) and I need to add to this variable the time but it is a fixed time for the begin and the end. The script below failed to execute.
where "begin" >= ('$(vFromDate)' || ' 00:00:00') and "end" <= ('$(vToDate)' || ' 23:59:59').
I tried the (+) it is also failed instead of (||).
Please note that I am connected with a PostgresSQL Server.

I think you are best to use the date# format when generating dates
where "begin" >= (date#($(vFromDate)&' 00:00:00','DD/MM/YYYY HH:mm:SS')) and "end" <= (date#($(vToDate)&' 00:00:00','DD/MM/YYYY HH:mm:SS'))

Related

SAS Proc SQL function in where clause for only current week

I have some data in week-date-time format ie 14dec2020 00:00:00:0000. I am using SAS and a proc sql query
This data contains many weeks worth of data but i was curious if theres in way to only pull data relevant to only current week? IE today is 17dec2020 so i would want to only pull data for the week of 14dec2020. if today was 22dec2020 then i would want it to pull data for the week of 21dec2020.
Here is one of the many queries i have tried.
data have;
today = today();
wkday = weekday(today);
start = today - (wkday - 1);
end = today + (7 - wkday);
length cstart cend $30;
cstart = put(start, date9.) || ' 00:00:00.0000' ;
cend = put(end, date9.) || ' 00:00:00.0000' ;
call symput('start', cstart);
call symput('end', cend);
run;
Proc Sql;
connect to odbc (environment=x user=y p=z);
create table basic.curweek as select * from connection to odbc
(select year, month, week, store, sales, SKU
from datatable
where (&start. <= week <= &end.)
order by sku);
disconnect from odbc;
quit;
Thanks to the help of the great people below i have gotten to this state. But am still facing some syntax errors.
Any help here would be greatly appreciated!!
Use intnx() to align both the datetime of interest and today's datetime to the start of the week.
proc sql;
create table want as
select *
from table
where intnx('dtweek', date, 0, 'B') = intnx('dtweek', datetime(), 0, 'B')
;
quit;
As others have pointed out - if you are using SQL pass-thru, you need to use date functions that exist in your "flavor" of SQL. SAS specific functions will not work, and in particular SAS function "today()" has no meaning in the SQL you are working with.
The approach I would take is:
in a SAS datastep - get today's date
use today's date to calculate beginning and end of the week
convert beginning/end dates to character strings
(string will depend on how dates are formatted in your sql database - date or datetime)
use character strings to create macro variables
feed macro variables into sql pass-thru query to subset dates wanted
Below is some example code. It might not get you all the way there, but could give you some more ideas to try.
data have;
today = today(); *** TODAYs DATE ***;
wkday = weekday(today); *** WEEK DAY NUMBER FOR TODAY, POSSIBLE VALUES ARE 1-7 ***;
start = today - (wkday - 1); *** CALCULATE SUNDAY ***;
end = today + (7 - wkday); *** CALCULATE SATURDAY ***;
*** UNCOMMENT AND USE BELOW IF WEEK START/END IS MON-FRI ***;
*start = today - (wkday - 2); *** CALCULATE MONDAY ***;
*end = today + (6 - wkday); *** CALCULATE FRIDAY ***;
*** REPRESENT DATES AS DATE-TIME CHARACTER STRING - SURROUNDED BY SINGLE QUOTES ***;
cstart = "'" || put(start, date9.) || ' 00:00:00.0000' || "'";
cend = "'" || put(end, date9.) || ' 00:00:00.0000' || "'";
*** USE CHARACTER VARIABLES TO CREATE MACRO VARIABLES ***;
call symput('start', cstart);
call symput('end', cend);
run;
*** IN SQL PASS-THRU, USE MACRO VARIABLES IN WHERE STATEMENT TO SUBSET ONE WEEK ***;
Proc Sql
connect to odbc (environment=x user=y p=z);
create table basic.curweek as select * from connection to odbc
(select year, month, week, store, sales, SKU
from datatable
where (&start. <= week and week <= &end.)
order by sku);
disconnect from odbc;
quit;

DB2 issue with date selection, basically want the previous 12 months from current date

Okay the following where clause works, except in January when the calculation is turning up an invalid date:
WHERE
(DATE((DIGITS(LNYYP2) || '-' || DIGITS(LNMMP2) || '-' || DIGITS(LNDDP2))) >
((CURRENT DATE - DAY (CURRENT TIMESTAMP) DAYS)) - 13 MONTH)
AND DIGITS(SHFP02.LNYYP2) || '-' || DIGITS(LNMMP2) <>
YEAR (CURRENT TIMESTAMP) || '-' || MONTH (CURRENT TIMESTAMP)
Potential issue:
For the second condition,
(DATE((DIGITS(LNYYP2) || '-' || DIGITS(LNMMP2) || '-' || DIGITS(LNDDP2))) >
((CURRENT DATE - DAY (CURRENT TIMESTAMP) DAYS)) - 13 MONTH)
You're comparing two strings and not two dates or two sets of numbers. I'm not sure that's your issue because you don't give any sample data that gives right or wrong answers, but maybe it could be better written as:
and (lnyyp2, lnmmp2) not in (values(int(year(current date)), int(month(current date)))
Sorry, it looks like you are using a different DB than what I thought. This is an answer for how to solve this issue in SQL Server.
Original Anser:
I am not sure exactly what is going on in that where clause, but you can get 12 months ago with DATEADD. If you need to get the first of the month, you can build it with DATEFROMPARTS.
declare #today date;
declare #past date;
set #today = SYSDATETIME();
set #past = dateadd(month, -12, #today)
select #today, #past, DATEFROMPARTS(YEAR(#past), MONTH(#PAST), 1)

Casting DATETIME on concatenated date and time

I'm trying to concatenate a column's date to a fixed time of the day and then CAST the whole thing as DATETIME.
The fixed time is 5:30am.
The date column I'm using needs to be adjusted as it shows the end date/time of when something ran; I want to use the start date/time.
The start date/time time is not available as its own column, but I have another column that has the duration the process took in seconds, so I can use DATEADD to roll the end date/time back to the start date/time.
Here's the full statement:
CAST(CONVERT(VARCHAR(10), DATEADD(ss,-ConfTask.[LastExecutedDuration], ConfTask.[LastExecutedDate]), 103) + ' ' + '05:30' as DATETIME)
Here's the error message I'm receiving:
The conversion of a varchar data type to a datetime data type resulted
in an out-of-range value.
I've tried testing these statements to investigate the issue, but they all run OK on their own:
CAST(CONVERT(VARCHAR(10), GETDATE(), 103) + ' ' + '05:30' as DATETIME)
CAST('2017-03-02' + ' ' + '05:30' as DATETIME)
DATEADD(ss,-ConfTask.[LastExecutedDuration], ConfTask.[LastExecutedDate])
I'm a bit stuck on how to get round this issue. Any help would be much appreciated.
Clearly, you must have some unexpected values in the column.
I would suggest finding them using a query such as this:
SELECT LastExecutedDuration, LastExecutedDate
FROM ConfTask
WHERE TRY_CONVERT(datetime,
CONVERT(VARCHAR(10),
DATEADD(second,
-ConfTask.[LastExecutedDuration],
ConfTask.[LastExecutedDate]
)
103
) + ' ' + '05:30')
)
You can also simplify the logic, by just using date functions:
select dateadd(minute,
5 * 60 + 30,
convert(datetime,
convert(date,
dateadd(second,
- ConfTask.LastExecutedDuration
ConfTask.LastExecutedDate
)
)
)
)
This worked:
CONVERT(DATETIME, CONVERT(CHAR(8), DATEADD(ss,-ConfTask.[LastExecutedDuration], ConfTask.[LastExecutedDate]), 112)) + ' ' + CONVERT(CHAR(8), '05:30:00', 108)

DB2 Adding word beside function MOD

I want to add word Week to show as Week then number from function MOD i tried the following
select EVENTTIMESTAMP, Year (EVENTTIMESTAMP) as Year, QUARTER (EVENTTIMESTAMP) as Quarter, 'Week' MOD(WEEK(EVENTTIMESTAMP)-1, 13) + 1 as WeekNoQuarter
but i get error :
An unexpected token "MOD" was found following ") as Quarter, 'Week'". Expected tokens may include: ",".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.16.53
any suggestion on way to put word Week.
Thanks!
In your code you are trying to name a string 'Week' as MOD(WEEK(EVENTTIMESTAMP)-1, 13) + 1 as WeekNoQuarter. Try concatenating ( || ) the String 'Week ' with the calculated value. Since the calculated value is of type int you will have to cast it:
'Week ' || cast(MOD(WEEK(EVENTTIMESTAMP)-1, 13) + 1 as char(2)) as WeekNoQua...

I think I need to change the format of how the date is stored

I have a field in a SQLite3 database stored as a date field.
Not thinking, I entered the date in the format mm/dd/yyyy and now none of the date functions will work.
Is there a simple way I can convert the dates to the proper format so I can perform searches that work correctly?
If the fields have a fixed length, you can extract them with a few simple substr calls:
UPDATE MyTable
SET DateColumn = substr(DateColumn, 7, 4) || '-' ||
substr(DateColumn, 1, 2) || '-' ||
substr(DateColumn, 4, 2);