473,796 Members | 2,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL Query Help-- Order By Clause

Hi

I want a simple select query on a column-name (smalldatetime) with
values dislayed in desc order with null values FIRST.

i.e.

Select orderdate from orders
order by ( null values first and then orderdate in desc order)

could any one please help

Thanks
Jul 20 '05 #1
7 3571
me
Just an idea that might work somehow don't know if its a good way to do it
or not but ...
order by
isnull(cast(dat efieldname as varchar(20)),'Z ') desc


"Muzamil" <mu*****@hotmai l.com> wrote in message
news:5a******** *************** ***@posting.goo gle.com...
Hi

I want a simple select query on a column-name (smalldatetime) with
values dislayed in desc order with null values FIRST.

i.e.

Select orderdate from orders
order by ( null values first and then orderdate in desc order)

could any one please help

Thanks


Jul 20 '05 #2
On 12 Aug 2004 07:09:16 -0700, Muzamil wrote:
Hi

I want a simple select query on a column-name (smalldatetime) with
values dislayed in desc order with null values FIRST.

i.e.

Select orderdate from orders
order by ( null values first and then orderdate in desc order)

could any one please help

Thanks


Hi Muzamil,

ORDER BY
CASE WHEN orderdate IS NULL THEN 1 ELSE 2 END,
orderdate DESC

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #3
>
ORDER BY
CASE WHEN orderdate IS NULL THEN 1 ELSE 2 END,
orderdate DESC

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)


IMHO - much better

declare @a table (
tid int identity(1,1) NOT NULL PRIMARY KEY
,tdate datetime NULL
)
insert into @a (
tdate
) select
'20040901'
union all
select
'20040601'
union all
select
'20040901'
union all
select
'20030101'
union all
select
NULL
union all
select
'20040212'
union all
select
NULL
select * from @a
ORDER BY
ISNULL(tdate, '20500101') desc
Jul 20 '05 #4
On Thu, 12 Aug 2004 20:02:56 +0300, Garry wrote:

ORDER BY
CASE WHEN orderdate IS NULL THEN 1 ELSE 2 END,
orderdate DESC

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)


IMHO - much better

declare @a table (
tid int identity(1,1) NOT NULL PRIMARY KEY
,tdate datetime NULL
)
insert into @a (
tdate
) select
'20040901'
union all
select
'20040601'
union all
select
'20040901'
union all
select
'20030101'
union all
select
NULL
union all
select
'20040212'
union all
select
NULL
select * from @a
ORDER BY
ISNULL(tdate, '20500101') desc


Hi Garry,

Yes you're right, that's better. Thanks. Though I'd use a date further in
the future (99991231 - the maximum datetime). Dates after Jan 1st 2050
might already start appearing in some databases (e.g. as ending date for a
mortgage or life insurance policy).

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #5
Thank you, Hugo.
I did not know, that datetime type is such large.
Now I have read BOL.
Jul 20 '05 #6
Hugo Kornelis (hugo@pe_NO_rFa ct.in_SPAM_fo) writes:
Yes you're right, that's better. Thanks. Though I'd use a date further in
the future (99991231 - the maximum datetime). Dates after Jan 1st 2050
might already start appearing in some databases (e.g. as ending date for a
mortgage or life insurance policy).


We went through a period where we tried to save some space by using
smalldatetime which goes over the brink in June 2076. We later reverted,
but a few tables were left behind (and an even smaller few may still
remain). And then one day as a customer of ours tried to import
securities into our database, thing blew up beacuse of a bond with a due
date in 2083 which came past this particular table.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #7
Thanks everyone for the solution as well as the valuable information

Erland Sommarskog <es****@sommars kog.se> wrote in message news:<Xn******* *************** @127.0.0.1>...
Hugo Kornelis (hugo@pe_NO_rFa ct.in_SPAM_fo) writes:
Yes you're right, that's better. Thanks. Though I'd use a date further in
the future (99991231 - the maximum datetime). Dates after Jan 1st 2050
might already start appearing in some databases (e.g. as ending date for a
mortgage or life insurance policy).


We went through a period where we tried to save some space by using
smalldatetime which goes over the brink in June 2076. We later reverted,
but a few tables were left behind (and an even smaller few may still
remain). And then one day as a customer of ours tried to import
securities into our database, thing blew up beacuse of a bond with a due
date in 2083 which came past this particular table.

Jul 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3074
by: Xenophobe | last post by:
I know this isn't a MySQL forum, but my question is related to a PHP project. I have two tables. table1 table2 "table1" contains 2 columns, ID and FirstName:
14
3233
by: Bruce W...1 | last post by:
I do a query to MySQL using PHP. Well actually I do too many of them which is the problem, it's too slow. I think maybe an inner join or something would be better but I can't figure this out. The web page on which the data is displayed has a list of countries. Each country has a label heading under which websites are listed that have information on that country. Any given website may appear many times (under different countries) if it...
2
630
by: Jesse Fitterer | last post by:
This query works except when the count for the Wins and losses is 0, the query returns nothing. The count has to be at least 1 or more. ANy ideas? Select fname+' '+lname as full_name,SubQuery.Wins, Query.losses from tbl_Participants a inner join (Select Part_ID,count(*) as Wins from tbl_Weekly_Picks where pick_winner = actual_winner and actual_winner <> 0 group by part_id) SubQuery
2
2363
by: aj70000 | last post by:
This is my query select ano,max(date),a_subject from MY_TAB where table_name='xyz' and ano=877 group by a_subject,ano order by a_subject ANO max(Date) A_Subject 877 2005-01-20 00:00:00.000 Subject_1 877 1900-01-01 00:00:00.000 Subject_2 877 2004-12-20 00:00:00.000 Subject_3
4
1485
by: frizzle | last post by:
Hi there, I can not seem to figure out where to put my WHERE clause in the next query: SELECT s.id, s.kind_id, s.active, s.name, COUNT(p.id) AS 'quantity' FROM pr_series s
1
1833
by: Hought, Todd | last post by:
Hi all, trying to run a query against a table, to pull the date out, and order it. problem is, the date is stored in character (string) format, not as an actual timestamp, so parsing it back into an offical 'date' is proving tricky. the last query that I ran, that I'm pretty sure should be working looks like this: (stdtime is the field with the date in numeric format, normally outpus as: Tue Jan 6 11:36:24 2004) select * from "table"...
2
369
by: schoultzy | last post by:
Hello Everyone, This is probably a simple fix so be kind when/if you reply. The query below retrieves information for individuals based on a column named ATTRIB_DEF, and assorted other columns; however, the ATTRIB_DEF column is important one. Currently the query gets all individuals that have an ATTRIB_DEF that contains the string 'AC1' at the beginning of the entry. I want the query to do the opposite. I want all of the individuals...
2
1657
by: AJ | last post by:
Hi all, I have this monster query (at least i think it is). SELECT c.ID, c.Company_Name, p., 1 As QueryNbr FROM (Company AS c LEFT JOIN Sale AS s ON c.ID = s.Company_ID) LEFT JOIN Package AS p ON s.Package_ID = p.ID
4
11119
by: islandfong | last post by:
Hi there, I am implementing a reporting database which manipulating a huge amount of data. I used a lot of join. Just wondering which one performs better between the two scenarios: 1. INNER JOIN first then filter by the WHERE clause. 2. Sub-query which WHERE clause first then INNER JOIN. Anybody have any recommendation?
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9524
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.