How can I translate this legacy SQL to standard SQL in BigQuery? - sql

I need to translate this to BigQuery. Can anyone help? Thanks
IF DATEDIFF('day',DATEADD('day',7,snapshot_date),TODAY(),'monday')>=1
THEN 1
END
This is the error I'm getting.
The Google BigQuery Standard SQL database encountered an error while
running this query. Query execution failed: - Syntax error: Expected
"(" but got identifier "DATEDIFF" at [2:25]

Try this:
case when DATE_DIFF(DATE_ADD(snapshot_date, INTERVAL 7 DAY), CURRENT_DATE(), WEEK(MONDAY)) >= 1 then 1 end

Try this (docs):
IF(DATE_DIFF(DATE_ADD(snapshot_date, INTERVAL 7 DAY), CURRENT_DATE(), WEEK(MONDAY)) >= 1, 1, NULL)

Related

Why this gives an error when I run in snowflake?

add_months(CURRENT_DATE() - 1,'MONTH')
throws an error
Invalid argument types for function '>=': (ROW(TIMESTAMP_NTZ(9), VARCHAR(5)), DATE)
Correct syntax would be like below as per documentation:
select current_date(), add_months(current_date(), 1);
In Oracle and Snowflake, the syntax is:
ADD_MONTHS( <date_or_timestamp_expr> , <num_months_expr> )
So you would want:
add_months(CURRENT_DATE, -1)
(Note: CURRENT_DATE() can also be used in Snowflake but not in Oracle.)

Error while executing 'select convert' on hive table

I want to execute this sql query on a hive table:
select * from sampleDB.sampleTable
where sampleDate>=(select convert(DATE, dateadd(MONTH,-6,getdate())));
But I am getting this error: Error while compiling statement: FAILED: parse exception line 1:64 cannot recognize input near 'select' 'convert' '(' in expression specification (state=42000,code=40000)
Can someone help me understand how this can be achieved? Basically I want to filter on date say 6 months from current date.
Thanks!
Hive do not supports only >= in sub query it support only below type of sub query
Scalar subqueries
IN subqueries
EXISTS subqueries
you can achieve the same with daenter link description hereteformate
select * from sampleDB.sampleTable
where sampleDate>= date_format(current_date - interval '7' day,'%Y-%m-%d') ;

ORA-00936: Missing Expression Error Code

New here and was wondering if anyone can shed some light into this ORA-00936 error with my SQL Query.
select Count(Auftragsnr) as Open_Orders
from wa_kopf
where convert(varchar(4), Aender_Datum, 104) = convert(varchar(4), getdate(), 104)
and zustand < (60);
Get the following error:
14:47:37 [SELECT - 0 row(s), 0.000 secs] [Error Code: 936, SQL
State: 42000] ORA-00936: Missing Expression
Any help would be greatly appreciated.
Regards
JRidge
You're using SQL Server syntax, which won't work with an Oracle database. You seem to be trying to format a date as dd.mm.yyyy (but in a 4-character string, somehow?). There is no getdate function in Oracle either, but it does have sysdate instead.
So the Oracle equivalent for what I think you're trying to do would be:
where to_char(Aender_Datum, 'dd.mm.yyyy') = to_char(sysdate, 'dd.mm.yyyy')
But that would just mean you're comparing dates as string, which isn't generally a good idea. If you're looking for any value from today you could use the trunc() function, which strips the time portion from a date by default; if your column is only representing a date and the time is always midnight then you can do:
where Aender_Datum = trunc(sysdate)
If your column has varying times then you could truncate that as well:
where trunc(Aender_Datum) = trunc(sysdate)
... but that can have a performance impact if the column is indexed, and an alternative is to use a range comparison:
where Aender_Datum >= trunc(sysdate)
and Aender_Datum < trunc(sysdate) + 1

SQL Query Current Date Minus 180 days?

Query
I'm getting a syntax error:
SELECT "LastFilterDate"
FROM "Filter Cartridge Updates"
WHERE "Filter Cartridge Updates"."LastFilterDate" < DATE_SUB(CURDATE(),INTERVAL 180 DAY)
I want to select the LastFilterDateS that are older than the current date minus 180 days (6 months).
I'm using LibreOffice base.
error in question:
SQL Status: HY000 Error code: 1000
syntax error, unexpected $end, expecting BETWEEN or IN or
SQL_TOKEN_LIKE
Sandesh gave a fantastic link on the subject, but I'm still getting a syntax error.
variations I've tried:
SELECT *
FROM Filter Cartridge Updates
WHERE LastFilterDate BETWEEN DATE( DATE_SUB( NOW() , INTERVAL 180 DAY ) ) AND DATE ( NOW() )
SELECT *
FROM "Filter Cartridge Updates"
WHERE "Filter Cartridge Updates"."LastFilterDate" BETWEEN DATE( DATE_SUB( NOW() , INTERVAL 180 DAY ) ) AND DATE ( NOW() )
The problem is that you're using MYSQL syntax for an SQL that is not MYSQL.
You can find some help on the LibreOffice website. I found this: =DATEDIF("1974-04-17";"2012-06-13";"d") which yields the number of days between 2 dates.
So you could change your where clause to be (if this allows substitution - you'll have to test that), to:
Where =DATEDIF(last_filter_date,current_date;"d") > 180
Note: I only put stubs in for the dates, you'll have to format those correctly.

sql query to get last 6 months of data

I am trying to get the data for the last 6 months.
This is what I have used:
WHERE d_date > DATEADD(m, -6, current_timestamp)
and I am getting this error.
ERROR: CLI prepare error: SQL0206N "M" is not valid in the context where it is used
also tried
WHERE d_date > current date -180
and got this error:
ERROR: CLI prepare error: SQL0171N The data type, length or value of the argument for the parameter in
position "2" of routine "-" is incorrect. Parameter name: "". SQLSTATE=42815
Please advice.
Based on Andriy's eagle-eyes, here is (I think) the DB2 syntax:
WHERE d_date > current_date - 6 MONTHS
And here is a link to a pretty good function to mirror DATEADD in DB2.
Also, since you mentioned SAS, here is the SAS syntax to do the same thing:
WHERE d_date > intnx('MONTH', today(), -6, 'SAME');
Although you say you are running this with SAS Enterprise Guide, the syntax you show is not SAS. The error message you are getting suggests you are submitting "pass-thru" code directly to a database.
In DB2, it should be something like
WHERE TIMESTAMPDIFF(64, CAST(CURRENT_TIMESTAMP- d_date AS CHAR(22))) <= 6
Remove the SQL-Server tag, that's for MS SQLServer questions.
You can also try this
SELECT *
From PIS.dbo.PIS_tblEmployeeGenInfo
WHERE dtJoiningDate > DATEADD(m, -6, current_timestamp)
I'm using in code the following:
INSERT INTO Employee(I_EMP, T_CRT, C_SYS, D_HIRED)
VALUES (21,CURRENT TIMESTAMP, 10,CURRENT DATE - 6 MONTH);
This is in DB2, and it is working with no issues.
Hope that will help.