Becky Edwards
asked on
Right Trim or Left Trim a column in SQL up to and including the comma separator
I have read through many explanations and cannot find one that helps me with this. Hopefully others will find the solution useful also.
I have a field with lastname, firstname. I need to split these into two separate fields and include consideration for null values.
Null values should display as blank.
The fields must be 15 spaces for last name, 14 spaces for first name, where the name starts at the left of the column and leave empty spaces at the end.
Example:
Smithsonian,Charles would end up as
[Smithsonian ]
[Charles ]
disregard the [ ] those are for explanation only.
Also, when I add this to my existing query I get this error message.
"Msg 116, Level 16, State 1, Line 25
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS."
I am using SQL Server 2008 and VS 2013.
This will one field in the entire query that will eventually end up as a stored procedure to be used in Visual Studio.
I have attached my entire query as I now understand that helps.
PLEASE disregard my attempt to do this inside the existing query, it does not work like it is supposed to. (Column SpouseLN)
metriq_for_EE.sql
I have a field with lastname, firstname. I need to split these into two separate fields and include consideration for null values.
Null values should display as blank.
The fields must be 15 spaces for last name, 14 spaces for first name, where the name starts at the left of the column and leave empty spaces at the end.
Example:
Smithsonian,Charles would end up as
[Smithsonian ]
[Charles ]
disregard the [ ] those are for explanation only.
Also, when I add this to my existing query I get this error message.
"Msg 116, Level 16, State 1, Line 25
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS."
I am using SQL Server 2008 and VS 2013.
This will one field in the entire query that will eventually end up as a stored procedure to be used in Visual Studio.
I have attached my entire query as I now understand that helps.
PLEASE disregard my attempt to do this inside the existing query, it does not work like it is supposed to. (Column SpouseLN)
metriq_for_EE.sql
ASKER
This piece does not work.
,SUBSTRING((@),1,CHARINDEX (',',@)-1) AS PatientFirst
The error is:
Invalid length parameter passed to the LEFT or SUBSTRING function.
Also, is it possible to build this as a separate subquery in the main query, so that I can have the following code included:
from emergency_contacts ec
INNER JOIN ZC_EMERG_PAT_REL ZC ON ec.EMERG_PAT_REL_C = zc.EMERG_PAT_REL_C
where zc.EMERG_PAT_REL_C = 17)
?
Thank you.
,SUBSTRING((@),1,CHARINDEX
The error is:
Invalid length parameter passed to the LEFT or SUBSTRING function.
Also, is it possible to build this as a separate subquery in the main query, so that I can have the following code included:
from emergency_contacts ec
INNER JOIN ZC_EMERG_PAT_REL ZC ON ec.EMERG_PAT_REL_C = zc.EMERG_PAT_REL_C
where zc.EMERG_PAT_REL_C = 17)
?
Thank you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi,
Is the query given above worked for the first part?
Regards,
Pawan
Is the query given above worked for the first part?
Regards,
Pawan
ASKER
Well I thought it was. I was just informed that in order for SSIS to keep the correct amount of characters (or spaces) in a field, we need to use the CAST function.
SELECT
CAST('' as char(5)) AS Hospital_ID --no hospital ids have 5 or less characters
,CAST((p.PAT_LAST_NAME) as char(20)) AS PatientLast
This way the extract displays the correct width for each field.
See attached example.
Now I have to rebuild each of these using CAST.
I still need to split the one field into two, regardless.
Becky
metriq_formatting.docx
SELECT
CAST('' as char(5)) AS Hospital_ID --no hospital ids have 5 or less characters
,CAST((p.PAT_LAST_NAME) as char(20)) AS PatientLast
This way the extract displays the correct width for each field.
See attached example.
Now I have to rebuild each of these using CAST.
I still need to split the one field into two, regardless.
Becky
metriq_formatting.docx
Excellent logic Becky !!
Regards,
Pawan
Regards,
Pawan
Open in new window
O/p
----------
PatientLast PatientFirst
Charles Smithsonian
Try
Open in new window