0

I'd like to add trailing zeros to a data set however there is a WHERE clause involved. In a DOB field I have a date of 1971 and I'd like to add 0000 to make the length equal 8 characters. Sometimes there is 197108 which then I'd need to only add two 00. The fields that are null are ok. Any ideas?? Thanks in advance...

4
  • What's a training zero? Commented Mar 11, 2014 at 19:18
  • u mean trailing zeroes? Commented Mar 11, 2014 at 19:19
  • I'm sorry.. trailing zeros :-) Commented Mar 11, 2014 at 19:20
  • No examples seem to be working... Commented Mar 11, 2014 at 20:17

4 Answers 4

1

You can add trailing zeros by doing:

select left(col+space(8), 8)

However, you probably shouldn't be storing date in a character field.

Sign up to request clarification or add additional context in comments.

Comments

0
Update table
    set Dob = CONCAT(TRIM(Dob), '0')
  where LEN(TRIM(Dob)) < 8

Comments

0
select cast(dob as nvarchar) + replicate('0',(8)-len(cast(dob as nvarchar)))
from table_name

SQL Fiddle

2 Comments

this worked but just selected the data didn't update the data
update table_name set dob = cast(dob as nvarchar) + replicate('0',(8)-len(cast(dob as nvarchar)))
0

Try REPLICATE function.

REPLICATE('0',8-LEN(CAST(DOB AS NVARCHAR))) + CAST(DOB AS NVARCHAR)

Edit: Try this

select REPLICATE('0',8-LEN('1985')) + '1985'

1 Comment

TRIM not recognized as a built in fi=unction in MS SQL 2008??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.