久久久久久久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性色| 国产综合精品一区二区三区 | 国产精品一区三区 | 在线观看成人精品 | 久久久久久国产精品 | 午夜精品久久久久久久久久久久 | 国产一区二区电影 | 国产精品毛片一区二区在线看 | 免费播放一级片 | 欧美成人精品一区二区三区 | 国产欧美日韩在线播放 | 中文字幕免费视频 | 国产特一级黄色片 | 午夜精品久久久久99蜜 | 中文字幕第100页 | 日韩免费视频 | 国产精品久久久久久一区二区三区 | 男女网站免费观看 | 黄色在线免费观看视频网站 | 国产伦精品一区二区三区四区视频 | 欧美1区2区 | 国产精品99久久久久久宅男 | 一区二区三区在线免费观看 | 日本欧美国产在线 | 国产激情视频在线观看 | 国产精成人 | 久久99精品久久久久久琪琪 | 久久精品亚洲精品 | 特黄色毛片 | 99精品欧美一区二区三区 | 观看av| 色噜噜色综合 | 国产网站在线 | 亚洲 欧美 日韩 在线 | 久久婷婷国产香蕉 | 亚洲欧美一区二区三区1000 |