how to get today date in YYYYMMDD in firebird, I had a look on following but could not figured how to write this.
I think you can do:
select replace(cast(cast('Now' as date) as varchar(10)), '-', '')
from rdb$database
IN FIREBIRD v2.5.5:
SELECT LPAD( EXTRACT( YEAR FROM CURRENT_TIMESTAMP ), 4, '0' ) ||
LPAD( EXTRACT( MONTH FROM CURRENT_TIMESTAMP ), 2, '0' ) ||
LPAD( EXTRACT( DAY FROM CURRENT_TIMESTAMP ), 2, '0' ) || ' ' ||
LPAD( EXTRACT( HOUR FROM CURRENT_TIMESTAMP ), 2, '0' ) ||
LPAD( EXTRACT( MINUTE FROM CURRENT_TIMESTAMP ), 2, '0' ) ||
LPAD( TRUNC( EXTRACT( SECOND FROM CURRENT_TIMESTAMP ) ), 2, '0' )
FROM rdb$database
OUTPUT IS: YYYYMMDD HHMMSS
This is a fully version (in Integer)
select Extract(year FROM cast('NOW' as date))*10000 +
Extract(month FROM cast('NOW' as date))*100 +
Extract(day FROM cast('NOW' as date)) from rdb$database
This is a fully version (in VARCHAR)
select CAST(Extract(year FROM cast('NOW' as date))*10000 +
Extract(month FROM cast('NOW' as date))*100 +
Extract(day FROM cast('NOW' as date)) AS VARCHAR(8)) from rdb$database
This Should work.
CREATE TABLE tab( t time, d date, ts timestamp );
INSERT INTO tab(t,d,ts) VALUES ('14:59:23', '2007-12-31', '2007-12-31 14:59');
SELECT CAST(CAST(d as varchar(10)))
FROM tab;
Related
I have two int columns:
thedate - for example 20210512
thetime - for example 142342
So, i need to unite them to one column to check if the time difference is lower the 5.
I tried this:
TO_DATE(sysdate) - TO_DATE(thedate, 'YYYY-MM-DD') < 5
But it only for the date not for the time so i will be glad to know how to unite the two int columns + and convert it to date type for time difference.
You can use:
SELECT *
FROM table_name
WHERE TO_DATE( thedate * 1000000 + thetime, 'YYYYMMDDHH24MISS' ) > SYSDATE - 5;
Which, for the sample data:
CREATE TABLE table_name ( thedate INT, thetime INT );
INSERT INTO TABLE_NAME ( thedate, thetime ) VALUES (
TO_NUMBER( TO_CHAR( SYSDATE, 'YYYYMMDD' ) ),
TO_NUMBER( TO_CHAR( SYSDATE, 'HH24MISS' ) )
);
INSERT INTO TABLE_NAME ( thedate, thetime ) VALUES (
TO_NUMBER( TO_CHAR( SYSDATE - INTERVAL '4 23' DAY TO HOUR, 'YYYYMMDD' ) ),
TO_NUMBER( TO_CHAR( SYSDATE - INTERVAL '4 23' DAY TO HOUR, 'HH24MISS' ) )
);
INSERT INTO TABLE_NAME ( thedate, thetime ) VALUES (
TO_NUMBER( TO_CHAR( SYSDATE - INTERVAL '5 1' DAY TO HOUR, 'YYYYMMDD' ) ),
TO_NUMBER( TO_CHAR( SYSDATE - INTERVAL '5 1' DAY TO HOUR, 'HH24MISS' ) )
);
Outputs:
THEDATE
THETIME
20210512
131832
20210507
141832
Or, if you want to use indexes on the thedate and thetime columns (the query above would not use indexes on thedate and thetime columns but would require a function-based index) then:
SELECT *
FROM table_name
WHERE thedate > TO_NUMBER( TO_CHAR( SYSDATE - 5, 'YYYYMMDD' ) )
OR ( thedate = TO_NUMBER( TO_CHAR( SYSDATE - 5, 'YYYYMMDD' ) )
AND thetime >= TO_NUMBER( TO_CHAR( SYSDATE - 5, 'HH24MISS' ) )
)
However, the better solution is to use appropriate data-types for your data; in this case, you should store date values in a DATE data-type (which, in Oracle, contains year-second components) rather than as two INT values for date and time.
db<>fiddle here
You want to convert to a timestamp, not a date. So:
select to_timestamp(cast(20210512 * 1000000 + 142342 as varchar2(255)), 'YYYYMMDDHH24MISS')
from dual;
Here is a db<>fiddle showing that this works.
Or in a where clause:
to_timestamp(cast(thedate * 1000000 + thetime as varchar2(255)), 'YYYYMMDDHH24MISS') > sysdate - interval '5' day
(or whatever you mean by "5").
Note: You may want to add a computed column to the table that has the full timestamp:
alter table t add column timestamp generated always as
( to_timestamp(cast(thedate * 10000 + thetime as varchar2(255)), 'YYYYMMDDHH24MISS') );
The date condition below is good for fetching current year and current month I would like to add current date to it ,please append the query to get current date .
If (( Extract ( Month, current_date )) = 1)
Then ( Cast ( ( Cast((( Extract (Year, current_date) -1)), varchar(4)) || '12'), int) )
Else ( If ((Extract ( Month, current_date) -1) < 10 )
Then ( Cast(( ( Cast ((Extract ( Year, current_date )), varchar(4))) || '0' || ( Cast (( Extract ( Month, current_date) - 1), varchar(2)))), int) )
Else ( Cast (( Cast ((Extract ( Year, current_date )), varchar(4)) || ( Cast(( Extract ( Month, current_date) - 1), varchar(2)))), int) )
)
thanks in advance
Your code is Cognos right? I can't help you there, but in DB2, this will get you current year, current month and current date.
values (year(current date), month(current date), current date)
e.g. it will return
1 2 3
---- - ----------
2018 3 2018-03-21
I have a column in my table having date value as Decimal like 20180715 for the date 15-07-2018.
I want to convert it to MMDDYYYY format.
Example:
Given decimal value 20180715 is converted to 07152018.
How to do it ?
try somthing like this:
select
VARCHAR_FORMAT( TIMESTAMP_FORMAT(cast(yourcolumn as varchar(8)), 'YYYYMMDD') , 'MMDDYYYY')
from yourtable
but you you want a really date do it:
select
DATE( TIMESTAMP_FORMAT(cast(yourcolumn as varchar(8)), 'YYYYMMDD'))
from yourtable
Try this query for the date
select
date(timestamp_format(char(yourcolumn+19000000), 'YYYYMMDD'))
from yourtable
To get the time
select
cast( substr( right( '00' || yourcolumn, 6) ,1,2) || ':' || substr( right( '00' || yourcolumn, 6) ,3,2) || ':' || substr( right( '00' || yourcolumn, 6) ,5,2) as time)
from yourtable
I would like to use CAST to convert a DATE type to a VARCHAR2 type.
DBUSER >SELECT CAST(CURRENT_DATE AS VARCHAR2(20)) THE_DATE from DUAL;
THE_DATE
--------------------
09-AUG-17
However, I need the VARCHAR2 result to be formatted as 'YYYYMM'. I know that I can achieve this effect by changing the session date format, but I would rather not do that.
DBUSER >ALTER SESSION SET NLS_DATE_FORMAT = 'YYYYMM';
Session altered.
DBUSER >SELECT CAST(CURRENT_DATE AS VARCHAR2(20)) THE_DATE from DUAL;
THE_DATE
--------------------
201708
I would like to avoid using Oracle's proprietary TO_CHAR() function. Does anyone have a suggestion on how to do that?
I am trying to standardize on ANSI SQL to the degree possible and avoid proprietary vendor nonstandard implementations.
There is no function specified in the ANSI SQL92 standard which formats DATETIME datatypes as a string.
The simplest solution is to use the functions Oracle provides for that purpose:
SELECT TO_CHAR( yourdate, 'YYYYMM' ) FROM yourtable;
However, you can get the year and month components using the EXTRACT function (which is in the ANSI standard):
SELECT EXTRACT( YEAR FROM yourdate ),
EXTRACT( MONTH FROM yourdate )
FROM yourtable;
Then you need to convert the numbers to a string and concatenate the strings:
SELECT TO_CHAR( EXTRACT( YEAR FROM yourdate ) )
|| TO_CHAR( EXTRACT( MONTH FROM yourdate ) )
FROM yourtable
but you were trying to avoid TO_CHAR so you could do:
SELECT CAST( EXTRACT( YEAR FROM yourdate ) AS VARCHAR2(4) )
|| CAST( EXTRACT( MONTH FROM yourdate ) AS VARCHAR2(2) )
FROM yourtable
or, using an implicit cast
SELECT EXTRACT( YEAR FROM yourdate )
|| EXTRACT( MONTH FROM yourdate )
FROM yourtable
However, if the year is not 4-digits or the month is not 2-digits then you need to pad the values; again, the simple solution is TO_CHAR:
SELECT TO_CHAR( EXTRACT( YEAR FROM yourdate ), 'FM0000' )
|| TO_CHAR( EXTRACT( MONTH FROM yourdate ), 'FM00' )
FROM yourtable
or LPAD:
SELECT LPAD( EXTRACT( YEAR FROM yourdate ), 4, '0' )
|| LPAD( EXTRACT( MONTH FROM yourdate ), 4, '0' )
FROM yourtable
But neither of those are in the ANSI standard so:
SELECT CASE
WHEN EXTRACT( YEAR FROM yourdate ) < 10 THEN '000'
WHEN EXTRACT( YEAR FROM yourdate ) < 100 THEN '00'
WHEN EXTRACT( YEAR FROM yourdate ) < 1000 THEN '0'
ELSE NULL
END
|| EXTRACT( YEAR FROM yourdate )
|| CASE
WHEN EXTRACT( MONTH FROM yourdate ) < 10 THEN '0'
END
|| EXTRACT( MONTH FROM yourdate )
FROM yourtable;
And we've managed to transform a single Oracle function into a behemoth of an ANSI compatible expression.
But, Oracle's DATE datatype does not comply to the ANSI standard (it is a concatenation of the ANSI DATE and TIME datatypes) so I'll ask whether it is worth it - especially if you then consider displaying the time component of a date (which EXTRACT will not extract unless you first use CAST to convert the DATE to a TIMESTAMP).
SELECT TO_CHAR( yourdate, 'YYYYMMDDHH24MISS' ) FROM yourtable
or
SELECT CASE
WHEN EXTRACT( YEAR FROM yourdate ) < 10 THEN '000'
WHEN EXTRACT( YEAR FROM yourdate ) < 100 THEN '00'
WHEN EXTRACT( YEAR FROM yourdate ) < 1000 THEN '0'
ELSE NULL
END
|| EXTRACT( YEAR FROM yourdate )
|| CASE
WHEN EXTRACT( MONTH FROM yourdate ) < 10 THEN '0'
END
|| EXTRACT( MONTH FROM yourdate )
|| CASE
WHEN EXTRACT( DAY FROM yourdate ) < 10 THEN '0'
END
|| EXTRACT( DAY FROM yourdate )
|| CASE
WHEN EXTRACT( HOUR FROM CAST( yourdate AS TIMESTAMP ) ) < 10 THEN '0'
END
|| EXTRACT( HOUR FROM CAST( yourdate AS TIMESTAMP ) )
|| CASE
WHEN EXTRACT( MINUTE FROM CAST( yourdate AS TIMESTAMP ) ) < 10 THEN '0'
END
|| EXTRACT( MINUTE FROM CAST( yourdate AS TIMESTAMP ) )
|| CASE
WHEN EXTRACT( SECOND FROM CAST( yourdate AS TIMESTAMP ) ) < 10 THEN '0'
END
|| EXTRACT( SECOND FROM CAST( yourdate AS TIMESTAMP ) )
FROM yourtable;
[TL/DR] Just use TO_CHAR
This may be help you:
SELECT extract(year from CURRENT_DATE) || case when extract(month from CURRENT_DATE) <10 THEN '0' || extract(month from CURRENT_DATE) END THE_DATE from DUAL;
There is a column 'DateTime' that displays the Date and Time in 'YYYY/MM/DD HH24:MI:SS' format.
I need to display only the rows that has time in a particular range(2AM - 6AM) regardless of the particular date.
I have a code that only displays the time range(2AM - 6AM) for the particular day. I need to display the rows that falls in the past 7 days with the time range 2AM-6AM.
SELECT *
FROM <table_name >
WHERE DateTime BETWEEN TO_DATE (
TO_CHAR (TRUNC (SYSDATE), 'DD-MM-YYYY')
|| ' '
|| '02:00:00',
'DD-MM-YYYY HH24:MI:SS')
AND TO_DATE (
TO_CHAR (TRUNC (SYSDATE), 'DD-MM-YYYY')
|| ' '
|| '06:00:00',
'DD-MM-YYYY HH24:MI:SS')
ORDER BY 1 DESC
SELECT *
FROM your_table
WHERE TO_CHAR( DateTime, 'HH24MMSS' ) BETWEEN '020000' AND '060000'
AND DateTime >= TRUNC( SYSDATE ) - INTERVAL '7' DAY
AND DateTime < TRUNC( SYSDATE ) + INTERVAL '1' DAY
or
SELECT *
FROM your_table
WHERE DateTime BETWEEN TRUNC( DateTime ) + INTERVAL '2' HOUR
AND TRUNC( DateTime ) + INTERVAL '6' HOUR
AND DateTime >= TRUNC( SYSDATE ) - INTERVAL '7' DAY
AND DateTime < TRUNC( SYSDATE ) + INTERVAL '1' DAY
You need two conditions for that: one for the hour being between 02 and 05, and one for the day being in the last seven days:
SELECT *
FROM <table_name>
WHERE to_char(DateTime, 'HH') BETWEEN '02' and '05' AND
DateTime BETWEEN SYSDATE - 7 AND SYSDATE
ORDER BY 1 DESC
In this case, I think the easiest way is string comparison:
WHERE SUBSTR(DateTime, 12, 2) BETWEEN '02' and '05'
Note: Between is inclusive so this should get everything up to 05:59:59.
SELECT *
FROM <table_name >
WHERE DateTime BETWEEN TO_DATE (
TO_CHAR (TRUNC (SYSDATE)-7, 'DD-MM-YYYY')
|| ' '
|| '02:00:00',
'DD-MM-YYYY HH24:MI:SS')
AND TO_DATE (
TO_CHAR (TRUNC (SYSDATE), 'DD-MM-YYYY')
|| ' '
|| '06:00:00',
'DD-MM-YYYY HH24:MI:SS')
select * from table_name where to_char(datetime,'HH24')
between '2' and '6' and to_char(datetime,'dd-mon-yy')
between to_char(sysdate,'dd-mon-yy') and to_char(sysdate-7,'dd-mon-yy');