Convert varchar(50) to Date Format 'yyyy-mm-dd' in teradata? - teradata

I have a column defined as Date format yyyy-mm-dd while creating.
I want to insert data from other table which has that column as varchar(50)
While selecting and inserting into the table I got this error
INSERT Failed. 2665: Invalid date.
Can someone help me in casting this?
INSERT INTO TEMP_TABLES.FACT
(
CUSTOMER_ACCOUNT_ID,
LOB_START_DATE,
)
SEL
CUSTOMER_ACCOUNT_ID,
I.start_date as LOB_START_DATE,
FROM #LOGIN I
left join JOURNEY_TABLE.DOTCOM_DIM d1
on I.PAGES = d1.PAGE_DESC
This is the example of date stored in varchar(50) field : 2014-04-03
Thanks in advance

In case it would be helpful, here's a query that should allow you to identify the rows with invalid dates:
select
*
from
#login t1
left outer join sys_calendar.calendar t2
on t1.start_date = cast (cast(t2.calendar_date as date format 'YYYY-MM-DD') as char(10))
where t2.calendar_date is null
Any rows that return from this query will have invalid dates.

I think all you need is the format statement
examples here
SEL
CUSTOMER_ACCOUNT_ID,
cast(((I.start_date (date, format 'yyyy-mm-dd'))(char(10))) as LOB_START_DATE,

In more recent versions of Teradata there is a TRYCAST() function. This function will attempt to cast the data and return NULL if the conversion fails, instead of failing the statement.
INSERT INTO TEMP_TABLES.FACT
(
CUSTOMER_ACCOUNT_ID,
LOB_START_DATE,
)
SELECT Customer_Account_ID
, TRYCAST(I.Start_Date AS DATE) AS LOB_START_DATE
FROM #LOGIN I
LEFT JOIN JOURNEY_TABLE.DOTCOM_DIM d1
ON I.Pages = d1.Page_Desc;

Related

Retrive date wise data from SQLITE database kotlin

I have one SQlite database where i store some data with date.
Now i want to get data date wise like:
Month wise - it means i pass the value like Current date is EndDate to this month 1st date.
Year wise - it means i want to get data 1st-april-20..previews to 31-march-20..current
Start and End date wise - hear is which i pass date.
For that i got one solution is HERE for java.
But i have no idea this HOW TO WORK. Anyone please explain me how to work this and How to i get data as i mention above. FOR KOTLIN
TABLE
db.createTable(customerDetail, true,
credit_id to INTEGER + PRIMARY_KEY + AUTOINCREMENT,
credit_type to TEXT,
c_id to TEXT,
credit_amount to TEXT,
credit_date to TEXT,
addFrom to TEXT
)
UPDATE
For Month wise data i'll try below query like:
"SELECT * FROM $customerDetail WHERE $credit_date BETWEEN strftime('%d-%m-%Y', '$startDate') AND strftime('%d-%m-%Y', '$currentDate')"
/*SELECT * FROM CustomerDetail WHERE credit_date BETWEEN strftime('%d-%m-%Y', '01/02/2019') AND strftime('%d-%m-%Y', '23/02/2019')*/
But it's give me arralistSize = 0.
After that i can write new query like:
"SELECT * FROM $customerDetail WHERE $credit_date BETWEEN '$startDate' AND '$currentDate'"
/*SELECT * FROM CustomerDetail WHERE credit_date BETWEEN '01/02/2019' AND '23/02/2019'*/
In this query data will return. But it's return all data without any filtering.
If anyone knows why this work like this please help me.
MY DATA LIST
Thanks in advance.
Solution :
Solution for MONTH wise
I just change date format "dd/mm/yyyy" TO "yyyy/mm/dd" and re insert all data.
AND Fire below QUERY :
"SELECT * FROM $customerDetail WHERE $credit_date BETWEEN '$startDate' AND '$currentDate'"
SQLite does not have a Date data type like other RDBMS's. It treats dates as TEXT.
So when it compares the credit_date column it actually does compare strings.
This would be fine if you stored your dates in the format YYYY-MM-DD and compared against the same format.
But if you store your dates in the format DD/MM/YYYY then you can't compare.
So the best solution would be to change the format of the column credit_date to YYYY-MM-DD.
If this is not possible then you have to transform the string value of the column like this:
substr(credit_date, 7, 4) || '-' || substr(credit_date, 4, 2) || '-' || substr(credit_date, 1, 2)
Now you can use this in a query like:
SELECT * FROM $customerDetail
WHERE substr($credit_date, 7, 4) || '-' || substr($credit_date, 4, 2) || '-' || substr($credit_date, 1, 2)
BETWEEN '$startDate' AND '$currentDate'
But you have to make sure that $startDate and $currentDate are also in the format YYYY-MM-DD

Join on time part is failing

I am facing an issue where i am not able to join two time columns:
The reported date is timestamp 0 with values like
4/1/2017 19:58:00
8/19/2017 19:58:00
Fault_Order_Submitted_Dt DATE FORMAT 'YYYY-MM-DD',
Fault_Order_Submitted_Tm INTEGER FORMAT '99:99:99',
sel a.completion_date, b.Fault_Order_Completed_Dt||b.fault_order_submitted_tm as Fault_time from
DG.LL_FMFTMX a
inner join DG.fault_order b
on a.fault_number=b.Fault_Order_Num
and cast(a.reported_date as date) =b.fault_order_submitted_dt
and cast(cast( a.reported_date as time(0) ) as integer format '99:99:99') = b.fault_order_submitted_tm
where fault_status='P'
The Join part on time is failing with invalid operation on date/time.
Please suggest.
To get the time portion as HHMISS integer you need to cast it to a string first:
Cast(To_Char(reported_date, 'hhmiss') AS INT)

How can i convert datetime to date format in sqlite selected vaues

I just try to convert datetime to date, but without success. Help me to do it:
select date('startTime') from usertable ;
select datetime(substr(startTime, 7, 4 )) from usertable;
Just try this.. it will convert datetime to date.
SELECT strftime('%d-%m-%Y', fieldname)
SELECT strftime('%d-%m-%Y', 'now') gives output 5-06-2015

How to compare various date formats stored as nvarchar in a column with the getdate() in SQL Server

I am using SQL Server 2008 R2
When examined the values in the table, I found that few values are stored as 2015-03-20T06:06:46 and few values are stored as 11/25/2014. So now how to compare these both values in where clause with getdate()
SELECT B.Value
FROM table1 A WITH (NOLOCK)
INNER JOIN table1 B WITH (NOLOCK) ON A.id = B.id
WHERE
A.Name = 'COMPLETED_AT'
AND CONVERT(smalldatetime, A.Value) < GETDATE() - 30
AND B.Name = 'RESULT'
Getting error message
Conversion failed when converting character string to smalldatetime data type
when executing the above query
Example table structure
ID Name Value
1 Result R12344
1 Completed_At 2015-03-20T06:06:46
2 Result R23445
2 Completed_At 2014-03-20T06:06:46
3 Result R83261
3 Completed_At 11/25/2014
Column value is of nvarchar(400) datatype
The query result should display the values of result name type which have been made an entry of more than 30 days.
Looking forward in hearing from you.
Finally got answer to my query by myself after going through the link
https://msdn.microsoft.com/en-us/library/ms187928.aspx
CONVERT(nvarchar(400),A.value,126) < CONVERT(nvarchar(400),GETDATE()-30,126)
When i have used the above value at the where clause i didnt get any error and also the result is also correct

Get count on a joined tables

I have two tables(oracle):
(I have marked the primary keys with a star before the column name)
Table1 Columns are :
*date,
*code,
*symbol,
price,
weight
Table2 columns are :
*descriptionID
code
symbol
date
description
I need to find the below information using query,
For a given code and a symbol on a particular day,is there any description.
for example: code = "AA" and symbol = "TEST" on 2012-4-1 on Table 1 => is there atleast one row like ID=, code ="AA", symbol ="TEST" ,date = 2012-4-1 in table 2
I tried with the below query:
select * from Table1 t1 INNER JOIN
Table2 t2
on t1.code = t2.code and t1.symbol = t2.symbol and
TO_CHAR(t1.date, 'YYYY/MM/DD') = TO_CHAR(t1.date, 'YYYY/MM/DD')
But it doesnt give me output like:
code = AA, symbol = TEST, date 2012-4-1 => descrition count = 10
code = AA, symbol = TEST, date 2012-4-2 => descrition count = 5
code = BB, symbol = HELO, date 2012-4-1 => descrition count = 20
Can some one suggest me a query which can achieve the above output.
I don't see why you need the join:
SELECT count(*)
FROM Table2
WHERE code='AA'
AND symbol = 'TEST'
AND date = to_date('2012-04-01', 'yyyy-mm-dd')
UPDATE: (after reading your comment)
I still don't see why you need the join. Do you need some data from table1 ?
Anyway, if you want the count for all the (code,symbol,date)s then why not group by ?
As for the dates, better use trunc to get rid of the time parts.
So:
SELECT code, symbol, date, count(*)
FROM Table2
GROUP BY code, symbol, date
the Trunc() Method takes a String\Date input and Creates a DATE output that is in this Format: "DD\MM\YYYY".
So Its should do exactly what you want.

Resources