Convert TimeStamp to Teradata TimeStamp - teradata

I have a table which contains DateTime in the following format
'MM/DD/YYYY HH:MI'
now I am trying to convert this time into Teradata Timestamp but getting an error.
What I have tried is:
select cast('4/13/2022 0:00' AS TIMESTAMP(0) Format 'mm/dd/yyyyBhh:mi')
but it return invalid timestamp error.
Any method for this?
Best regards.

You've got single digit month and hour values in there. I don't think there's any way to make Teradata play nice with those, other than adding the leading 0s.
I think this should work but you'll want to test it thoroughly.
select cast (regexp_replace('4/13/2022 0:00', '\b([0-9])\b', '0\1') AS TIMESTAMP(0) Format 'mm/dd/yyyyBhh:mi')

after different solutions, this worked.
CASE WHEN CHAR_LENGTH( STRTOK(ACT_TIME, '/', 1)) = 1 THEN TO_TIMESTAMP( '0' || ACT_TIME, 'MM/DD/YYYY HH24:MI') END ACT_TIME_TIMESTAMP
,CASE WHEN CHAR_LENGTH( STRTOK(ACT_TIME, '/', 1)) = 1 THEN CAST(TO_TIMESTAMP( '0' || ACT_TIME, 'MM/DD/YYYY HH24:MI') AS DATE) END ACT_TIME_DATE
,CASE WHEN CHAR_LENGTH( STRTOK(ACT_TIME, '/', 1)) = 1 THEN CAST(TO_TIMESTAMP( '0' || ACT_TIME, 'MM/DD/YYYY HH24:MI') AS TIME) END ACT_TIME_DATE

Related

SQL: Sum based on specified dates

Thanks again for the help everyone. I went with the script below...
SELECT beginning, end,
(SELECT SUM(sale) FROM sales_log WHERE date BETWEEN beginning AND `end` ) AS sales
FROM performance
and I added a salesperson column to both the performance table and sales_log but it winds up crashing DB Browser. What is the issue here? New code below:
SELECT beginning, end, salesperson
(SELECT SUM(sale) FROM sales_log WHERE (date BETWEEN beginning AND end) AND sales_log.salesperson = performance.salesperson ) AS sales
FROM performance
I believe that the following may do what you wish or be the basis for what you wish.
WITH sales_log_cte AS
(
SELECT substr(date,(length(date) -3),4)||'-'||
CASE WHEN length(replace(substr(date,instr(date,'/')+1,2),'/','')) < 2 THEN '0' ELSE '' END
||replace(substr(date,instr(date,'/')+1,2),'/','')||'-'||
CASE WHEN length(substr(date,1,instr(date,'/') -1)) < 2 THEN '0' ELSE '' END||substr(date,1,instr(date,'/') -1) AS date,
CAST(sale AS REAL) AS sale
FROM sales_log
),
performance_cte AS
(
SELECT substr(beginning,(length(beginning) -3),4)||'-'||
CASE WHEN length(replace(substr(beginning,instr(beginning,'/')+1,2),'/','')) < 2 THEN '0' ELSE '' END
||replace(substr(beginning,instr(beginning,'/')+1,2),'/','')||'-'||
CASE WHEN length(substr(beginning,1,instr(beginning,'/') -1)) < 2 THEN '0' ELSE '' END||substr(beginning,1,instr(beginning,'/') -1)
AS beginning,
substr(`end`,(length(`end`) -3),4)||'-'||
CASE WHEN length(replace(substr(`end`,instr(`end`,'/')+1,2),'/','')) < 2 THEN '0' ELSE '' END
||replace(substr(`end`,instr(`end`,'/')+1,2),'/','')||'-'||
CASE WHEN length(substr(`end`,1,instr(`end`,'/') -1)) < 2 THEN '0' ELSE '' END||substr(`end`,1,instr(`end`,'/') -1)
AS `end`
FROM performance
)
SELECT beginning, `end` , (SELECT SUM(sale) FROM sales_log_cte WHERE date BETWEEN beginning AND `end` ) AS sales
FROM performance_cte
;
From your data this results in :-
As can be seen the bulk of the code is converting the dates into a format (i.e. YYYY-MM-DD) that is usable/recognisable by SQLite for the BETWEEN clause.
Date And Time Functions
I don't believe that you want a join between performance (preformance_cte after reformatting the dates) and sales_log (sales_log_cte) as this will be a cartesian product and then sum will sum all the results within the range.
The use of end as a column name is also awkward as it is a KEYWORD requiring it to be enclosed (` grave accents used in the above).
The above works by using 2 CTE's (Common Table Expresssions), which are temporary tables who'd life time is for the query in which they are used.
The first sales_log_cte is simply the sales_log table but with the date reformatted. The second, likewise, is simply the performace table with the dates reformatted.
If the tables already has suitable date formatting then all of the above could simply be :-
SELECT beginning, `end` , (SELECT SUM(sale) FROM sales_log WHERE date BETWEEN beginning AND `end` ) AS sales FROM performance;

Sqlite sum time

I have a table with a column that contained time duration of events.
It it formatted as 'h:mm:ss'
I found the function strftime - but according to the manual, it requires the format 'hh:mm:ss'
can someone tell me how i can sum up the duration without recreating the sql table?
Is this what you want ?
with t as (
select '4:02:01' as v
union all
select '9:30:12'
union all
select '2:14:00'
),
diff as (
select sum(strftime('%s', '0'||v) - strftime('%s', '00:00:00')) as v
from t
)
select (v/3600) || ' hours, ' || (v%3600/60) ||' minutes, '
|| (v%60) || ' seconds.'
from diff
https://www.db-fiddle.com/f/2SNbFYQv2zYiCE4gw5bhzi/0
You can use time() and strftime():
select
time(sum(
strftime('%s', case length(timecolumn) when 7 then '0' else '' end || timecolumn)
- strftime('%s','00:00:00')),
'unixepoch') totaltime
from tablename
The result time sum will be in format hh:mm:ss.

PL/SQL Adding hours to timestamp parameter

I got parameter
:dateFrom
which gonna be used as an argument in a function as a TIMESTAMP. I need to add to :dateFrom + 7 hours, how can I do that?
If your parameter is not already a timestamp, use to_timestamp or to_date to convert it:
to_timestamp(dateFrom,'mm/dd/yyyy hh24:mi:ss')
(substitute the appropriate mask based on the format of your input parameter)
Then just add 7/24.
to_timestamp(dateFrom,'mm/dd/yyyy hh24:mi:ss') + 7/24;
Adding 1 adds a full day, so adding 1/24 adds 1 hour.
This can also be done with the INTERVAL operator:
to_timestamp(dateFrom,'mm/dd/yyyy hh24:mi:ss') + INTERVAL '7' hour
Here is some PL/SQL that will:
DECLARE
dateFrom TIMESTAMP;
BEGIN
dateFrom := SYSTIMESTAMP;
DBMS_OUTPUT.PUT_LINE('BEFORE :: ' || dateFrom);
dateFrom := dateFrom + INTERVAL '2' HOUR;
DBMS_OUTPUT.PUT_LINE('AFTER :: ' || dateFrom);
END;
/
Look into the INTERVAL operator.
You can do like this,
select dateFrom + interval '7' hours from dual
In the arithmetic of the dates, in Oracle, when you add a number, it is intended as NUMBER OF DAYS.
7 hours are 7/24 days, so you have simply to add 7/24.

how to extract only month from date which is in sqlite database?

I want to fetch few data that occur in particular month. For example, I need to choose all the names of employees who joined in July(irrespective of date). What is the query to choose particular month field alone from date field from database ? How do I compare the month field in database(stored in date of format mm/dd/yyyy) and the user given value of month. I'm using sqlite3 database and date field is set to text.
Thanks in advance.
SQLite only has a small set of date and time functions. You can use them like this:
sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE t1 (f1 string);
INSERT INTO "t1" VALUES('03/31/1970');
COMMIT;
sqlite> select substr(f1, 7) || '-' || substr(f1, 0, 3) || '-' || substr(f1, 4, 2) from t1;
1970-03-31
sqlite> select strftime("%m", substr(f1, 7) || '-' || substr(f1, 0, 3) || '-' || substr(f1, 4, 2)) from t1;
03
I Resolving this using this code in SQLITE.
If You have dat like this format : "2021-12-23 12:33:01"
then convert it in strftime() format function.
ex.-> Select strftime('%d/%m/%Y',EntryDate) as Date from table_name;
then output comes in
"2021-12-23 12:33:01" to "23-12-2021"
and then fire this query to get MONTH NAME from MONTH NUMBER in SQLITE
-> using case we can fetch it.
Select strftime('%d/%m/%Y',checkInDate) as Date,
case strftime('%m', checkInDate) when '01' then 'JAN'
when '02' then 'FEB' when '03' then 'MAR' when '04' then 'APR' when '05' then 'MAY' when '06' then 'JUN'
when '07' then 'JUL' when '08' then 'AUG' when '09' then 'SEP' when '10' then 'OCT'
when '11' then 'NOV' when '12' then 'DEC' else '' end as Month from AttendanceTable
☻♥ Done Keep Code.

Can you format 24-hour time string into 12-hour time string with AM/PM?

I store some time values in sqlite in %H:%M string format (for example "15:43"), but I would like to get them out formatted in 12 hour format with AM/PM indicators (for example "3:43 PM"). Is this possible with sqlite (and if so, how), or do I need to do this in my application code?
Unless you extend sqlite with your own custom function, you'll have to do this is code.
sqlite's strftime date formatting function only supports a small subset of its C counterpart, insufficient for your problem. sqlite also lacks a selection construct like IF or CASE, making simple if/else impossible.
Some pseudo code to help you on the way:
if (hourpart of time >= 12)
subtract 12 from hours
append string " pm"
else // hourpart < 12
append string " am"
end if
In SQL you can accomplish this using the CASE syntax.
After taking a closer look at the problem:
SELECT (CASE HOUR(myTimeColumn) >= 12 WHEN 1 THEN
((HOUR(myTimeColumn) - 12) + '-' + MINUTE(myTimeColumn) + ' pm')
ELSE
(HOUR(myTimeColumn) + '-' + MINUTE(myTimeColumn) + ' am')
AS AmPmTime,
someOtherColumn
FROM myTable
I'm not entirely sure that all of that is valid SQLite syntax, but you should be able to correct the bugs.
There are a few special situation that are covered here. I'm using 'now' as a source, but you can adjust it for your string:
select
CASE
--For 00:05, for example.
WHEN (strftime('%H', 'now', 'localtime') - 12) = -12
THEN '12:' || strftime('%M', 'now', 'localtime') ||' '|| 'AM'
--For 12:05, for example.
WHEN (strftime('%H', 'now', 'localtime') - 12) = 0
THEN '12:' || strftime('%M', 'now', 'localtime') ||' '|| 'PM'
--other am time
WHEN (strftime('%H', 'now', 'localtime') - 12) < 0
THEN strftime('%H', 'now', 'localtime') ||':'||
strftime('%M', 'now', 'localtime') ||' '|| 'AM'
ELSE
--other pm time
(cast(strftime('%H', 'now', 'localtime') as integer) - 12) ||':'||
strftime('%M', 'now', 'localtime') ||' '|| 'PM'
END here_you_go_usa;
Do it in your application. Store it in normal 24h format in the database. In the database it can be stored as a Date entry instead of a string (correct me if im wrong)
As PoweRoy recomended, this belongs in the application.
It is recommended that any kind of data stored of used in communication uses a standard, locale-insensitive format: http://www.mihai-nita.net/article.php?artID=20051025a
Here's a working one.. Thanks to Tomas
SELECT
PatientName,
CASE WHEN
StrFTime('%H', AppointmentTime) % 12 = 0 THEN 12
ELSE StrFTime('%H', AppointmentTime) % 12 END
|| ':' ||
StrFTime('%M', AppointmentTime)
|| ' ' ||
CASE WHEN
StrFTime('%H', AppointmentTime) > 12 THEN 'PM'
ELSE 'AM' END
`APP_TIME`
From Patients;
OUTPUT
Abdul Salim, 12:05 PM

Resources