To Pull records between two dates in db2 - db2

I tried below two ways they not working
Select * from Table
where SERV_DATE BETWEEN '03/01/2013'AND
'03/31/2013'
ALSO This is not working
Select * from Table
where SERV_DATE BETWEEN DATE('03/01/2013') AND
DATE('03/31/2013')
What should be the correct format ?

Did you tried what NealB suggested? The reason for not accepting 03/01/2013 as an entry date format is, that it is region dependent in the US it is March 1, 2013 an in the UK it is January 3, 2013. So without considering the local, it is not certain, what the actual date is.
"why would db2 give error on the same format and will go well when given different format" - Don't forget, that db2 is an old lady and as all old ladies she has peculiarities. You just get used to it and there will be an happy ending.

SELECT * FROM tableName WHERE date(modifiedBy_date) between '2017-07-28' AND '2017-08-01';
Works cool for DB2.

Select * from Table
where SERV_DATE BETWEEN DATE('2013-03-01') AND DATE('2013-03-31');
Worked for me.

Select * from Table
where (SERV_DATE BETWEEN '03/01/2013'AND
'03/31/2013')
Select * from Table
where (SERV_DATE BETWEEN '2013-03-01'AND
'2013-03-31')

select count(*) from TABLE where time_stamp BETWEEN DATE('2018-01-01') AND DATE('2018-01-31');
Here time_stamp is field name and copy your timestamp filed name instead of time_stamp.

Related

Select datetime in postgres

Must be missing something very simple here. Maybe someone can answer this. You want to select from a datetimestamp to now. Why would the where clause return nothing with this?
select * from xyz where ('2020-01-29 04:18:00-06'::timestamptz > CURRENT_TIMESTAMP);
when
select CURRENT_TIMESTAMP;
current_timestamp
-------------------------------
2020-01-29 04:47:06.011133-06
(1 row)
Time: 0.258 ms
Should not be that hard in PostgreSQL to get every row from a certain time to current time to see what has been added or new / changed.
I found the answer to this problem with the help here. First try I did not post was using between. It should have been used like this. Need to remove timestamp from the where clause and use BETWEEN!
TABLENAME.COL-NAME-FOR-TIMESTAMP BETWEEN '2020-01-29 04:18:00-06' AND CURRENT_TIMESTAMP
TABLENAME.COL-NAME-FOR-TIMESTAMP should be your column in the table you are referencing for datetime.
Hope this helps someone!
The explanation is that your current timestamp, in the time zone of your Postgres server, is past 4:18am on January 29, 2020. Try setting your time zone to something which should make your current query pass, e.g.
SET timezone = 'America/Los_Angeles'; -- 2:58 AM as of the time this answer was posted
SELECT * FROM xyz WHERE '2020-01-29 04:18:00-06'::timestamptz > CURRENT_TIMESTAMP;
I suspect that the doubt here is that you are expecting your Postgres server to be set to a certain time zone, so you should look into this.

how to get each and every Date in between selected date (in DB2)

Dear StackOverflow Community,
as a New to DB2 ,i have a a query
may be its a very basic question for you, please share your knowledge.
i have a start date and End Date.
I need a list of each and every date in between.
Its ok with me ,if it creates a temp table no issue.
Thanks in Advance
You can generate the dates between start and end dates by using Recursive CTE expression. Try below code
with cte(your_columns,startdate,enddate)
as (select your_columns,startdate,enddate,startdate
as derDate
from yourTable
union all
select your_columns,startdate,enddate,derDate+1
from cte where
derDate<=endDate)
select * from cte

postgresql text type to date

so I am pretty new to this, but I would like to convert a text data_type into a date type (preferably into something like this yyyy-mm-ddThh:mi:ss.mmmZ date/time with timezone).
Now, I have found the W3 page's convert
and postresql. I have also found some solutions on stack overflow, but they all didn't work.
I have queried my database like this
SELECT TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,DATA_TYPE
FROM myappname.INFORMATION_SCHEMA.columns
WHERE TABLE_SCHEMA='myapp'
AND COLUMN_name LIKE '%date%'
ORDER BY table_name,column_name
where table_schema is where the tables of importance are in and table_name are tables of all my events. I selected only the properties (column_name) of these tables where they have a date, and all of them have a 'text' data_type.
Hence to my question, how can I convert them all to a date/time with timezone as above, so I can query my events (tables) by
where [column_name] between [date] and [date/time]
Thanks, in advance.
After looking into this for some time I found a solution. I am using
to_date() as a_horse_with_no_name suggests.
I post this as this may help others. My date format in string looked like this:
Mon Feb 20 11:34:21 GMT+07:00 2017
by running the select code below I get a column named to_date with a date type that looks like
2017-02-20.
select to_date(column_name,'Dy Mon DD HH24:MI:SS GMT+07:00 YYYY') from table_name;

How to Select data from db2 for current date

I have to retrieve data from DB2 for current date, but I am not able to find correct solution for the same.
For example:
SELECT * FROM table WHERE date = current-date
(I know this is wrong, it's just an example.)
I need a correct where condition for DB2.
There are two way to get this:
There is a special system table
enter link description here
SELECT current date FROM sysibm.sysdummy1
So you would need to use a subquery:
SELECT * FROM table
WHERE date = (SELECT current date FROM sysibm.sysdummy1)
There is even a special register
enter link description here
This allows to avoid the subquery:
SELECT * FROM table WHERE date = CURRENT DATE

PostgresSQL database has date field, I want all data but the date column as year only

I have data that has a date column in the full format but would like the output to only represent the year.
eg: from
SELECT * FROM tablename;
I want something like:
SELECT * FROM tablename WITH 'newdatefield' as SELECT EXTRACT(YEAR FROM TIMESTAMP 'datefield');
I'm not great at SQL - I remember that something like this can be done but can't find how to do it again.
I think you are overthinking this one:
SELECT EXTRACT(YEAR from datefield), The, Other, Fields, You, Want FROM tableName