Input in November 2019, output: '201911'
Input 'poiisiis201909' output '201909'
CodePudding user response:
If the date is fixed input behind, you can use the: Select right (' dfdsffdfs201911, 6)CodePudding user response:
Date is not fixed position, I this is possibleCodePudding user response:
- remember the junction post
Declare @ STR Varchar (4000)
With TB (a) as (
Select 'asdasdasd January 2019 - on
), the tc as (
Select a, number, number - ROW_NUMBER () over (order by a, number) num
The from TB, master.. Spt_values
Where type='p' and ASCII (SUBSTRING (a, number, 1)) between 48 and 57
), td as (
Select a, b, the substring (a, MIN (number), MAX (number) - MIN (number) + 1). A,
ROW_NUMBER () over (partition by a order by min (number)) num from tc
Group by a, num)
Select @ STR=IsNull (@ STR, ' ') + IsNull (a, ' ') + '/' From td where num<=2
The SET @ STR=REVERSE (@ STR)
SET @ STR=CASE WHEN CHARINDEX ('/', @ STR)=1 THEN STUFF (@ STR, 1, 1, ' ') ELSE @ STR END
The SET @ STR=REVERSE (@ STR)
Select a case when len (@ STR) & lt;> 7 then replace (@ STR, '/', '0') else replace (@ STR, '/', ') end
CodePudding user response:
Only output to the month is
DECLARE @ A VARCHAR (100)
SET @ A='January 2019 asdasdasd
; WITH CTE
AS
(SELECT *, the SUBSTRING (STRING, NUMBER, 1) AS SINGLE_NUM
FROM the MASTER. The DBO. SPT_VALUES A
The JOIN (SELECT @ A AS STRING) B ON NUMBERWHERE TYPE='P' AND NUMBER> 0
AND the ASCII (SUBSTRING (STRING, NUMBER, 1)) BETWEEN 48 AND 59)
SELECT a STRING, LEFT (NUM, 4) + RIGHT (' 0 '+ SUBSTRING (NUM, 5, LEN (NUM)), 2)
The FROM
(SELECT *, (SELECT CAST (SINGLE_NUM AS VARCHAR) + 'FROM CTE ORDER BY NUMBER FOR an XML PATH ('') AS NUM
The FROM CTE A) AS A
GROUP BY STRING, NUM