Translate Teradata DATE function (division/extract and sum) into BigQuery - sql

I have this code in Teradata that reads "x_date/100+190000". So from my understanding it removes the 'day' portion from DATE and then adds an INT number of days. Now I have to translate the same into BigQuery but can't see how.
edit: so what I have is a SELECT statement that includes the "x_date" field, which has a DATE format. It contains a list of dates in the form of 'yyyy-mm-dd'. The query reads something like:
SELECT x_date/100+190000
FROM x_table
and the field has this sort of rows:
| '2022-06-06' |
| '2020-03-06' |
| '2019-09-01' |
| '2028-05-06' |
What I don't understand exactly is what this functions are doing in Teradata.
My expected output should be in DATE format and should be copying (in BigQuery), whatever the Teradata function is doing to the field.

Use below
SELECT FORMAT_DATE('%Y%m', x_date)
FROM x_table

Related

How to get different date formats in Oracle DB from a select query existing in a table?

In my Oracle DB, I have a date field called HIGH_DATE. The format for some entries is "27-SEP-12" (DD-MON-YY) and for some entries it is "27-09-12" (DD-MM-YY).
Can someone help me in framing a select query through which I can get dates in either formats??
If you have a DATE column then it does not have any format; it is stored internally as 7-bytes (century, year-of-century, month, day, hour, minute, second) and it is only when the user interface being used to access the database returns data to the user that it then gets formatted (and all the dates will be implicitly converted to strings with a consistent format).
I'm going to assume that when you say:
I have a date field called "HIGH_DATE"
What you actually mean is: "I have a column with a VARCHAR2 data-type where I store date values".
If that is the case then all you need to do is:
SELECT TO_DATE( high_date, 'DD-MM-RR' ) AS high_date
FROM table_name;
Oracle's string-to-date conversion rules will match additionally the MON format model if you use the MM format model and don't specify an exact match using the FX format model.
If you have the test data:
CREATE TABLE table_name ( high_date ) AS
SELECT '23-09-20' FROM DUAL UNION ALL
SELECT '15-AUG-99' FROM DUAL;
Then the above query will output (depending on your NLS_DATE_FORMAT):
| HIGH_DATE |
| :------------------ |
| 2020-09-23T00:00:00 |
| 1999-08-15T00:00:00 |
db<>fiddle here
However, the best solution is going to be to stop storing the values as strings and to store them (without a format) as a date.

SAS internal Date format to yyyy-MM-dd in HIVE

I got SAS dataset into txt file format from client. But client didnt change the SAS date format to mm/dd/yyyy or yyyy-MM-dd. As SAS uses seconds since Jan 1, 1960, Date is coming like:
| response_dt |
+----------------
| 19724 |
| 19673 |
| 19698 |
| 19738 |
| 19738 |
I want to convert this to yyyy-MM-dd format in hive. Kindly help
Just read in the numbers. They are indeed days since Jan 1, 1960.
Then assign a format to them in one of the several ways you can, like
Data myData;
set myData;
format response_dt ddmmyy8.;
run;
If the dataset is huge, consider using proc datasets
Sas dates are days since 1jan1960.
i made this a wiki.
Can anyone add the correct function to add days to a date in hive?

Changing the format of data in a column

Trying the change the date column from YYYYMMDD to MMDDYYYY while maintaining varchar value. Currently my column is set as varchar(10). Is there a way to change the strings in mass numbers because I have thousands of rows that need the format converted.
For example:
| ID | Date |
------------------------
| 1 | 20140911 |
| 2 | 20140101 |
| 3 | 20140829 |
What I want my table to look like:
| ID | Date |
------------------------
| 1 | 09112014 |
| 2 | 01012014 |
| 3 | 08292014 |
Bonus question: Would it cause an issue while trying to convert this column if there is data such as 91212 for 09/12/2012 or something like 1381 which is supposed to be 08/01/2013?
Instead of storing the formatted date in separate column; just correct the format while fetching using STR_TO_DATE function (as you said your dates are stored as string/varchar) like below. Again, as other have suggested don't store date data as string rather use the datetime data type instead
SELECT STR_TO_DATE(`Date`, '%m/%d/%Y')
FROM yourtable
EDIT:
In that case, I would suggest don't update your original table. Rather store this formatted data in a view or in a separate table all together like below
create view formatted_date_view
as
SELECT ID,STR_TO_DATE(`Date`, '%m/%d/%Y') as 'Formatted_Date'
FROM yourtable
(OR)
create table formatted_date_table
as
SELECT ID,STR_TO_DATE(`Date`, '%m/%d/%Y') as 'Formatted_Date'
FROM yourtable
EDIT1:
In case of SQL Server use CONVERT function like CONVERT(datetime, Date,110). so, it would be (Here 110 is the style for mm-dd-yyyy format)
SELECT ID,convert(datetime,[Date],110) as 'Formatted_Date'
FROM yourtable
(OR)
CAST function like below (only drawback, you can't use any specific style to format the date)
SELECT ID, cast([Date] as datetime) as 'Formatted_Date'
FROM yourtable
MS SQL Server Solution:
Which SQL are you trying with?
MSSQL Server 2008 R2
You can use Convert function on your date field. You have to specify the date's format Style.
For mm/dd/yyyy format Style value is 101.
Using with style value, your update statement can be:
UPDATE table_name
SET date = CONVERT( VARCHAR, date, 101 )
Refer To:
How to format datetime & date in Sql Server
SQL Server 2008 Date Format
Demo # MS SQL Server 2008 Fiddle
MySQL Solution:
it needs to stay in varchar or int and the dates are yyyymmdd and I need to change thousands of rows of data to be in mmddyyyy format.
Change to date type using str_to_date and then change again to string using date_format.
UPDATE table_name
SET date = DATE_FORMAT( STR_TO_DATE( date, '%Y%m%d' ), '%m%d%Y' )
The value 20140911 when converted from yyyymmdd to mmddyyyy format, will retain the leading 0 as 09112014.
Bonus question: Would it cause an issue while trying to convert this column if there is data such as 91212 for 09/12/2012 or something like 1381 which is supposed to be 08/01/2013
You can use str_to_date( '91212', '%c%e%y' ) to convert the same to valid date object. But MySQL, though defines to support single digit month and date numbers, it won't parse such date correctly and returns a NULL on such formats.
mysql> select str_to_date( '91212', '%c%e%y' ) s1, str_to_date( '091212', '%c%e%y' ) s2;
+------+------------+
| s1 | s2 |
+------+------------+
| NULL | 2012-09-12 |
+------+------------+
1 row in set, 1 warning (0.00 sec)
mysql> show warnings;
+---------+------+------------------------------------------------------------+
| Level | Code | Message |
+---------+------+------------------------------------------------------------+
| Warning | 1411 | Incorrect datetime value: '91212' for function str_to_date |
+---------+------+------------------------------------------------------------+
1 row in set (0.00 sec)

Select between varchar as a date returns null

I have a table attendance_sheet and it has column string_date which is a varchar.
This is inside my table data.
id | string_date | pname
1 | '06/03/2013' | 'sam'
2 | '08/23/2013' | 'sd'
3 | '11/26/2013' | 'rt'
I try to query it using this range.
SELECT * FROM attendance_sheet
where string_date between '06/01/2013' and '12/31/2013'
then it returns the data.. but when I try to query it using this
SELECT * FROM attendance_sheet
where string_date between '06/01/2013' and '03/31/2014'
it did not return any results...
It can be fixed without any changing the column type for example the string_date which is a varchar will be changed into a date?
Does anyone has an Idea about my case?
any help will be appreciated, thanks in advance ..
Use strftime
SELECT * FROM attendance_sheet
where strftime(string_date,'%m/%d/%Y') between '2013-06-01' and '2013-31-21'
The reason this:
where string_date between '06/01/2013' and '03/31/2014'
does not return any results is that '06' is greater than '03'. It's essentially the same as using this filter.
where SomeField between 'b' and 'a'
The cause of this problem is a poorly designed database. Storing dates as strings is a bad idea. Juergen has shown you a function that might help you, but since your field is varchar, values like, 'fred', 'barney', and 'dino' are perfectly valid. The Str_to_date() function won't work very well with those.
If you are able to change your database, do so.
To be able to do string comparisons correctly, you have to change your database so that the most significant field of the date comes first, i.e., use the format yyyy-mm-dd.

Numbers to Dates in SQL Where Statement

I have a table with 2 columns, EffectiveBeginDate and EffectiveEndDate. However, they're in a number YYYYMMDD format so for example:
Agent | EffectiveBeginDate | EffectiveEndDate
John Doe | 20080922 | 20080924
Mark Smith| 20100922 | 20110226
etc. etc.
I need to do 2 things. First, I need to have a formula to change the EffectiveBegin/End dates to an actual date format in my Select statement.
More importantly, I need to be able to query this column in my where statement as if they were acutal dates b/c the where clause is going to be something like:
Where EffectiveStartDate between '01/01/2010' and '01/31/2010'
Thoughts?
In SQL SERVER it is sufficient to perform CONVERT(DATETIME, dateTimeField) when dateTimeField is in the format "yyyymmdd".
Update:
For numeric dates, use this:
CONVERT(DATETIME, CONVERT(char(8),dateTimeField) )