我不太喜欢数据库,我发现在查询上出现以下问题SQL服务器数据库旧的遗留应用程序的。我声明不幸的是我无法更改数据库结构\字段类型(这非常难看)
我有以下情况:
SELECT [Sottocategoria]
,[IdSottocategoria]
,[IdCategoria]
,[Note]
FROM [dbo].[PROT_TITOLARIO]
where IdCategoria = '5'
order by IdSottocategoria
我得到了这样的东西:
我已经突出显示了我的输出错误。正如你所看到的,我正在尝试按类别字段(定义为varchar).
以这种方式排序是使用字母顺序排序(而不是根据我的需要将其视为数字)。
在实践中我需要一个递增的顺序类别像这样的字段:
5.0 --> 5.1 --> 5.2 --> 5.3 --> ............... --> 5.10 --> 5.11 --> ...........
请注意,这类别字段还可以包含以下值:5.0.0 or 5.0.1或更高“级别”为5.0.1.3.4
我该如何尝试处理这种情况并实现这种行为?
EDIT 1:我正在添加数据:
USE [MY_DB]
GO
/****** Object: Table [dbo].[PROT_TITOLARIO] Script Date: 8/7/2019 11:38:11 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[PROT_TITOLARIO](
[IdSottocategoria] [varchar](50) NOT NULL,
[Sottocategoria] [varchar](500) NOT NULL,
[IdCategoria] [varchar](50) NOT NULL,
[Note] [varchar](max) NULL,
CONSTRAINT [PK_PROT_TITOLARIO_NEW] PRIMARY KEY CLUSTERED
(
[IdSottocategoria] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'0', N'HR', N'IWG', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'0.0', N'CV', N'0', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'0.1', N'test', N'0', NULL)
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'0.1.0', N'ghjghjg', N'0.1', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'0.1.0.0', N'ghghg', N'0.1.0', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'0.1.0.0.0', N'sdf', N'0.1.0.0', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'0.1.0.0.0.0', N'fsdfsdfsd', N'0.1.0.0.0', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'0.1.0.0.0.0.0', N'dgdgdfg', N'0.1.0.0.0.0', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'0.1.0.0.0.0.1', N'uiouoi', N'0.1.0.0.0.0', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'0.1.0.0.0.0.2', N'yrtyrtyrtyrt', N'0.1.0.0.0.0', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'0.1.0.0.1', N'HR - fghfhfghf', N'0.1.0.0', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'0.2', N'bla', N'0', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'0.3', N'AAA', N'0', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'0.4', N'dasdas', N'0', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'0.5', N'fghfghfgh', N'0', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'0.6', N'HR - TEST PRIMO LIVELLO', N'0', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'0.6.0', N'HR - TEST SECONDO LIVELLO', N'0.6', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'0.6.0.0', N'HR - TEST TERZO LIVELLO', N'0.6.0', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'1', N'Commerciale', N'IWG', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'1.1', N'XXX', N'1', NULL)
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'1.1.0', N'Commerciale - bla', N'1.1', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'1.2', N'YYY', N'1', NULL)
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'1.3', N'ZZZ', N'1', NULL)
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'1.4', N'Commerciale - dsasdasd', N'1', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'2', N'Titolario - TEST LIVELLO 0', N'IWG', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'2.0', N'bhjbhjbhbjb', N'2', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'2.0.0', N'pippo', N'2.0', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'2.0.1', N'Titolario - TEST LIVELLO 0 - dfgdfgdfg', N'2.0', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'3', N'Titolario - TEST 2 LIVELLO 0', N'IWG', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'4', N'TEST 3 LIVELLO 0', N'IWG', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'4.0', N'TEST 3 LIVELLO 0 - LIVELLO 1', N'4', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'4.0.0', N'TEST 3 LIVELLO 0 - LIVELLO 2', N'4.0', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'4.0.0.0', N'TEST 3 LIVELLO 0 - LIVELLO 3', N'4.0.0', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'4.0.1', N'TEST 3 LIVELLO 0 - DASDASDAS', N'4.0', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'5', N'ORDINAMENTO', N'IWG', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'5.0', N'ORDINAMENTO - ORD 0', N'5', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'5.0.0', N'ORDINAMENTO - test', N'5.0', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'5.1', N'ORDINAMENTO - ORD 1', N'5', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'5.10', N'ORDINAMENTO - ORD 10', N'5', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'5.11', N'ORDINAMENTO - ORD 11', N'5', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'5.12', N'ORDINAMENTO - ORD 12', N'5', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'5.2', N'ORDINAMENTO - ORD 2', N'5', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'5.3', N'ORDINAMENTO - ORD 3', N'5', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'5.4', N'ORDINAMENTO - ORD 4', N'5', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'5.5', N'ORDINAMENTO - ORD 5', N'5', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'5.6', N'ORDINAMENTO - ORD 6', N'5', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'5.7', N'ORDINAMENTO - ORD 7', N'5', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'5.8', N'ORDINAMENTO - ORD 8', N'5', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'5.9', N'ORDINAMENTO - ORD 9', N'5', N'')
GO
INSERT [dbo].[PROT_TITOLARIO] ([IdSottocategoria], [Sottocategoria], [IdCategoria], [Note]) VALUES (N'IWG', N'Titolario', N'-', N'Categoria')
GO
*