Query teradata to hive - sql

I have this teradata query, but when I run it on hive it doesn't support it
CAST(DATE '1900-01-01'+CAST( 999999999 - TRIM(BASM_DATE) AS INTEGER)
AS DATE) AS BASM_DATE
Error
Error while compiling statement: FAILED: SemanticException line
0:undefined:-1 Wrong arguments 'BASM_DATE': No matching method for class
org.apache.hadoop.hive.ql.udf.generic.GenericUDFOPDTIPlus with (date,
int)
can you guys tell me which part to fix and what the query will be?
Thank You.

You need to use date_add(dt,num).
Pls use below SQL -
date_add( '1900-01-01', 999999999 - cast( TRIM(BASM_DATE) as INT) ) AS BASM_DATE
I assumed BASM_DATE is a string column and using TRIM you are trying to remove trailing or leading spaces.

The Teradata SQL is relying on two implicit conversions that happen around the TRIM.
First the BASM_DATE is implicitly cast to VARCHAR using the default format, then TRIM is applied, then that string is implicitly cast to FLOAT followed by a floating-point subtraction, with that result explicitly cast to INTEGER. Then that number of days is added to the 1900-01-01 date, followed by a redundant CAST from DATE to DATE. (There's really no reason to use FLOAT here, that's just the type Teradata uses for implicit numeric conversions.) A better, clearer choice in Teradata that gives the same result would have been:
DATE'1900-01-01'+(999999999 - CAST(TO_CHAR(BASM_DATE,'YYYYMMDD') AS INTEGER) AS BASM_DATE
So the Hive equivalent would be:
date_add('1900-01-01',999999999 - cast(date_format(BASM_DATE,'yyyyMMdd') AS INT)) AS BASM_DATE
or it may be more efficient to use
date_add('1900-01-01',999999999 - year(BASM_DATE)*10000 - month(BASM_DATE)*100 - day(BASM_DATE)) AS BASM_DATE

Related

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 Server query - Correction in syntax

Could you please help me to correct the syntax ?
I have to write below code in SQL server.
This is working perfectly fine in Oracle database.
Select to_number(substr((((END_TS - BEGIN_TS)*(24*60*60))),1,10)) AS EXECUTION_TIME
from TABLE B
Also END_TS and BEGIN_TS are of datetime datatypes.
In SQL Server math can not be performed directly on dates, as it can in Oracle. You need to apply a datediff function to calculate the difference before you manipulate it:
select convert(numeric(10,9),left(datediff(second,begin_ts,end_ts)/(24.0*60*60),10)) from table;
Note that the expression in the divisor needs to have a floating point number in it (hence the ".0") otherwise the result is rounded to an integer.
After performing the date calculation, the left function is the equivalent of the substring in Oracle. It converts to a varchar then takes the first 10 characters. Convert then returns to a numeric, which is the equivalent of Oracle's variable-length number. It is necessary to tell convert that you expect digits after the decimal, otherwise it will round.
The substring for the first 10 characters has a bad smell, I would leave it out. This snippet does the calculation without restricting to the first ten characters.
select datediff(second,begin_ts,end_ts)/(24.0*60*60) from table;
Also note that the Oracle version provides fractional dates. If you only wanted the whole day then use "day" as the datepart parameter to datediff.

Convert INT to VARCHAR SQL

I am using Sybase and I am doing a select which returns me a column called "iftype", but its type is int and I need to convert into varchar. When I try to do the select without the convert function I get this error:
Error code 257, SQL state 37000: Implicit conversion from datatype 'VARCHAR' to 'INT' is not allowed. Use the CONVERT function to run this query.
I dont know how to implement the function CONVERT. Can anyone help me, please ?
Use the convert function.
SELECT CONVERT(varchar(10), field_name) FROM table_name
Use the STR function:
SELECT STR(field_name) FROM table_name
Arguments
float_expression
Is an expression of approximate numeric (float) data type with a decimal point.
length
Is the total length. This includes decimal point, sign, digits, and spaces. The default is 10.
decimal
Is the number of places to the right of the decimal point. decimal must be less than or equal to 16. If decimal is more than 16 then the result is truncated to sixteen places to the right of the decimal point.
source: https://msdn.microsoft.com/en-us/library/ms189527.aspx
You can use CAST function:
SELECT CAST(your_column_name AS varchar(10)) FROM your_table_name
Actually you don't need to use STR Or Convert. Just select 'xxx'+LTRIM(ColumnName) does the job.
Possibly, LTRIM uses Convert or STR under the hood.
LTRIM also removes need for providing length. It seems to be working for integer or float without worry of truncation.
SELECT LTRIM(ColumnName) FROM TableName
also, LTRIM is better than STR as
SELECT STR(1234567890.123)
gives 1234567890
whereas
SELECT LTRIM(1234567890.123)
gives 1234567890.123
SELECT Cast(Cast([field_name] AS BIGINT) AS NVARCHAR(255))
FROM table_name
CONVERT(DATA_TYPE , Your_Column) is the syntax for CONVERT method in SQL. From this convert function we can convert the data of the Column which is on the right side of the comma (,) to the data type in the left side of the comma (,) Please see below example.
SELECT CONVERT (VARCHAR(10), ColumnName) FROM TableName

Errors while trying to cast/convert VARCHAR to DATETIME in ANSI SQL

I have a column in a table where timestamps have been stored in VARCHAR format, but I need to compare these against a column of DATETIME values from another table to find time intervals, so I want to either cast or convert the VARCHAR timestamps to DATETIME. However, both casting and converting are giving me problems.
The format of the VARCHAR timestamp looks like this: "29/07/2012 01:53:36 +12".
Using the query:
SELECT CAST(event_timestamp AS datetime) FROM the_table
produces ERROR: date/time field value out of range: "29/07/2012 01:53:36 +12".
Using the query:
SELECT CONVERT(datetime, event_timestamp, 131) from the_table;
produces
ERROR: syntax error at or near ","
LINE 1: select CONVERT(datetime, event_timestamp, 131) from the_tab...
^ (note: this is pointing at the first comma).
The error with CONVERT actually happens even if you use a generic function such as getdate() for the data source. This db uses ANSI SQL-92 (or so I'm told). Could anyone please help me out with this?
This seems really painful, but the following should work:
select dateadd(hh, cast(right(tv, 3) as int),
CONVERT(datetime, left(tv, 10), 103)+CONVERT(datetime, substring(tv, 12, 8), 108)
)
from (select '29/07/2012 01:53:36 +12' as tv) t
I've never added datetime's before, but this just worked on SQL Server 2008.
Why can't SQL Server just support a flexible notation built around yyyy, mm, mmm, dd and so on?
The actual database is Aster Data, which is based on Postgres (as are most recent database engines). In this database, you would use to_timestamp(). See the documentation here http://www.postgresql.org/docs/8.2/static/functions-formatting.html. The call would be something like:
to_timestamp(val, 'MM/DD/YYYY HH:MI:SS tz') -- not sure if this gets the +12
There are no ANSI functions for date conversion, so each database does its own. Even string functions vary among databases (substr? substring? charindex? instr? location?), so there is no ANSI way to do this.
You are using the wrong syntax, try:
CONVERT(varchar(X), datetimeValue, 131)
Where X is the total number of characters desired.
You will then be able to search for a match with datetimeValue and event_timestamp, assuming each value share the same structure. This will allow you to match string against string.
If I'm not mistaken the standard (ANSI SQL) CAST operator always expect time/date/timstamp literals in ISO format ('YYYY-MM-DD')
But according to the manual for Teradata V12 (can't test it), the format of the CAST operator is
CAST(character_expression AS TIMESTAMP timestamp_data_attribute)
with date_data_attribute being a character value plus an optional FORMAT specifier.
So in your case this would probably be:
cast(event_timestamp AS TIMESTAMP FORMAT 'MM/DD/YYYY HH:MI:SS Z');
I'm not entirely sure about the format definition though. You'll probably need to adjust that
Btw: CONVERT isn't a standard SQL function. It's SQL Server specific.

mysql datetime comparison

For example the following query works fine:
SELECT *
FROM quotes
WHERE expires_at <= '2010-10-15 10:00:00';
But this is obviously performing a 'string' comparison - I was wondering if there was a function built in to MySQL that specifically does 'datetime' comparisons.
...this is obviously performing a 'string' comparison
No - if the date/time format matches the supported format, MySQL performs implicit conversion to convert the value to a DATETIME, based on the column it is being compared to. Same thing happens with:
WHERE int_column = '1'
...where the string value of "1" is converted to an INTeger because int_column's data type is INT, not CHAR/VARCHAR/TEXT.
If you want to explicitly convert the string to a DATETIME, the STR_TO_DATE function would be the best choice:
WHERE expires_at <= STR_TO_DATE('2010-10-15 10:00:00', '%Y-%m-%d %H:%i:%s')
But this is obviously performing a 'string' comparison
No. The string will be automatically cast into a DATETIME value.
See 11.2. Type Conversion in Expression Evaluation.
When an operator is used with operands of different types, type conversion occurs to make the operands compatible. Some conversions occur implicitly. For example, MySQL automatically converts numbers to strings as necessary, and vice versa.
I know its pretty old but I just encounter the problem and there is what I saw in the SQL doc :
[For best results when using BETWEEN with date or time values,] use CAST() to explicitly convert the values to the desired data type. Examples: If you compare a DATETIME to two DATE values, convert the DATE values to DATETIME values. If you use a string constant such as '2001-1-1' in a comparison to a DATE, cast the string to a DATE.
I assume it's better to use STR_TO_DATE since they took the time to make a function just for that and also the fact that i found this in the BETWEEN doc...