get date order by day and month in sql - sql

I am storing the date as "14-02-2013" in date column of my table. Now when I get the date using "ORDER BY" then it should display the output as
14-02-2013
15-03-2013
24-05-2013
How to write the query for this. i.e getting order by day and month.
Any suggestion will be helpful.

Use DATE (or DATETIME) type in your column. Otherwise you will have to perform operations on string representation of date, which is not cool and will cost some extra time to perform

Try one of following:
Select * from Table1 order by date(dtcolumn) Asc
Select * from Table1 order by strftime('%d-%m-%Y', dtcolumn)

Try this query
Select * from Table1 Order By mydatecol Asc

Given you store your date as a temporal data type you can use
select * from your_date_table order by date(your_date_column) ASC
If you store it as a string(which you should not do), you can try
select * from your_date_table order by your_date_column ASC
Here is the doc for the sqlite date and time functions

Related

TRIM function on Datefield in SQL Server

I have written the following query:
select *
from employees
order by (CAST(LTRIM(RTRIM(JoingDate)) AS SMALLDATETIME)) desc
The query affects performance. Do we require to use TRIM on a DateTime column? How can we rewrite the above query to increase performance and to minimize execution time? Would we use CONVERT function instead of CAST?
Unless I am missing something, you should just do this:
select *
from employees
order by JoingDate desc
Not sure why you are converting your date to a string and then back to a date. If you want smalldatetime over datetime, you could just cast/convert it but why?
select *
from employees
order by CAST(JoingDate AS SMALLDATETIME) desc

how to SQL query for unique values but include an extra date field?

(I'm using Oracle 11.)
I have a query that looks like this to get me the unique report numbers ...
select distinct (REPORT_NUMBERS) from REPORTS
Works fine.
Now I want to sort them by their CREATION_DATE field. I tried this ...
select distinct (REPORT_NUMBERS), CREATION_DATE from REPORTS order by CREATION_DATE asc
But I get duplicate REPORT_NUMBERS. I tried this ...
select distinct (REPORT_NUMBERS) from REPORTS order by CREATION_DATE asc
But that gives me a "ORA-01791: not a SELECTed expression" error.
How can I get the unique list of report numbers ordered by creation date?
Any help is greatly appreciated!
Rob
Since you want them ordered in an ascending way, then this should do:
SELECT REPORT_NUMBERS, MIN(CREATION_DATE) MinCreationDate
FROM REPORTS
GROUP BY REPORT_NUMBERS
ORDER BY MinCreationDate
You can GROUP to get MAX/MIN creation date:
select REPORT_NUMBERS, MIN(CREATION_DATE)
from REPORTS
GROUP BY REPORT_NUMBERS
Min() would be the earliest date, MAX() the most recent.
The requirement doesn't make sense, unless you want to order them by the latest creation date, or earliest creation date etc. in which case you can:
select REPORT_NUMBERS, MAX(CREATION_DATE) lastest_creation_date
from REPORTS
group by REPORT_NUMBERS
order by 2
You are getting unique of distinct (REPORT_NUMBERS) and CREATION_DATE together.Not REPORT_NUMBERS alone.
Try following -
select REPORT_NUMBERS, MIN(CREATION_DATE) AS MinCreationDate
from REPORTS
group by REPORT_NUMBERS
order by 2 desc

Order by SQL timestamp

I have a timestamp field added to every of my records with an SQL timestamp, (yes, the format is actually TIMESTAMP). I want to display my records by date and time; can I just ORDER BY in an SQL statement?
SELECT * FROM table ORDER BY timestamp DESC
Assuming its MySQL,
SELECT *
FROM randomTable
ORDER BY timestampfield DESC;
is just perfect.
Yes, I tried it and it is working for me on Oracl SQL Developer

Sql Server 2005: Today's random records

I can easily get a random record with this:
SELECT * FROM MyTable ORDER BY NewId()
I can easily get a record with "today's date" with this:
SELECT * FROM MyTable WHERE MyDate = "2010-24-08" -- db doesn't store times
But how would I combind the two?
Get 1 random record... anything with today's date.
If none are found... get 1 random record from yesterday (today-1).
If none are found... get 1 random record from etc, etc, today-2
... until 1 record is found.
Just make the day date the primary order by condition:
select top(1) *
from Table
order by Date desc, newid();
If you store the dates as full day and time, you need to round them out to the day part only: cast (Date as DATE) in SQL 2008 or cast(floor(cast(Date as FLOAT)) as DATETIME) in pre-2008.
Use the TOP operator:
SELECT TOP 1 *
FROM MyTable
WHERE MyDate = "2010-24-08"
ORDER BY NEWID()
...combined with the ORDER BY NEWID(). Without the ORDER BY, you'd get the first inserted row/record of the records returned by the filteration in most cases typically, but the only way to ensure order is with an ORDER BY clause.
SQL Server 2005+ supports brackets on the TOP value, so you can use a variable in the brackets without needing to use dynamic SQL.
Does this give you what you want?
SELECT TOP 1 *
FROM MyTable
ORDER BY MyDate desc, NewId()
This assumes there are no dates later than today.

I want to show records from a table having a date column from sqldatabase in dates order. How should I?

I want to show records from a table having a date column from sqldatabase in dates order. How should I?
SELECT
*
FROM yourTable
ORDER BY yourDateColumn
you can use order by
select * from my_table t order by t.date_column
where date_column is a column name in your table.
SELECT * FROM `table_name` ORDER BY `dates_column`
optionally you can add DESC to reverse the order (newest to oldest):
SELECT * FROM `table_name` ORDER BY `dates_column` DESC
If I understand your question correctly, you want to use an ORDER BY clause on the column containing the dates.
The database engine should handle the proper sorting for a date or timestamp column using an ORDER BY clause. The only exception might be if your column is of type VARCHAR and holds a date of the form "mm/dd/yyyy". Then you've got a bit more work to do.