SQL Server how to get the date value inside string text in sql 2014 query

zf9nrax1  于 2023-02-18  发布在  其他
关注(0)|答案(3)|浏览(106)

I have a String text in my table.

  1. Salaries and wages for the period ending January 31, 2018
  2. Salaries and wages for the period ending March 31, 2018
  3. Salaries and wages for the period ending October 31, 2018
  4. Salaries and wages for the period ending December 31, 2018

How can I get the Date only in the last?

I've already tried the Parse() function but it did'nt work.

I'm expecting to get the Date Only(in any format).

  1. January 31, 2018
  2. March 31, 2018
  3. October 31, 2018
  4. December 31, 2018
xriantvc

xriantvc1#

If the string in your table are always in the same format starting with phrase:
Salaries and wages for the period ending

than the most simpole way to achive this is:

Create table table1(text1 nvarchar(100)) 
insert into table1 values (
'Salaries and wages for the period ending January 31, 2018')
insert into table1 values (
'Salaries and wages for the period ending March 31, 2018')
insert into table1 values (
'Salaries and wages for the period ending October 31, 2018')
insert into table1 values (
'Salaries and wages for the period ending December 31, 2018')

select Convert(date,REPLACE(text1,'Salaries and wages for the period ending ',''))  from table1

But if date is at the end in format:

March 31, 2018

then You must find a way to get last 3 words in this string. I will edit my answer if that is what You need.

ruarlubt

ruarlubt2#

If you are happy with the format of the dates as you already have them in your strings, it simplifies the problem to just removing the redundant text.

You can try this:

SELECT REPLACE(YourColumn, 'Salaries and wages for the period ending ', '') FROM YourTable

Updated to handle variable text strings as per new information:

create table #temp (data varchar(100))

insert into #temp values
    ('Salaries and wages for the period ending January 31, 2018'),
    ('Salaries and wages for the period ending March 31, 2018'),
    ('Salaries and wages for the period ending October 31, 2018'),
    ('Salaries and wages for the period ending December 31, 2018'),
    ('This string looks completely different July 31, 2019')

SELECT REVERSE(SUBSTRING(REVERSE(data), 1, CHARINDEX(' ', REVERSE(data), CHARINDEX(' ', REVERSE(data), CHARINDEX(' ', REVERSE(data)) + 1) + 1) - 1)) AS Date
FROM #temp

This will look for the third space from the end of the string and use it as a delimiter to extract the date.

As mentioned in the comments, SQL Server is not the ideal platform for text manipulation though, so consider whether this can be done more efficiently earlier in your pipeline.

uqzxnwby

uqzxnwby3#

You could use PATINDEX function to find index of date values in your text, then use SUBSTRING function to extract these values.

Since name of the months are vary in length, from 3 letters (for May ) to 9 letters (for September ), we need to define 7 search pattern for using in PATINDEX, corresponding from the shortest date value to the longest date value.

WITH data AS 
         (SELECT 'Salaries and wages for the period ending January 31, 2018' AS text    
UNION ALL SELECT 'Salaries and wages for the period ending March 31, 2018' AS text
UNION ALL SELECT 'Salaries and wages for the period ending May 31, 2018' AS text 
UNION ALL SELECT 'Salaries and wages for the period ending September 30, 2018' AS text   
UNION ALL SELECT 'Salaries and wages for the period ending October 31, 2018' AS text
UNION ALL SELECT 'Salaries and wages for the period ending December 31, 2018' AS text),
     intermediary AS  
       (SELECT text,
          PATINDEX('% [A-S]__ [0-9][0-9], [1-9][0-9][0-9][0-9]', text) AS index1,
          PATINDEX('% [A-S]___ [0-9][0-9], [1-9][0-9][0-9][0-9]', text) AS index2,
          PATINDEX('% [A-S]____ [0-9][0-9], [1-9][0-9][0-9][0-9]', text) AS index3,
          PATINDEX('% [A-S]_____ [0-9][0-9], [1-9][0-9][0-9][0-9]', text) AS index4,
          PATINDEX('% [A-S]______ [0-9][0-9], [1-9][0-9][0-9][0-9]', text) AS index5,
          PATINDEX('% [A-S]_______ [0-9][0-9], [1-9][0-9][0-9][0-9]', text) AS index6,
          PATINDEX('% [A-S]________ [0-9][0-9], [1-9][0-9][0-9][0-9]', text) AS index7,
          LEN(text) AS length                                                
       FROM data)
SELECT text,
       CASE 
         WHEN  index1 > 0 THEN SUBSTRING(text, index1, length)
         WHEN  index2 > 0 THEN SUBSTRING(text, index2, length)
         WHEN  index3 > 0 THEN SUBSTRING(text, index3, length)
         WHEN  index4 > 0 THEN SUBSTRING(text, index4, length)
         WHEN  index5 > 0 THEN SUBSTRING(text, index5, length)
         WHEN  index6 > 0 THEN SUBSTRING(text, index6, length)
         WHEN  index7 > 0 THEN SUBSTRING(text, index7, length)
         ELSE ''
       END AS extracted_date  
FROM intermediary;

You could check demo query here .

相关问题