Teradata: how to get greatest DATE in a row? - sql

I'm trying to get maximum date in a row. Both fuctions, MAX and Greatest return errors:
SEL Max(date1,date2,date3...)
SELECT Failed. 3706: Syntax error: expected something between a string or a Unicode character literal and ','.
SEL Greatest(date1,date2,date3...)
SELECT Failed. 9881: Function 'GREATEST' called with an invalid number or type of parameters
How to solve this?
thx

Yep, it's stupid, LEAST and GREATEST don't work with date/time (fixed in 16.10).
As a workaround you can cast it to integer:
SEL cast(GREATEST(cast(date1 as int)
,cast(date2 as int)
,cast(date3 as int)
...) as date)
Hopefully there's no NULL, otherwise it gets ugly with additional COALESCEs/NULLIF

According to documentation, arguments for the GREATEST function can't be dates. Try to convert them to strings in the YYYYMMDD (or similar) format (so that the result wouldn't suffer from issues when strings are being sorted).

Try this :
SELECT (
SELECT MAX(maxdate)
FROM (
VALUES (date1)
,(date2)
,(date3)
) AS maximumdate(maxdate)
) AS maxdate
FROM #temp

In TD 16.x0, GREATEST/LEAST work both with dates and timestamps. However users may need to add database, like it was UDF function:
SELECT TD_SYSFNLIB.LEAST(CURRENT_TIMESTAMP(0),ADD_MONTHS(CURRENT_TIMESTAMP(0),2))

Related

Select a date in a string and convert it to datetime

I have a string like:
'SPY US 03/20/20 P45'
I want to select just the date from the string.
My current query is:
Select Ticker, SUBSTRING(Ticker, PATINDEX('%[0-9][0-9]/[0-9][0-9]/[0-9][0-9]%',o.Ticker),8) AS 'myDate' FROM TABLE
This returns: 'SPY US 03/20/20 P45', 03/20/20
I want myDate to be in datetime.
I have tried various morphs of cast and convert, but they fail, presumably because they want the format to be in YYYY-MM-DD rather than MM/DD/YY.
My "smartest" attempt to convert was this:
CONVERT(DATETIME, SUBSTRING(o.Ticker, PATINDEX('%[0-9][0-9]/[0-9][0-9]/[0-9][0-9]%',o.Ticker),8),1)
After reading style guidelines here: https://www.w3schools.com/sql/func_sqlserver_convert.asp
but it still failed.
The ideal end-format for the date would be YYYY-MM-DD
Edited to add:
I have been fiddling with it and realized that I over simplied my question. The convert works if I just test it on a string, but the entire query involves several joins.
As I can understand you are looking for something like this.
You can use string_split() function to split string with blank space and then use try_cast() function to check each value whether it is a date.
declare #string as varchar(120) = 'SPY US 03/20/20 P4'
; with cte as (select
value
from string_split (#string, ' ')
)Select value from cte where try_cast (value as datetime) is not null
Live db<>fiddle demo.
So it turns out that there were a few entries in the column that didn't have friendly date format, so the patindex clause was returning nonsense.
For some reason that caused the entire operation to fail(rather than just returning null on the few entries that were failing).
Once I selected the entire (ultimately more complicated join statement) into a temp table, then I was able to try_convert the substring into a date and run my operations.

DATE_FROM_UNIX_DATE and UNIX_DATE both return errors on Google BigQuery

The query I'm running as a test is:
SELECT
UNIX_DATE(created_utc)
FROM `fh-bigquery.reddit_comments.2017_08`
But I keep getting this error:
Error: No matching signature for function UNIX_DATE for argument types:
INT64. Supported signature: UNIX_DATE(DATE) at [2:3]
I checked the datatype for the created_utc field and it's an integer. Casting and whatnot won't work either.
Would really appreciate any help. Thanks!
You should use TIMESTAMP_SECONDS() instead
#standardSQL
SELECT
TIMESTAMP_SECONDS(created_utc)
FROM `fh-bigquery.reddit_comments.2017_08`
LIMIT 5
Then you can use DATE() if you need date only
DATE(TIMESTAMP_SECONDS(created_utc))
UNIX_DATE() takes a String.
And DATE_FROM_UNIX_DATE() takes an INT64. SQL has a legacy problem of thinking of time ("date") in DAYS and not SECONDS like Unix. Thus:
SELECT DATE_FROM_UNIX_DATE(CAST(created_utc/86400 as INT64))
FROM `fh-bigquery.reddit_comments.2017_08`

SQL: Casting text to date

I have a TEXT column of dates and need to convert them to dates, but the two methods I'm using are not working correctly. See below.
SELECT CAST("12/01/2009" as date);
12
This only returns the first digit before stoping at the '/'.
SELECT DATE("12/01/2009");
Returns nothing
I also tried CONVERT, but I'm using SQLite and it doesn't appear to support it as I'm getting a syntax error. Any suggestions on how to solve this?
Try using STR_TO_DATE function
SELECT STR_TO_DATE('12/01/2009','%m/%d/%Y');
SQL FIDDLE DEMO
SqLite doesn't have date type. You need to do string manipulation do achieve this.
SELECT CAST('2009-01-12' AS DATE);
Use it.
It returns 2014-02-28

How to convert from CHAR to TIME in postgresql

I have a table with a column defined as time CHAR(6) with values like '18:00' which I need to convert from char to time.
I searched here, but didn't succeed.
You can use the :: syntax to cast the value:
SELECT my_column::time
FROM my_table
If the value really is a valid time, you can just cast it:
select '18:00'::time
As said, you could use :: to cast, but you could also use the standard CAST() function:
SELECT CAST(my_column AS time) AS my_column_time
FROM my_table;
This also works in other databases, not just PostgreSQL.

Cast(to_number(to_char())) not returning second values

I am trying to convert a timestamp to a number but it is failing to return second values.
when looking at the date 2012/01/04 00:00:21 I am expecting the below code to return for example 20100104000021.
Instead I am getting 20100104000000 with the 21 at the end being converted to 00. This is happening for all records and I am unsure why.
to_number(TO_CHAR(p_timestamp,'YYYYMMDDHH24MISS'))
Could anyone advise?
EDIT: I have removed CAST however I am still encountering the same issue.
You should either use To_Number function:
select To_Number(To_Char(p_timestamp, 'YYYYMMDDHH24MISS'))
from MyTable
or cast to appropriate Number:
-- change Number(20) to required type
select Cast(To_Char(p_timestamp, 'YYYYMMDDHH24MISS') as Number(20))
from MyTable
P.S. (Side note): use VarChar2 instead of VarChar in Oracle
The CAST function is not correctly used. You can drop it, as:
SELECT to_number(TO_CHAR(p_timestamp,'YYYYMMDDHH24MISS')) FROM YourTable;