I have a String text in my table.
- 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
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).
- January 31, 2018
- March 31, 2018
- October 31, 2018
- December 31, 2018
3条答案
按热度按时间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:
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.
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:
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.
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 (forSeptember
), we need to define 7 search pattern for using in PATINDEX, corresponding from the shortest date value to the longest date value.You could check demo query here .