• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 427
  • Last Modified:

Add leading 0's

how do I add a leading 0 to all records in a column of type char?

exisiting data format  = 171347, 171331, etc
required data format = 0171347, 0171331, etc.

thanks
0
kpwhitte
Asked:
kpwhitte
1 Solution
 
SQL_SERVER_DBACommented:
select '0' + column from tablename


update tablename
set column = '0' + column
0
 
SwindleCommented:
It also might be good to mention that if the field is a numeric datatype, you'd have to convert it to a character type or always select it back out adding the zero and converting it to a character:

SELECT '0' + convert(varchar, ColumnName) from tablename
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now