This is what I'm getting
And this is how I want
Query that I use:
select *
from view_abc
order by col1
Use a function to strip out the non numeric characters and leave just the value. Use another function to strip out all the numeric data. You can then sort on the two returned values.
It seems like a bit of work at first but once the functions are in you can re-use them in the future. Here's two functions I use regularly when we get data in from external sources and it's not very normalised.
They may not be the most efficient functions in the world but they work for my purposes
1st a function to just leave the numeric portion.
CREATE FUNCTION [fn].[StripToAlpha]
(
@inputString nvarchar(4000)
)
RETURNS varchar(4000)
AS
BEGIN
DECLARE @Counter as int
DECLARE @strReturnVal varchar(4000)
DECLARE @Len as int
DECLARE @ASCII as int
SET @Counter=0
SET @Len=LEN(@inputString)
SET @strReturnVal = ''
WHILE @Counter<=@Len
BEGIN
SET @Counter = @Counter +1
SET @ascii= ASCII(SUBSTRING(@inputString,@counter,1))
IF(@ascii BETWEEN 65 AND 90) OR (@ascii BETWEEN 97 AND 122)
BEGIN
SET @strReturnVal = @strReturnVal + (SUBSTRING(@inputString,@counter,1))
END
END
RETURN @strReturnVal
END
2nd a function to extract the value from a text field , this also handle percentages (eg abc 23% comes out as 0.23) but this is not required in your case.
You'll need to CREATE an 'fn' schema of change the schema name first...
CREATE FUNCTION [fn].[ConvertToValue]
(
@inputString nvarchar(4000)
)
RETURNS Float
AS
BEGIN
DECLARE @Counter as int
DECLARE @strReturnVal varchar(4000)
DECLARE @ReturnVal Float
DECLARE @Len as int
DECLARE @ASCII as int
SET @Counter=0
SET @Len=LEN(@inputString)
SET @strReturnVal = ''
IF @inputString IS NULL
BEGIN
Return NULL
END
IF @Len = 0 OR LEN(LTRIM(RTRIM(@inputString))) = 0
BEGIN
SET @ReturnVal=0
END
ELSE
BEGIN
WHILE @Counter<=@Len
BEGIN
SET @Counter = @Counter +1
SET @ascii= ASCII(SUBSTRING(@inputString,@counter,1))
IF(@ascii BETWEEN 48 AND 57) OR (@ascii IN (46,37))
BEGIN
SET @strReturnVal = @strReturnVal + (SUBSTRING(@inputString,@counter,1))
END
END
if RIGHT(@strReturnVal,1)='%'
BEGIN
SET @strReturnVal = LEFT(@strReturnVal,len(@strReturnVal)-1)
SET @strReturnVal = CAST((CAST(@strReturnVal AS FLOAT)/100) AS nvarchar(4000))
END
SET @ReturnVal = ISNULL(@strReturnVal,0)
END
RETURN @ReturnVal
END
Now we have the two functions created you can simply do
SELECT *
FROM view_abc
ORDER BY fn.StripToAlpha(Col1), fn.ConvertToValue(Col1)
Try this
Edited :
SELECT CAST(SUBSTRING(ColumnNameToOrder, CHARINDEX(' ', ColumnNameToOrder, 0), LEN (ColumnNameToOrder)) AS INT) AS IntColumn, SUBSTRING(ColumnNameToOrder,0, CHARINDEX(' ', ColumnNameToOrder, 0)) AS CharColumn, * FROM view_abc ORDER BY Charcolumn, Intcolumn
Instead of ColumnNameToOrder, you can put your column name which contains the data like 'abc 123'... Tell me if it works please.
This is what I have come up with. Maybe it can help you, or at least point you in the right direction.
I tested the values. When the values have a zero , the order is like you would like the order to be. Like this:
So you can run this query to update the existing values, to add the zero .
UPDATE abc
SET col1 = 'abc 0' + SUBSTRING(col1, 5, 1)
WHERE LEN(col1) = 5
Or you can do the above query like this if the first three characters can vary:
UPDATE abc
SET col1 = (SUBSTRING(col1, 1, 3) + ' 0' + SUBSTRING(col1, 5, 1))
WHERE col1 LIKE 'abc__'
This will override the existing value in the col1 column, only when the length of the current String is of length 5.
Then you can run the following query to get the results:
SELECT col1
FROM abc
ORDER BY col1 ASC
=cint(right(Fields!Col1.Value, instrrev(Fields!Col1.Value, " ")-1))
这将在SSRS中工作并正确排序,但是仅在Col1始终包含空格并且空格之后的字符可以转换为整数的情况下才有效。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.