How to Prefix 0's in Athena Query?

0

I would like to prefix 0's for employee id column and not sure how can I do this on Athena? It appears LPAD function work differently in Athena vs Redshift.

select LPAD(CAST(employee_id As varchar),8,0) as EmployeeID

질문됨 일 년 전1402회 조회
3개 답변
1

I have fixed this by adding an If condition.

This is what worked for me: select IF(LENGTH(employee_id)<8,LPAD(employee_id,8,'0'),employee_id) from table1

답변함 일 년 전
AWS
전문가
검토됨 일 년 전
0

The third argument is expected to be a string for LPAD function - can you pelase try it like this?

select LPAD(CAST(employee_id As varchar),8,'0') as EmployeeID
AWS
Alex_T
답변함 일 년 전
AWS
전문가
검토됨 일 년 전
0

Thank you for your response. I tried this last night and found a problem with Lpad function. Its prefixing the 0's however, its also truncating the string if the string size is more than the size of the padding we give. Just wondering if there is any way we can avoid truncating.

select lpad(employee_id,8,'0') from table1 --> This is prefixing 0's to employee id less than 8 however its also truncating employee ids more than 8 characters.

Example: 123456 --> 00123456 123456789012 --> 12345678

답변함 일 년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인