snowflake version of a teradata case to remove minus value - sql

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

Related

Rounding DOWN postgresql

Here is a example query, I've been trying to find a workaround for this but my knowledge for postgresql at this time is still limited. Thanks in advance.
Sample Query:
select round(3.041,2) as column
Expected Output:
3.03 instead of 3.04
Rounding UP works but I need it to round DOWN as well if the decimal value is <5.
You can round down with some simple decimal counting like
SELECT
3.04 - POWER(10,
(-1 *
CAST(
LENGTH(
SPLIT_PART(
CAST(3.04 AS STRING), '.', 2
)
)
AS INT)
)
)
However, I have no idea why you would do this. Are you sure you aren't trying to use the basic ROUND function and cut off more significant figures?

SQL - convert yyyy-MM-ddThh:mm:ssZ to simple date with no time

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]

How to convert Timestamp to Date Data Type in Google Bigquery

I am trying to convert Timestamp data type columns to Date datatype using:
bq query -q --destination_table=NEW_DATE_TABLE --replace "SELECT DATE(CURR_DT) AS CURR_DT from TEST.DATE_TABLE"
The new table shows the column as STRING rather than date. Is there a way to convert timestamp to date data type.
Requested Screenshot
If you use Standard SQL, you can do the following:
SELECT * REPLACE(EXTRACT(DATE FROM curr_dt)) AS curr_dt FROM test.date_table
If curr_dt is repeated field, then the solution will look the following:
SELECT * REPLACE(
ARRAY(
SELECT EXTRACT(DATE FROM curr_dt) FROM t.curr_dt
) AS curr_dt)
FROM test.date_table t
Consider below!
Works in both Legacy and Standard SQL
SELECT CAST(DATE(CURR_DT) AS DATE) AS CURR_DT FROM TEST.DATE_TABLE
Added to address comment
Try below - as I mentioned above - it works for both Legacy and Standard
SELECT CAST(DATE(CURR_DT) AS DATE) AS CURR_DT
FROM (SELECT CURRENT_TIMESTAMP() AS CURR_DT)
if you are interested in making your case working with Legacy SQL - provide more details about CURR_DT field
Try this
SELECT TIMESTAMP_SECONDS(CAST(CURR_DT AS INT64)) AS CURR_DT FROM TEST.DATE_TABLE

SQL Convert dd/mm/yy to yymmdd

I have been trying to achieve this all day, I have followed numerous tutorials and can't seem to crack it, I have been trying things like:
select CONVERT(VARCHAR(10), DATE, 131) from Table
yet it does not seem to change anything.
Any advice or help would be appreciated. Thankyou in advance.
SELECT CONVERT(VARCHAR(10), DATE, 12)
12 is the right code for the format you want. See: https://msdn.microsoft.com/en-GB/library/ms187928.aspx
Try this
declare #TDATE Date = '2015-11-10';
select Convert(varchar,Datepart(Year, #TDATE))+Convert(varchar,Datepart(Month, #TDATE))+Convert(varchar,Datepart(Day, #TDATE))
Output:
20151110
12 is the right code. Use below query to get output as 'yymmdd'
select CONVERT(VARCHAR(10), DATE, 12) from Table
On PostgreSQL the easiest way is to use to_char:
to_char(date, 'yyyymmdd')::int
One method is to construct the value from date parts. Here is a numeric conversion:
select (year(date) % 100) * 10000) + month(date) * 100 + day(date)
It is easy to convert this to a number:
select cast( (year(date) % 100) * 10000) + month(date) * 100 + day(date) as varchar(10))
The slight advantage to this approach over using convert is that a human being can understand the logic without perusing arcane documentation, specific to SQL Server. I don't know why SQL Server doesn't support a simple format-type function similar to most other databases (and programming languages).
select date_format(column_name, '%Y%m%d') from table_name;

convert datetime (mm/dd/YYYY) to decimal(YYYYmmDD)

I am trying to get the following result in Sql:
example: 23/05/2014 to 20142305
but get this:
select convert(decimal, convert(datetime, '5/23/2014'))
result:41780
anyone know how you can get the following format?? (if possible ??)
regards and thanks
In many databases (including SQL Server), you can just do:
select 10000 * year(datetime) + 100 * month(datetime) + day(datetime)
Some databases don't support these functions, so you might need extract(datepart from datetime) or a similar function.
You can try this
SELECT CAST(CONVERT(VARCHAR(8), datetime, 112) AS DECIMAL)
CREATE FUNCTION DateToYYYYMMDD
(
#Date DateTime
)
RETURNS decimal
AS
BEGIN
DECLARE #ResultVar decimal
SET #ResultVar=Year(#Date)*10000+Month(#Date)*100+Day(#Date)
RETURN #ResultVar
END
You can use it like this
Select dbo.DateToYYYYMMDD(GetDate())
Remeber,when calling a user function yoy MUST include the schema in this case dbo.
select convert(decimal, format(convert(datetime, '5/23/2014'),'yyyyMMdd'))
SELECT
CONVERT(NUMERIC,
CONCAT(
LEFT('23/05/2014',2)
,RIGHT(LEFT('23/05/2014',5),2)
,RIGHT('23/05/2014',4)
)
)
The CONVERT(NUMERIC part may/not be necessary depending on how the value will be used.