Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one dropdownlist and i will get the data from database A to Z alphabets. How to write sql stored procedure and i would like to get letter 'k' comes to last place remaining must come in alphabet order
Posted

Option A:
If you would like to get letter 'K' as a last sign in a string:
SQL
SELECT [K], [FieldWithLetterK]
FROM (SELECT 'K' AS [K], [FieldWithLetterK]
		FROM [Database1].[dbo].[Table1]
		WHERE RIGHT(RTRIM([FieldWithLetterK]),1)= 'K') AS T
ORDER BY T.[FieldWithLetterK]

Rows count: 555K
Time of execution: 0 s.

If you have similar data in yor table (more then one time) , you can show only non-duplicates in this way:
SQL
SELECT [K], [FieldWithLetterK]
FROM (SELECT 'K' AS [K], [FieldWithLetterK]
        FROM [Database1].[dbo].[Table1]
        GROUP BY [FieldWithLetterK]
        HAVING COUNT([FieldWithLetterK])>=1 AND RIGHT(RTRIM([FieldWithLetterK]),1)= 'K'
        ) AS T
ORDER BY T.[FieldWithLetterK]

Rows count: 555K
Time of execution: 1 s.

Option B:
But, if you would like to get letter 'K' as a first sign in a string, replace RIGHT(RTRIM([FieldWithLetterK]),1)= 'K' with LEFT(LTRIM([FieldWithLetterK]),1)= 'K'.

Option C:
You can use Like operator.
SQL
SET ANSI_NULLS ON
GO
CREATE PROCEDURE GetDataLike 
	-- Add the parameters for the stored procedure here
	@inputstring nvarchar(10) = '*'
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT *
	FROM Database1.dbo.Table1
	WHERE [Field1] Like  @inputstring + '%'
END
GO
 
Share this answer
 
v6
Comments
[no name] 8-May-12 9:21am    
I think OP means K coming last in the list not ends with K... Could be wrong but that's how I interpret it.
Maciej Los 8-May-12 9:45am    
Wes, maybe you are right. Thank you for your reply. Now, we can only wait...
Sandeep Mewara 9-May-12 3:05am    
5 for the super effort.
Maciej Los 9-May-12 3:20am    
Thank you ;)
i would like to get letter 'k' comes to last place remaining must come in alphabet order
I would suggest to do this change in your code and not in SQL. It would be more costly at DB level. Further, it would be easy to do in code. Just bring the data in alphabetical order. In code, find all the values starting from K. Shift them to bottom in a loop.
 
Share this answer
 
Comments
Maciej Los 8-May-12 9:17am    
Sandeep, the OP want "(...) to get letter 'k'" as a "last" sign in a string or am i wrong?
And another one thing... SQL query is very quick. Take a look at my answer.
Sandeep Mewara 8-May-12 12:11pm    
I read it as 'words starting from letter K to be at the end of the word series'
Maciej Los 8-May-12 15:17pm    
Thank you for your reply. English is not my native language ;) Probably it's visible ;)
Sandeep Mewara 9-May-12 0:55am    
Thats ok. The confusion you had is pretty much possible.
Jörgen Andersson 9-May-12 3:08am    
It's not necessarily more costly at the DB level.
If you add a computed column and index it (SQLServer) or add a function based index (Oracle) you could get your sorting done in the database faster than in the application.
But I would only do it if I had large amounts of data.

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