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.)
Related
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)
I have a table with datehourminute column with string datatype and formatted as 202012062302. I want to select the date and cast it to date, so I made a query that looks like this:
select cast(date_parse(created_date, '%Y-%m-%d') as date) created_date,
daily_user
from
(
select concat(substr(datehourminute,1,4),'-',
substr(datehourminute,5,2),'-',substr(datehourminute,7,2))
as created_date, sum(users) as daily_user
from "a-schema".a-table
group by 1
)
order by 1;
However, that query returns this error:
SQL Error [100071] [HY000]: [Simba]AthenaJDBC
An error has been thrown from the AWS Athena client.
INVALID_FUNCTION_ARGUMENT: Invalid format: "(oth-er-)"
Any idea how to fix this? Thanks!
what would be the Snowflake counterpart of following Teradata SQL?
select CAST( CAST(( 2*-1 )AS FORMAT '9.99') AS CHAR(4))
will give
"2.00"
however in Snowflake,
select CAST(to_char(( 2*-1), 'FM0.00' ) AS CHAR(5));
gives:
"-2.00"
(example was simplified)
How to get rid of the - symbol?
thank you
Probably using ABS() is a good workaround:
select CAST(to_char(( abs(2*-1)), 'FM0.00' ) AS CHAR(5));
https://docs.snowflake.com/en/sql-reference/functions/abs.html
I need to convert date/time with this format yyyy-MM-ddThh:mm:ssZ to simple date with no time.
Example : 2020-10-21T07:28:48.021Z
to : 2020-10-21
CAST(XX AS TIMESTAMP) works fine, but it doesn't drop the time in my sql query as i would like.
Any idea ? Thanks !
Below is for BigQuery Standard SQL
select date('2020-10-21T07:28:48.021Z')
with output
cast(field as date) OR try_cast( field as date)
/*
Try cast will not return error if it is not convertable it will return null*/
It's done differently in different databases, but in DB2, it's super simple:
select date(thatTimestamp)
from sysibm.sysdummy1;
HTH !
you want cast( 2020-10-21T07:28:48.021Z as date)
or
select cast(dateField as date) as [DateFieldName]
I have following SQL for Microsoft SQL Server
SELECT *
FROM tblA
WHERE CREATION_DATE BETWEEN DATE'2015-09-12' AND DATE'2015-09-15'
But this throws a syntax error:
Error: Incorrect syntax near '2015-09-12'. SQLState: S0001 ErrorCode:
102
What is wrong? I want to use ANSI literal code.
This is not how you cast in MS SQL Server. Instead, you should try the cast syntax:
SELECT *
FROM tblA
WHERE creation_date BETWEEN CAST('2015-09-12' AS DATE) AND
CAST('2015-09-15' AS DATE)
You can simply use this query, without adding date:
SELECT *
FROM tblA
WHERE CREATION_DATE BETWEEN '2015-09-12' AND '2015-09-15'
Date literals are tricky... besides the problem with conversion (often depending on your system's culture) you must think of the day's end if you use between!
Here you find a system independent way to literally write dates: https://msdn.microsoft.com/en-us/library/ms187819.aspx?f=255&MSPPError=-2147217396 (look at "ODBC)
So you could write
BETWEEN {d'2015-09-12'} AND {ts'2015-09-12 23:59:59'}
SELECT *
FROM tblA
WHERE CREATION_DATE BETWEEN CAST('2015-09-12' AS DATE)
AND CAST('2015-09-15' AS DATE)