Results 1 to 5 of 5

Thread: SQL Padding

  1. #1
    Join Date
    May 2003
    Posts
    30

    SQL Padding

    Can anyone help me. Is it possible to left pad an int column in a table at it's source. I've read the scripts to update it but would prefer it if was automatic at time of entry. IS this possible, any ideas?

    Many thanks

  2. #2
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    Not in integer column.

    If it is char/varchar/nchar/nvarchar column then Yes using trigger or when inserting using a udf function

  3. #3
    Join Date
    May 2003
    Posts
    30
    If I alter the column type to nvarchar for example what is my best way of padding it?

  4. #4
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    Why cant you use a view or a computed column

    create table mytable (id int, name varchar(100))
    insert into mytable select 1,'a'
    insert into mytable select 2,'b'
    insert into mytable select 3,'c'
    insert into mytable select 441,'d'
    insert into mytable select 51,'e'
    insert into mytable select 61,'f'

    --View with LPAD 5
    create view mytable_view as
    select right('00000'+convert(varchar(5),id),5) as id , name from
    mytable

    select * from mytable_view

    --Computed column with LPAD5

    alter table mytable add myid as right('00000'+convert(varchar(5),id),5)

    select * from mytable

    id,name,myid
    1,a,00001
    2,b,00002
    3,c,00003
    441,d,00441
    51,e,00051
    61,f,00061

  5. #5
    Join Date
    May 2003
    Posts
    30
    Will do that, that's great. Many thanks for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •