Solved

T-SQL:  Number of characters

Posted on 2014-04-29
4
256 Views
Last Modified: 2014-05-01
Hello:

Below is a snippet of my T-SQL programming where I'm trying to very specifically tell SQL how many characters are to be accommodated by each field.  For example, the "rectype" field is to be 15 characters long.

But, this programming does not seem to be set in logic.  For example, the field "wotclocname" is to start at position 137.  Yet, it starts at position 138.  Somehow, I'm off one character.

What I'm trying to ask is this:  "Is there a better way of telling SQL how many characters a field should be?".  

Sure, I could reduce the number of one of the other fields by 1.  But, I want to know how this logic is flawed.

Thanks!

TBSupport


CAST('140ERWOTCLOC' as CHAR(15)) as rectype,
CAST('16860' as CHAR(16)) as cocode,
CAST('' as CHAR(75)) as filler,
CAST('71912' as CHAR(5)) as tci_id,
CAST('100' as CHAR(25)) as wotclocnumber,
CAST(' Anodyne Medical Services Corp' as CHAR(50)) as wotclocname
0
Comment
Question by:TBSupport
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 22

Accepted Solution

by:
plusone3055 earned 500 total points
ID: 40030078
its not flawed
Character representation starts @ 0 not 1
hence why your one character off
that's just the way programming works
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40030079
You're going to have to show us what the source of data looks like in order to answer this question.

>For example, the "rectype" field is to be 15 characters long.
Do you mean defining the column as 15 characters, or interpreting a source of data as a specific 15 characters?
0
 
LVL 1

Author Comment

by:TBSupport
ID: 40030118
Thanks, plusone3055!

@Jim Horn:  I mean defining the column as 15 characters.

TBSupport
0
 
LVL 41

Expert Comment

by:Sharath
ID: 40031273
Why don't you cast to VARCHAR with sufficient length?
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Recursion 6 44
SQL Database Restore 2008 R2 1 40
denied execute as 13 55
T-SQL: How to extract records into a new table 7 43
     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 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