Obtaining collation length of declared varchar variable

  • Morning All,

    Can I have some help with this one please, I am having to make a fixed length text file based on information from the DB

    Declare @EDIString varchar(MAX)

    Declare @RecordType varchar(2)

    Declare @RegistrationMark varchar(7)

    Declare @Model_Chassis varchar(11)

    Declare @LocationCode Varchar(4)

    Declare @MovementDate varchar(8)

    Declare @IMSAccountCode varchar(5)

    Declare @MovementType varchar(8)

    Declare @NotUsed1 Varchar(28)

    Declare @NotUsed2 varchar(7)

    Select @RecordType = RecordType, @RegistrationMark = RegistrationMark, @Model_Chassis = Model_And_Chassis, @LocationCode = LocationCode, @MovementDate = MovementDate, @IMSAccountCode = IMSAccountCode, @Movementtype = MovementTypeCode from Fiat_OutBound

    Once I have selected the information from the DB I need to ensure that each field is the correct length. I therefore want to pass the variable and the length of the variable into a function to return the correct length.

    So if location Code = 'AB' this needs to be four characters long so want to pass it into a function and return 'AB '

    As I need to do this for 70+ variables is there an easy way to obtain the length of the collation for the variable?

    regards

    Tom

  • Just declare variable as CHAR, not VARCHAR.

    SQL Server will do everything for you.

    _____________
    Code for TallyGenerator

  • Doh - Thanks

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply