Mysql

一個字元串中的字元串連接和日期/數字序數?

  • June 4, 2021

如何將函式與字元串結合起來,在一句話中得到一個整體?

我的意思是這樣的(預期結果):

今天是一年中的第三天,一年中的第二周和一周的第一天。

下面是我的程式碼,但它將函式分成單獨的欄位,我真的不知道如何將字元串放入其中:

dayofyear(current_date()), weekofyear(current_date()), dayofweek(current_date()); 

這是翻譯和顯示邏輯。您正在做的是標記一個數字以用一種語言顯示:英語。雖然這可以完成工作,但這是一個不好的做法。您可能想要的是一個支持

您可以使用 React 文件中的 ICU MessageFormatselectordinal在這裡看到它想要什麼(它實際上是一個範例)。Angular也支持這一點。如果您使用支持這一點的框架,您可以用更少的工作支持多種語言。採用一個雖然需要將數字傳遞給框架。這也使這些冗長的英語(非字元串)操作完全脫離了 SQL。

我會用一個函式來解決這個問題,否則你必須用 case when 子句檢查每個數字,就像在最後一個查詢中看到的那樣

CREATE FUNCTION ending( _num INT )
RETURNS VARCHAR(2)
DETERMINISTIC
BEGIN
DECLARE _ending VARCHAR(2);
SET _ending := 


  CASE RIGHT(_num,1)
      WHEN '1' THEN  IF(LENGTH(_num)  = 1,'st',IF(MID(_num,-2, 1) = 1,'th','st'))
      WHEN '2' THEN  IF(LENGTH(_num)  = 1,'nd',IF(MID(_num,-2, 1) = 1,'th','nd'))
      WHEN '3' THEN  IF(LENGTH(_num)  = 1,'rd',IF(MID(_num,-2, 1) = 1,'th','rd'))
      ELSE  'th'
  END;
RETURN(_ending);
END
SELECT CONCAT('Today is the ',dayofyear(current_date()),ending(dayofyear(current_date()))
,' day of the year, the ', weekofyear(current_date()),ending(weekofyear(current_date()))
,' week of the year and the ',dayofweek(current_date()),ending(dayofweek(current_date())),' day of the week.') As mytext; 
| 正文 |
| :----------------------------------------------------------------------------------------- |
| 今天是一年中的第 151 天,一年中的第 22 周和一周的第 2 天。|
SELECT 
    CONCAT('Today is the ',
            DAYOFYEAR(CURRENT_DATE()),
            CASE RIGHT(DAYOFYEAR(CURRENT_DATE()), 1)
      WHEN '1' THEN  IF(LENGTH(CURRENT_DATE())  = 1,'st',IF(MID(CURRENT_DATE(),-2, 1) = 1,'th','st'))
      WHEN '2' THEN  IF(LENGTH(CURRENT_DATE())  = 1,'nd',IF(MID(CURRENT_DATE(),-2, 1) = 1,'th','nd'))
      WHEN '3' THEN  IF(LENGTH(CURRENT_DATE())  = 1,'rd',IF(MID(CURRENT_DATE(),-2, 1) = 1,'th','rd'))
                ELSE 'th'
            END,
            ' day of the year, the ',
            WEEKOFYEAR(CURRENT_DATE()),
            CASE RIGHT(WEEKOFYEAR(CURRENT_DATE()), 1)
      WHEN '1' THEN  IF(LENGTH(CURRENT_DATE())  = 1,'st',IF(MID(CURRENT_DATE(),-2, 1) = 1,'th','st'))
      WHEN '2' THEN  IF(LENGTH(CURRENT_DATE())  = 1,'nd',IF(MID(CURRENT_DATE(),-2, 1) = 1,'th','nd'))
      WHEN '3' THEN  IF(LENGTH(CURRENT_DATE())  = 1,'rd',IF(MID(CURRENT_DATE(),-2, 1) = 1,'th','rd'))
                ELSE 'th'
            END,
            ' week of the year and the ',
            DAYOFWEEK(CURRENT_DATE()),
            CASE RIGHT(DAYOFWEEK(CURRENT_DATE()), 1)
      WHEN '1' THEN  IF(LENGTH(DAYOFWEEK(CURRENT_DATE()))  = 1,'st',IF(MID(DAYOFWEEK(CURRENT_DATE()),-2, 1) = 1,'th','st'))
      WHEN '2' THEN  IF(LENGTH(DAYOFWEEK(CURRENT_DATE()))  = 1,'nd',IF(MID(DAYOFWEEK(CURRENT_DATE()),-2, 1) = 1,'th','nd'))
      WHEN '3' THEN  IF(LENGTH(DAYOFWEEK(CURRENT_DATE()))  = 1,'rd',IF(MID(DAYOFWEEK(CURRENT_DATE()),-2, 1) = 1,'th','rd'))
                ELSE 'th'
            END,
            ' day of the week.') AS mytext; 
| 正文 |
| :----------------------------------------------------------------------------------------- |
| 今天是一年中的第 151 天,一年中的第 22 周和一周的第 2 天。|

db<>在這裡擺弄

引用自:https://dba.stackexchange.com/questions/292493