guys. I hava the query,which counts the number of some events per one day.
But it returns date in wrong format.
select trunc(action_dt) as action_dt, count(*) as cnt
from stat.jurnal_orders
where action_dt between TO_DATE('17.05.12','DD/MM/YY') AND TO_DATE('12.11.13','DD/MM/YY')
group by trunc(action_dt)
order by action_dt asc
In database I have my date in DD.MM.YYYY but I want YYYY/MM/DD
Can somebody help me with that query?
you have two options
First - explicitly set the format you want in the query using TO_CHAR
select to_char(trunc(action_dt),'yyyy/mm/dd') as action_dt, count(*) as cnt
from stat.jurnal_orders
where action_dt between TO_DATE('17.05.12','DD/MM/YY') AND TO_DATE('12.11.13','DD/MM/YY')
group by to_char(trunc(action_dt),'yyyy/mm/dd')
order by action_dt asc
Second, set the nls_date_format at session level.
alter session set nls_date_format = 'yyyy/mm/dd';
and you query
select trunc(action_dt) as action_dt, count(*) as cnt
from stat.jurnal_orders
where action_dt between TO_DATE('2012/05/17') AND TO_DATE('2013/12/11')
group by trunc(action_dt)
order by action_dt asc
To convert dates into a different format you can use the Oracle TO_CHAR function. http://www.techonthenet.com/oracle/functions/to_char.php
For your example, assuming you want to TRUNC the date first, you can use it like so:
SELECT TO_CHAR(TRUNC(action_dt), 'YYYY/MM/DD') as action_dt
FROM some_table
Please try TO_CHAR:
select TO_CHAR (sysdate, 'YYYY/MM/DD') from dual;
SELECT REPLACE(CONVERT(VARCHAR(10), action_dt, 102), '.', '/')
http://msdn.microsoft.com/en-us/library/ms187928.aspx
EDIT: This is for T-SQL not Oracle. Misread the question.
Related
I followed this post Order by using month name in PostgreSQL but not success!
I have a query (its working) and I just need to order the results by mont name. This is thr query I am using:
select to_char(purchase_date, 'Month') as mes_2021,
sum(gmv::float4) as soma_gmv
from tablename
where purchase_date > '2021-01-01'
GROUP BY mes_2021
I am trying:
order by to_date(purchase_date, 'Month') - No success
order by date_part(purchase_date::date, 'Month') - No success
If i use order by mes_2021
One trick is to use a window function on the date:
select to_char(purchase_date, 'Month') as mes_2021,
sum(gmv::float4) as soma_gmv
from tablename
where purchase_date > '2021-01-01'
group by mes_2021
order by min(purchase_date);
This, of course, assumes that the dates are all in the same year. But your where clause is taking care of that.
I want to write a select that would return me a distinct years only (2018, 2017,2016).
I have column AS_OF_DATE in table HISTORY.
Here are some example values of AS_OF_DATE:
31-05-18,
31-04-17,
31-07-16,
...
This is what I tried, but it doesn't work:
SELECT CONCAT('20',DISTINCT SUBSTR(AS_OF_DATE, 7, 2) FROM HISTORY
I used CONCAT to add 20 in front of the result and SUBSTR that would start at the 7th string and would be 2 strings long (so I get 18,17,16...)
try like below
SELECT DISTINCT '20' || SUBSTR(AS_OF_DATE, 7, 2) FROM HISTORY
Normally, you would do this with extract() or to_char():
select extract(year from as_of_date) as yyyy
or
select to_char(as_of_date, 'YYYY') as yyyy
This assumes that as_of_date is a date, which is should be.
You can add select distinct if you want a result set with the distinct years.
You can use
SELECT CONCAT('20', SUBSTR(AS_OF_DATE, -2) ) as "Years"
FROM HISTORY
GROUP BY SUBSTR(AS_OF_DATE, -2)
ORDER BY "Years"
Demo
this will work:
select distinct extract(year from as_of_date) from History;
the extract function takes off the as_of_date provided it is a date datatype and distinct select only one for dates which are multiple times in the extract.
I have the following query that I am trying to run on Athena.
SELECT observation_date, COUNT(*) AS count
FROM db.table_name
WHERE observation_date > '2017-12-31'
GROUP BY observation_date
However it is producing this error:
SYNTAX_ERROR: line 3:24: '>' cannot be applied to date, varchar(10)
This seems odd to me. Is there an error in my query or is Athena not able to handle greater than operators on date columns?
Thanks!
You need to use a cast to format the date correctly before making this comparison. Try the following:
SELECT observation_date, COUNT(*) AS count
FROM db.table_name
WHERE observation_date > CAST('2017-12-31' AS DATE)
GROUP BY observation_date
Check it out in Fiddler: SQL Fidle
UPDATE 17/07/2019
In order to reflect comments
SELECT observation_date, COUNT(*) AS count
FROM db.table_name
WHERE observation_date > DATE('2017-12-31')
GROUP BY observation_date
You can also use the date function which is a convenient alias for CAST(x AS date):
SELECT *
FROM date_data
WHERE trading_date >= DATE('2018-07-06');
select * from my_schema.my_table_name where date_column = cast('2017-03-29' as DATE) limit 5
I just want to add my little words here, if you have date column with ISO-8601 format, for example: 2022-08-02T01:46:46.963120Z then you can use parse_datetime function.
In my case, the query looks like this:
SELECT * FROM internal_alb_logs
WHERE elb_status_code >= 500 AND parse_datetime(time,'yyyy-MM-dd''T''HH:mm:ss.SSSSSS''Z') > parse_datetime('2022-08-01-23:00:00','yyyy-MM-dd-HH:mm:ss')
ORDER BY time DESC
See more other examples here: https://docs.aws.amazon.com/athena/latest/ug/application-load-balancer-logs.html#query-alb-logs-examples
I would like to get the unique date values from order table using oracle query. I am getting
ORA-01791: not a SELECTed expression
error, When i tried this below query
SELECT DISTINCT (TO_DATE(LAST_INSERT_TIMESTAMP, 'YYYY-MM-DD HH24:MI'))
FROM ORDER
WHERE LAST_INSERT_TIMESTAMP IS NOT NULL
ORDER BY LAST_INSERT_TIMESTAMP DESC;
LAST_INSERT_TIMESTAMP is not in your result list, because you have aggregated your rows with DISTINCT to a truncated timestamp. You can only order by this.
SELECT DISTINCT TRUNC(LAST_INSERT_TIMESTAMP, 'MI')
FROM ORDER
WHERE LAST_INSERT_TIMESTAMP IS NOT NULL
ORDER BY TRUNC(LAST_INSERT_TIMESTAMP, 'MI') DESC;
If you don't want to repeat the expression use positional sort:
ORDER BY 1 DESC;
Or use an alias for the expression:
SELECT DISTINCT TRUNC(LAST_INSERT_TIMESTAMP, 'MI') AS LAST_INSERT
FROM ORDER
WHERE LAST_INSERT_TIMESTAMP IS NOT NULL
ORDER BY LAST_INSERT DESC;
Please note that I replaced your TO_DATE with the appropriate TRUNC because all you want to do is truncate your timestamp, not convert to and from string.
Can you please help me in solving this problem. I am trying to order the results of an SQL query by date, but I'm not getting the results I need.
The query I'm using is:
SELECT date FROM tbemp ORDER BY date ASC
Results are:
01/02/2009
03/01/2009
04/06/2009
05/03/2009
06/12/2008
07/02/2009
Results should be:
06/12/2008
03/01/2009
01/02/2009
07/02/2009
I need to select the date in the format above.
Your help is much appreciated.
It seems that your date column is not of type datetime but varchar. You have to convert it to datetime when sorting:
select date
from tbemp
order by convert(datetime, date, 103) ASC
style 103 = dd/MM/yyyy (msdn)
It sounds to me like your column isn't a date column but a text column (varchar/nvarchar etc). You should store it in the database as a date, not a string.
If you have to store it as a string for some reason, store it in a sortable format e.g. yyyy/MM/dd.
As najmeddine shows, you could convert the column on every access, but I would try very hard not to do that. It will make the database do a lot more work - it won't be able to keep appropriate indexes etc. Whenever possible, store the data in a type appropriate to the data itself.
Unsure what dbms you're using however I'd do it this way in Microsoft SQL:
select [date]
from tbemp
order by cast([date] as datetime) asc
this works for me:
SELECT datefield
FROM myTable
ORDER BY CONVERT(DATE, datefield) ASC
Following answer may help you
perform your date ordering by your date identifier but use to_char() function in select clause and use some other identifier in select clause for date
e.g.
SELECT TO_CHAR(DISPDATE1,'DD/MM/YYYY') AS DISPDATE,
SUM(APPLCOUNT) AS APPLIED,
SUM(CONFCOUNT) AS CONFIRMED
FROM
(
SELECT COUNT(ID) AS APPLCOUNT,
0 AS CONFCOUNT,
STUDENT.APPLIED_ON AS DISPDATE1
FROM STUDENT
WHERE STUDENT.ID = P_ID
GROUP BY STUDENT.APPLIED_ON
UNION
SELECT 0 AS APPLCOUNT,
COUNT(ID) AS CONFCOUNT,
STUDENT.CONFIRMED_ON AS DISPDATE1
FROM STUDENT
WHERE STUDENT.ID = P_ID
GROUP BY STUDENT.CONFIRMED_ON
)
GROUP BY DISPDATE1
ORDER BY DISPDATE1;
SELECT CONVERT(char(19), CAST(date AS datetime), 101) as [date]
FROM tbemp
ORDER BY convert(datetime, date, 101) ASC
Try using this this work for me
select * from `table_name` ORDER BY STR_TO_DATE(start_date,"%d-%m-%Y") ASC
where start_date is the field name
I wanted to edit several events in descendant chonologic order, and I just made a :
select
TO_CHAR(startdate,'YYYYMMDD') dateorder,
TO_CHAR(startdate,'DD/MM/YYYY') startdate,
...
from ...
...
order by dateorder desc
and it works for me.
But surely not adapted for every case...
Just hope it'll help someone !
This may help you in mysql, php.
//your date in any format
$date = $this->input->post('txtCouponExpiry');
$day = (int)substr($date, 3, 2);
$month = (int)substr($date, 0, 2);
$year = (int)substr($date, 7, 4);
$unixTimestamp = mktime(0, 0, 0, $year, $day, $month);
// insert it into database
'date'->$unixTimestamp;
//query for selecting order by date ASC or DESC
select * from table order_by date asc;
try this
Order by Convert(datetime,#date) desc
this should work for your date format
order by convert(date, your_column, 104) desc
Casting/Converting can result in out of range exceptions that unfortunately are not always as simple as excluding nulls.
A simple alternative method, which avoids the cast, is:
SELECT date
FROM table
ORDER BY YEAR(date), MONTH(date), DAY(date) ASC;