Click here to Skip to main content
16,016,140 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

i want a query for Order by clause in sql with starting character as new row.

ColumnName
Austrilia
A1
Colombo
Brazil

OutPut:

ColumnName
A
A1
Austrilia
B
Brazil
C
Colombo


Select query for adding a new row with starting character "A,B,C,D etc"
Posted

SQL
WITH CTE
AS
(
    Select 'Austrilia' as Country
    UNION
    Select 'A1' as country
    UNION
    Select 'Colombo' as country
    UNION
    SELECT 'Brazil' as country
),

CTEModified
AS
(
    Select SUBSTRING(country, 1, 1) as country FROM CTE
    Union
    SElect country from CTE
)

SELECT * FROM CTEModified
 
Share this answer
 
Try this:
SQL
DECLARE @tmp TABLE (Country VARCHAR(30))

INSERT INTO @tmp (Country)
VALUES ('Austrilia'), ('A1'), ('Colombo'), ('Brazil')

SELECT Country
FROM (
    SELECT Country
    FROM @tmp
    UNION ALL
    SELECT LEFT(Country,1) AS Country
    FROM @tmp
) AS T
ORDER BY Country
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900