Solved

Concaenate field that needs to be 30 characters long

Posted on 2014-11-26
6
106 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
ID: 40467268
select LEFT (REPLACE(concat(glje_ref1,glje_ref2, glje_ref3, glje_ref4), ' ', '') , 30 )
0
 

Author Comment

by:COHFL
ID: 40467270
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
ID: 40467290
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:COHFL
ID: 40467304
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
ID: 40467307
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
ID: 40467332
>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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

821 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