Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Display first true and then false

In my SQL table, I have records. Some of them have FLAG Y/N. Now, when I run my query, I want to display first all the Y values and then N.

Table details

StuID …. StuFlag

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

1 …. N

2 …. N

3 …. Y

4 …. N

5 …. Y

Now, when I execute the query, the output should be:

3 …. Y

5 …. Y

1 …. N

2 …. N

4 …. N

>Solution :

The clean way would be:

CREATE TABLE table1 (
  stuid  INTEGER NOT NULL ...
, stuflag BIT
, [.. OTHER COLUMNS .. ]
);

INSERT INTO table1 VALUES (42,TRUE);
INSERT INTO table1 VALUES (42,FALSE);

SELECT * FROM table1 ORDER BY stuflag DESC;
-- FALSE is less than TRUE ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading