Option A:
If you would like to get letter 'K' as a last sign in a string:
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:
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.
SET ANSI_NULLS ON
GO
CREATE PROCEDURE GetDataLike
@inputstring nvarchar(10) = '*'
AS
BEGIN
SET NOCOUNT ON;
SELECT *
FROM Database1.dbo.Table1
WHERE [Field1] Like @inputstring + '%'
END
GO