久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

確定列值(SQL)中最后一個大寫字母的索引?

Determining index of last uppercase letter in column value (SQL)?(確定列值(SQL)中最后一個大寫字母的索引?)
本文介紹了確定列值(SQL)中最后一個大寫字母的索引?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

簡短版本: 有沒有一種方法可以根據該值中最后一個大寫字母的索引(位置)輕松提取和 ORDER BY 數據庫列中的值的子字符串, 使用 SQL?

長版本:我有一個帶有用戶名字段的表,用戶名的約定是名字的首字母大寫,然后是姓氏的首字母大寫,然后是其余的姓氏.因此,按用戶名字段排序是錯誤的".按用戶名值的子字符串排序理論上可行,例如

SUBSTRING(username,2, LEN(username))

...除了在其他兩個首字母之間有大寫中間首字母的值.我很想知道僅使用 SQL(MS SQL Server)是否有一種相當直接/簡單的方法:

  1. 測試 DB 值中字符的大小寫(并返回一個布爾值)
  2. 確定字符串值中最后一個大寫字符的索引

假設這甚至是遙不可及的,我認為人們必須遍歷每個用戶名的各個字母才能完成它,使其效率極低,但如果您有一個神奇的快捷方式,請隨時分享.注意:這個問題純粹是學術性的,因為我決定采用更簡單的方法.我只是好奇這是否可能.

解決方案

  1. 測試 DB 值中字符的大小寫(并返回一個布爾值)

SQL Server 沒有布爾數據類型.bit 經常被用來代替它.

DECLARE @Char CHAR(1) = 'f'選擇演員表(案例當@Char LIKE '[A-Z]' COLLATE Latin1_General_Bin那么 1其他 0以位結束)/* 返回 0 */

請注意,在上述語法中使用二進制排序規則而不是區分大小寫的排序規則子句很重要.如果使用 CS collat??e 子句,則需要將模式完整拼寫為 '[ABCDEFGHIJKLMNOPQRSTUVWXYZ]' 以避免匹配小寫字符.

<塊引用>

  1. 確定字符串值中最后一個大寫字符的索引

SELECT PATINDEX('%[A-Z]%' COLLATE Latin1_General_Bin, REVERSE('Your String'))/* 返回一個基于索引 (6) */SELECT PATINDEX('%[A-Z]%' COLLATE Latin1_General_Bin, REVERSE('無大寫'))/* 如果測試字符串不包含 A-Z 范圍內的任何字母,則返回 0 */

<塊引用>

  1. 根據您可以使用的規則提取姓氏

SELECT RIGHT(Name,PATINDEX('%[A-Z]%' COLLATE Latin1_General_Bin ,REVERSE(Name)))從你的桌子

Short version: Is there a way to easily extract and ORDER BY a substring of values in a DB column based on the index (position) of the last upper case letter in that value, only using SQL?

Long version: I have a table with a username field, and the convention for usernames is the capitalized first initial of the first name, followed by the capitalized first initial of the last name, followed by the rest of the last name. As a result, ordering by the username field is 'wrong'. Ordering by a substring of the username value would theoretically work, e.g.

SUBSTRING(username,2, LEN(username))

...except that there are values with a capitalized middle initials between the other two initials. I am curious to know if, using only SQL (MS SQL Server,) there is a fairly straightforward / simple way to:

  1. Test the case of a character in a DB value (and return a boolean)
  2. Determine the index of the last upper case character in a string value

Assuming this is even remotely possible, I assume one would have to loop through the individual letters of each username to accomplish it, making it terribly inefficient, but if you have a magical shortcut, feel free to share. Note: This question is purely academic as I have since decided to go a much simpler way. I am just curious if this is even possible.

解決方案

  1. Test the case of a character in a DB value (and return a boolean)

SQL Server does not have a boolean datatype. bit is often used in its place.

DECLARE @Char CHAR(1) = 'f'

SELECT CAST(CASE
              WHEN @Char LIKE '[A-Z]' COLLATE Latin1_General_Bin
                THEN 1
              ELSE 0
            END AS BIT) 
 /* Returns 0 */

Note it is important to use a binary collation rather than a case sensitive collate clause with the above syntax. If using a CS collate clause the pattern would need to be spelled out in full as '[ABCDEFGHIJKLMNOPQRSTUVWXYZ]' to avoid matching lower case characters.

  1. Determine the index of the last upper case character in a string value

SELECT PATINDEX('%[A-Z]%' COLLATE Latin1_General_Bin, REVERSE('Your String'))
/* Returns one based index (6 ) */

SELECT PATINDEX('%[A-Z]%' COLLATE Latin1_General_Bin, REVERSE('no capitals'))
/* Returns 0 if the test string doesn't contain any letters in the range A-Z */

  1. To extract the surname according to those rules you can use

SELECT RIGHT(Name,PATINDEX('%[A-Z]%' COLLATE Latin1_General_Bin ,REVERSE(Name)))
FROM YourTable

這篇關于確定列值(SQL)中最后一個大寫字母的索引?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What SQL Server Datatype Should I Use To Store A Byte[](我應該使用什么 SQL Server 數據類型來存儲字節 [])
Interpreting type codes in sys.objects in SQL Server(解釋 SQL Server 中 sys.objects 中的類型代碼)
Typeorm .loadRelationCountAndMap returns zeros(Typeorm .loadRelationCountAndMap 返回零)
MS SQL: Should ISDATE() Return quot;1quot; when Cannot Cast as Date?(MS SQL:ISDATE() 是否應該返回“1?什么時候不能投射為日期?)
Converting the name of a day to its integer representation(將一天的名稱轉換為其整數表示)
How to convert nvarchar m/d/yy to mm/dd/yyyy in SQL Server?(如何在 SQL Server 中將 nvarchar m/d/yy 轉換為 mm/dd/yyyy?)
主站蜘蛛池模板: 欧美精品91| 亚洲精品久久久一区二区三区 | 看av电影 | 一级做a爰片久久毛片免费看 | 99re99| 中国一级特黄真人毛片 | 欧美精品一二三 | 日韩免费 | 日韩视频免费看 | 亚洲欧美日韩国产综合 | 国产欧美一区二区三区在线看 | 欧美精品在线免费观看 | 精品av天堂毛片久久久借种 | 免费人成在线观看网站 | 超碰精品在线 | 国产日韩中文字幕 | 综合一区二区三区 | 欧美区在线| 日韩国产精品一区二区三区 | 国产69久久精品成人看动漫 | 免费v片| 亚洲国产一区二区视频 | 久久精品视频9 | 日本免费一区二区三区四区 | 日韩不卡在线观看 | 国产免费观看一区 | 91欧美精品成人综合在线观看 | 精品伊人久久 | 欧美情趣视频 | 成人午夜看片 | 黄免费观看视频 | 欧美精品久久久久 | 国产精品久久久久久av公交车 | 亚洲另类自拍 | 日韩影院在线观看 | 国产成人精品久久二区二区91 | 99re国产精品 | 日本黄色免费大片 | 99精品欧美一区二区三区综合在线 | 97视频在线观看网站 | 日韩视频国产 |