久久久久久久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?)
主站蜘蛛池模板: 五月天天丁香婷婷在线中 | 日韩综合在线 | 亚洲视频一区 | 亚洲三区在线 | 激情av免费看 | 天天综合久久 | 亚洲精品电影网在线观看 | 国产精品日韩欧美一区二区 | 欧美精品一区二区三区四区 在线 | 视频在线日韩 | 亚洲国产日韩欧美 | 精品欧美一区免费观看α√ | 亚洲成av | 中文字幕91| 亚洲一区二区三区在线视频 | 99精品网 | 99热在线播放 | 亚洲视频免费在线观看 | 全免费a级毛片免费看视频免费下 | 欧洲视频一区二区 | 成人午夜在线 | 日日干夜夜干 | 午夜免费在线电影 | 一级毛片免费看 | 久久精品视频在线观看 | 久久精品亚洲 | 日本亚洲精品成人欧美一区 | 欧美成人精品在线观看 | 免费视频一区 | av一级毛片| 天天躁日日躁狠狠很躁 | 一区二区三区欧美 | 亚洲精品第一国产综合野 | 一区二区三区久久久 | 亚洲综合在 | www.久久 | 欧美片网站免费 | 孕妇一级毛片 | av网站免费 | 久久综合成人精品亚洲另类欧美 | 精品国产一区二区三区免费 |