Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to search for a string within a string 2

Status
Not open for further replies.

iwm

Programmer
Feb 7, 2001
55
US
There is a string I need to parse to create several columns. I am using the following statement to create the employeeID column and the Import Date column:

select Substring(rowdata, 1, 9) as 'EmployeeID',
substring (rowdata, 11, 8) as 'Import Date'
from livereqlog

Example of string to be parsed:

rowdata
123456789~20160502~Thomas
[blank]~20160701~Richardson

Example current of output:
wrong_output_vwe1bu.png



This statement doesnt return the desired output when the first 9 characters of the rowdata column are populated with [blank]. If the EmployeeID is blank I would like to return the word 'blank' and if it is populated with a number I would like to return the number.

Example of desired output:

desired_output_xts4y9.png


Thank you in advance for your assistance.

Ingrid
 
You may also check the Length of your rowdata field and determine this way if you have a blank EmployeeID or not.

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
If you don't want to write your own Split function (Why isn't Split a native T-SQL function?), you could repurpose the built-in PARSENAME function...

Code:
;WITH t AS (
SELECT REPLACE(rowdata, '~', '.') DotData
  FROM livereqlog
)

SELECT PARSENAME(DotData, 3) EmployeeID,
       PARSENAME(DotData, 2) [Import Date]
  FROM t
 
fredericofonseca, Thanks for sharing. That looks like a very interesting setup.

And thanks to OlafDoschke for mentioning that Split now exists in SQL 2016. Good to know for future reference.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top