Solved

Concaenate field that needs to be 30 characters long

Posted on 2014-11-26
6
94 Views
Last Modified: 2014-11-26
Hi there,
I have the following statement:
select 
REPLACE(concat(glje_ref1,glje_ref2, glje_ref3, glje_ref4), ' ', '')
from mytable

Open in new window

and this returns a string with different lengths. What I need to do is to return the first 30 Characters it is ok if the trailing character are cut of
what's the best practice on going about this?
Thanks
0
Comment
Question by:COHFL
  • 2
  • 2
  • 2
6 Comments
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 250 total points
Comment Utility
select LEFT (REPLACE(concat(glje_ref1,glje_ref2, glje_ref3, glje_ref4), ' ', '') , 30 )
0
 

Author Comment

by:COHFL
Comment Utility
Im thinking on
SUBSTRING(REPLACE(concat(glje_ref1,glje_ref2, glje_ref3, glje_ref4), ' ', '')
, 1, 30)

Open in new window

0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 250 total points
Comment Utility
Either of these will work, as they both return a 30-character varchar:
select CAST(REPLACE(concat(glje_ref1,glje_ref2, glje_ref3, glje_ref4), ' ', '') as varchar(30)) 
from mytable

select LEFT(REPLACE(concat(glje_ref1,glje_ref2, glje_ref3, glje_ref4), ' ', ''), 30) 
from mytable

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:COHFL
Comment Utility
they all worked but one thing I guess I did not account it for is when there is not data under any of the refs or the string is less than 30 the replace kick in and remove empty spaces. and if the string is less said 20 characters I need it to be 30 characters long how do I achieve this?
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 250 total points
Comment Utility
select LEFT (REPLACE(concat(glje_ref1,glje_ref2, glje_ref3, glje_ref4), ' ', '')+replicate('-',30 )  , 30 )
0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 250 total points
Comment Utility
>I need it to be 30 characters long
In that case CAST it as a CHAR instead of VARCHAR.  
To demonstrate copy-paste the below code into your SSMS and execute.
Declare @val varchar(50) = '12345678901234567890'

SELECT DATALENGTH(CAST(@val as char(30)))
SELECT DATALENGTH(CAST(@val as varchar(30)))
SELECT DATALENGTH(LEFT(@val, 30))

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now