select * from OrderPolicyDetail order by ( case Project when 'C' then 1, when 'A' then 2, when 'D' then 3, when 'B' then 4 else '' end ) 当字段类别少时,可以利用case when排序字段
SQL Server获得排名或排序的函数有如下几种: 1.Rank():在结果集中每一条记录所在的排名位置,但排名可能不连续,例如:若同一组内有两个第一名,则该组内下一个名次直接跳至第三名 select *,Rank() over (partition by modifieddate order by locationid ) as Rank from Production.ProductInventory 2.Dense_Rank():功能与Rank相似,但排名的数值是连续的,例如:若同
一.写在前面 最近公司进行开发环境升级,数据库也准备了一个新的服务器,一切准备好后开始数据迁移,采取的方式为对现有Database(现有服务器Windows Server 2003 + SQL Server 2005)进行Back up,然后在新服务器(Windows Server 2008 R2 + SQL Server 2008 R2)上再Restore Databse,一切似乎挺顺利的.但是当从Web Server开始访问的时候出现错误Cannot resolve the collatio
在SQL Server中有4个排序函数:ROW_NUMBER().RANK().DENSE_RANK()及NTILE()函数. 1. ROW_NUMBER()函数 ROW_NUMBER()函数为每条记录添加递增的顺序数值序号,即使存在相同的值也递增序号. 示例: SELECT ROW_NUMBER() OVER (ORDER BY [CategoryID] DESC) AS ROWID, [CategoryID], [CategoryName] FROM [dbo].[Category] WIT
>>>>英文版 (更简洁易懂)<<<< 转载自:https://dzone.com/articles/difference-between-rownumber One of the most obvious and useful set of window functions are ranking functions where rows from your result set are ranked according to a certain sche
目前工作中遇到一个情况,需要将SQL Server中的一个字段提取拼音的首字母,字段由汉字.英文.数字以及“-”构成,百度了一堆,找到如下方法,记录一下,以备后用! 首先建立一个函数 --生成拼音首码 )) ) --WITH ENCRYPTION as begin declare @intLen int ) ) set @intLen = len(@str) set @strRet = '' begin set @temp = '' select @temp = case ) >= '帀' th
采用c#开发dll,并添加到sql server 中. 具体代码,可以用visual studio的向导生成模板. using System; using System.Collections; using System.Data; using Microsoft.SqlServer.Server; using System.Data.SqlTypes; using System.IO; using System.Text; [Serializable] [Microsoft.SqlServer
SELECT id , rownum FROM test ))),) ASC 1.如果想要根据字符型的数字排序,不建议转换成数字类型在order by,因为数据存在字符时,可能转换失败. 2.使用RIGHT方法,格式化字符串,如上述代码所示,假设rownum字段长度为10,数据中包含'1','11','tt',转换后为'0000000001','0000000011','00000000tt'
sql server 字段合并(CAST) ---------------------- select (CAST(org_no AS VARCHAR(20))+CAST(page_no AS VARCHAR(20))+CAST(djlb_no AS VARCHAR(20))) from ERP_BASIC_BILLNO_PREFIX where org_num=#{orgNum} and page_name=#{pageName} and djlb_name=#{djlbName};") 字段
更改数据库的排序规则,SQL提示 5030 的错误,错误信息如下: The database could not be exclusively locked to perform the operation. 解决方法: 1.设置数据库的 Ristrict Access 为 SINGLE_USER. 2.执行下列语句更改排序规则 USE master GO ALTER DATABASE 'your db name' COLLATE Chinese_PRC_CI_AS GO 3.上述命令执行成功之
有时候一张表的一个字段是以逗号分割的一个字符串,分割的数字是明细表的主键id. 关联明细表查询可以这样做: ) ) --这是把areanos字段赋值给@areanos变量 set @areanos='1,2,3' --将拼接的SQL脚本赋值给变量 set @str='select * from area where areano in ('+@areanos+') order by areano' --调用系统存储过程,执行SQL exec sp_executesql @str