How to get data between two date range in PostgreSQL? - postgresql

I m querying data between two date range in PostgreSQL SQL but it does not give me expected result.
select
pi_serial,
amount_in_local_currency,
status,
proforma_invoice_date
from proforma_invoice
where created_by = 25
and proforma_invoice_date BETWEEN '03/01/2018' and '09/03/2018'
order by proforma_invoice_date
Now look at the query and column proforma_invoice_date. In this query i am searching data between 03/01/2018 and 09/03/2018. the date format is (DD/MM/YYYY) and it's character varying. The result i have got in this picture. it just give me the result according by the only day but not the whole date format. i have tried so many things date conversion, character varying to date. but i didn't get any expected result

Your Query is absolutely fine. just change it as follows
select pi_serial, amount_in_local_currency, status, proforma_invoice_date
from proforma_invoice
where created_by = 25
and to_date(proforma_invoice_date, 'DD/MM/YYYY') BETWEEN to_date('03/01/2018', 'DD/MM/YYYY') and to_date('09/03/2018', 'DD/MM/YYYY')
order by proforma_invoice_date

Related

query to fetch records between two date and time

I have been using postgreSQL. My table has 3 columns date, time and userId. I have to find out records between the given date and time frame. Since date and time columns are different, 'BETWEEN' clause is not providing valid results
Combine the two columns into a single timestamp by adding the time to the date:
select *
from some_table
where date_column + time_column
between timestamp '2017-06-14 17:30:00' and timestamp '2017-06-19 08:26:00';
Note that this will not use an index on date_column or time_column. You would need to create an index on that expression. Or better: use a single column defined as timestamp instead.

hive cast string to date in 'dd/MMM/yyyy' format order by and group by issue

I've date stored as [27/Feb/2016:00:24:31 +0530].
I want date format in 27/Feb/2016 and also want to order by it.
I've tried this solution but it returns in form 2016-02-27 and also orders properly.
SELECT
TO_DATE( FROM_UNIXTIME( UNIX_TIMESTAMP( SUBSTR( time, 2, 11), 'dd/MMM/yyyy' ))) AS real_date,
url
FROM cleanned_logs
ORDER BY real_date ASC;
To get desired format i tried with date_format() function.It is not available in 1.2.1 so i switched to it from 1.0.1.
SELECT
DATE_FORMAT( FROM_UNIXTIME( UNIX_TIMESTAMP( SUBSTR(time,2,11),'dd/MMM/yyyy')), 'dd/MMM/yyyy') AS real_date,
url
FROM cleanned_logs
ORDER BY real_date ASC;
It gives me desired format but does not order properly.
UPDATED:
SELECT display_date,COUNT(url) FROM
(
SELECT SUBSTR(time,2,11) as display_date,url,UNIX_TIMESTAMP(SUBSTR(time,2,11),'dd/MMM/yyyy') as real_date FROM cleanned_logs order by real_date ASC
)b group by real_date;
Creates problem in grouping. Here hive expects real_date in select clause.
I think you're mixing up the formatting or display of data, with the underlying data itself. If the table stores a date as a string formatted in one manner, [27/Feb/2016:00:24:31 +0530] it's still a string, and strings sort differently than actual dates, timestamps, or numbers.
Ideally, you would store the date as a TIMESTAMP datatype. When you want to display it, use DATE_FORMAT, and when you want to sort it, use ORDER BY on the underlying data field. So if your field is of type TIMESTAMP called some_time, you could query as
SELECT DATE_FORMAT(some_time, 'dd/MMM/yyyy')
FROM some_table
WHERE some_condition
ORDER BY some_time DESC
If you're stuck with a string that's stored as a valid timestamp value, then you'll have to do more work, perhaps
SELECT SUBSTR(some_time, 2, 11)
FROM some_table
WHERE some_condition
ORDER BY unix_timestamp(SUBSTR(some_time,2,11), 'dd/MMM/yyyy'))
The second option displays the value as desired, and orders by a number -- a unix timestamp is just a number, but it has the same order as the date, so no need to cast that further to an actual date.

Convert Time Zone in Amazon Redshift

I have a datetime column in a redshift table. It contains date in UTC format.
So, when I am querying based on time_zone like UTC+5:30/ UTC+4:30, I want to convert the table datetime column to chosen time_zone and proceed further. It gives me wrong result.
Using this method :
CONVERT_TIMEZONE ( ['source_zone',] 'target_zone', 'timestamp')
Query Type 1: Wrong input, correct answer
SELECT id, convert_timezone('UTC+5:30','UTC', date) as converted_time, ingest_date
FROM table_name
WHERE conditions
Query Type 2: Correct input, wrong answer -> It again subtracting 5:30 from the date in column
SELECT id , convert_timezone('UTC','UTC+5:30',ingest_date) as converted_time, ingest_date
FROM table_name
WHERE conditions
Query Type 3: Wrong input, correct answer
SELECT id, convert_timezone('UTC','UTC-5:30',ingest_date) as converted_time, ingest_date
FROM table_name
WHERE conditions
How to convert / parse the UTC column into selected timezone?
In Redshift, CONVERT_TIMEZONE interprets the offset as the time from UTC. For example, an offset of +2 is equivalent to UTC–2, and an offset of -2 is equivalent to UTC+2. CONVERT_TIMEZONE does not use the prefix string when calculating the offset, even if the string represents a valid time zone. For example, 'NEWZONE+2’, 'PDT+2', and'GMT+2' all have the same result. If a string does not include an offset, then it must represent a valid time zone or CONVERT_TIMEZONE returns an error.
For converting time_zone, if you send "UTC+5:30/UTC-4:30", amazon interpreting it as "UTC-5:30 / UTC+4:30".
Now you can convert + into - and vice versa before sending it to redshift.
(http://docs.aws.amazon.com/redshift/latest/dg/CONVERT_TIMEZONE.html)

how to insert the current system date and time in oracle10g database

I have created a table with a column date_time type (varchar2 (40) ) but when i try to insert the current system date and time the doesnt work it gives error (too many values). please tell me what's wrong with the insert statement.
create table HR (type varchar2 (20), raised_by number (6), complaint varchar2 (500), date_time varchar2(40))
insert into HR values ('request',6785,'good morning',sysdate,'YYYY/MM/DD:HH:MI:SSAM')
The immediate cause of the error is that you have too many values, as the message says; that is, more elements in your values clause than there are columns. It is better to explicitly list the column names to avoid future problems and confusion, so you're really doing this:
insert into HR (type, raised_by, complaint, date_time)
values ('request',6785,'good morning',sysdate,'YYYY/MM/DD:HH:MI:SSAM')
... sp you have four columns, but five values. You're trying to insert the current date/time as a string so you would need to use the to_char() function:
insert into HR (type, raised_by, complaint, date_time)
values ('request',6785,'good morning',
to_char(sysdate,'YYYY/MM/DD:HH:MI:SSAM'))
But it is bad practice to store a date (or any other structured data, such as a number) as a string. As the documentation notes:
Each value manipulated by Oracle Database has a data type. The data
type of a value associates a fixed set of properties with the value.
These properties cause Oracle to treat values of one data type
differently from values of another. For example, you can add values of
NUMBER data type, but not values of RAW data type.
If you use a string then you can put invalid values in. If you use a proper DATE data type then you cannot accidentally put an invalid or confusing value in. Oracle will also be able to optimise the use of the column, and will be able to compare values safely and efficiently. Although the format you're using is better than some, using string comparison you still can't easily compare two values to see which is earlier, so you can't properly order by the date_time column for example.
Say you inserted two rows with values 2013/11/15:09:00:00AM and 2013/11/15:08:00:00PM - which is earlier? You need to look at the AM/PM marker to realise the first one is earlier; with a string comparison you'd get it wrong because 8 would be sorted before 9. Using HH24 instead of HH and AM avoids that, but would still be less efficient than a true date.
If you need to store a date with a time component you can use the DATE data type, which has precision down to the second; or if you need fractional seconds too then you can use TIMESTAMP. Then your table and insert would be:
create table HR (type varchar2 (20), raised_by number (6),
complaint varchar2 (500), date_time date);
insert into HR (type, raised_by, complaint, date_time)
values ('request',6785,'good morning',sysdate);
You can still get the value in the format you wanted for display purposes as part of a query:
select type, raised_by, complaint,
to_char(date_time, 'YYYY/MM/DD:HH:MI:SSAM') as date_time
from HR
order by date_time;
TYPE RAISED_BY COMPLAINT DATE_TIME
-------------------- ---------- -------------------- ---------------------
request 6785 good morning 2013/11/15:08:44:35AM
Only treat a date as a string for display.
You can use TO_DATE() or TO_TIMESTAMP or To_char() function,
insert into HR values ('request',6785,'good morning',TO_DATE(sysdate, 'yyyy/mm/dd hh24:mi:ss'))
insert into HR values ('request',6785,'good morning',TO_TIMESTAMP(systimestamp, 'yyyy/mm/dd hh24:mi:ss'))
sysdate - It will give date with time.
systimestamp - It will give datetime with milliseconds.
To_date() - Used to convert string to date.
To_char() - Used to convert date to string.
Probably here you have to use To_char() because your table definition have varchar type for date_time column.
Use TIMESTAMP datatype for date_time. And while inserting use the current timestamp.
create table HR (type varchar2(20), raised_by number(6), complaint varchar2(500), date_time timestamp);
insert into HR values ('request',6785,'good morning', systimestamp);
For other options: http://psoug.org/reference/timestamp.html

Select rows within a date range in T-SQL

I have a set of rows, each with a date value, and I need to select rows that fall within a specific date range. How can I do this?
select * from table where convert(int,date_created) between //what should go here?
I want to select between '20-10-2010' and '22-10-2010'.
It keeps complaining about string to date conversion.
You need to use yyyymmdd which is the safest format for SQL Server
select * from table
where date_created BETWEEN '20101020' and '20101022'
Not sure why you had CONVERT to int there...
Note: if date_created has a time component that this fails because it assume midnight.
Edit:
To filter for the day 20 Oct 2010 to 22 Oct 2010 inclusive, if the date_created column has times, without applying a function to date_created:
where date_created >= '20101020' and date_created < '20101023'
Either don't convert the date_created to an int or use integers for your data values
I would leave the date_created as a date.
select * from table
where date_created between '20101020' and '20101022'