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

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.

Related

Convert TimeStamp to Teradata TimeStamp

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

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.

save datetime value 12 in Oracle database

When I try to insert datetime value 12:58 AM into the oracle table it gets inserted as 00:58. How can I insert datetime value as 12 in my oracle db? I've set my Oracle time format as 24 hr time. Any suggestions would help.
Insert statement :
INSERT INTO TABLE
(
DATE_CREATED,
PLANNED_START,
PLANNED_COMPLETION
)
VALUES
(
sysdate,
TO_CHAR(p_planned_Start_Date, 'DD-MM-YYYY HH24:MI:SS'),
TO_CHAR(end_date_, 'DD-MM-YYYY HH24:MI:SS')
);
For the 24-hour time, you need to use HH24 instead of HH.
For the 12-hour time, the AM/PM indicator is written as A.M. (if you want periods in the result) or AM (if you don't). For example:
INSERT INTO TEST (LD_DATE) Values (TO_DATE('08/30/2016', 'MM/DD/YYYY '));
And select it as below:
SELECT LD_DATE,
TO_CHAR(LD_DATE, 'DD-MM-YYYY HH24:MI:SS') "Date 24Hr",
TO_CHAR(LD_DATE, 'DD-MM-YYYY HH:MI:SS AM') "Date 12Hr"
FROM test
;

Correctly looping through a data set, and displaying, or outputting all results, WITHOUT using a Temporary Table

I have some PL/SQL code that is intended to loop through the entire month range for each month in each year, and then retrieve the number of times something occurred within a monthly period.
Right now, I'm not doing a nested loop for each year that exists because I need to properly understand how this works before continuing.
Here's my code:
BEGIN
FOR i IN 1..11 LOOP
BEGIN
SELECT COUNT(UNIQUE EMPLOYEE) as Emp FROM (SELECT DATE_COL, EMPLOYEE
FROM CORE.DATE_TEST
WHERE DATE_COL >= TO_DATE(i || '/1/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS')
AND DATE_COL < TO_DATE((i + 1) || '/1/2015 23:59:59', 'MM/DD/YYYY HH24:MI:SS')
ORDER BY DATE_COL ASC);
END;
END LOOP;
COMMIT;
END;
And I'm getting this error: PLS-00428: an INTO clause is expected in this SELECT statement
Obviously, I need to select the results into something, but what that something is, I'm not quite sure. This may not even be the right way to do it.
However, I'd like to take all of these results, and combine them into a single table like this:
+----------+-------+
| date | count |
+----------+-------+
| January | 200 |
+----------+-------|
| February | 100 |
+----------+-------|
| March | 500 |
+----------+-------|
And so on.
EDIT: There are no priviledges to create a TABLE, or even a GLOBAL TEMPORARY TABLE. This needs to be done without temporary tables.
This will print your query.
DECLARE
V_number varchar2(100);
BEGIN
FOR i IN 1..11 LOOP
SELECT COUNT(UNIQUE EMPLOYEE) AS EMP INTO V_number FROM (SELECT CURRENT_DATE, EMPLOYEE
FROM CORE.DATE_TEST
WHERE CURRENT_DATE >= TO_DATE(i || '/1/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS')
AND CURRENT_DATE < TO_DATE((i + 1) || '/1/2015 23:59:59', 'MM/DD/YYYY HH24:MI:SS')
ORDER BY CURRENT_DATE ASC);
dbms_output.put_line(TO_CHAR(TO_DATE(I || '/1/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 'Month')||' '||V_NUMBER);
END LOOP;
END;

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