Loading...
アイコン

ETL SQL

チャンネル登録者数 1.31万人

1万 回視聴 ・ 39いいね ・ 2022/01/04

Free SQL Pattern Training: etlsql.kartra.com/page/sps-free-training

In this tutorial, we will see 3 methods to add leading zeros to number in sql.

Video timeline

00:00 Introduction
00:25 Problem Statement
01:20 First Method: to_char function
02:50 Second Method: case statement
04:05 Third Method: right function

SQL Queries used in the video:

/* using to_char function */
select catid,
to_char(catid,'0000') as catid_1
from category
order by 1 desc;

/* using case statement */
select catid,
case when length(catid)=1 then '000'||catid
when length(catid)=2 then '00'||catid
when length(catid)=3 then '0'||catid
when length(catid)=4 then ''||catid
end as catid_1
from category
order by 1 desc;

/* using right function */
select catid,
right('0000'||catid,4) as catid_1
from category
order by 1 desc;

Practice SQL questions on Data Lemur platform.
I will highly recommend to sign up for this platform.
I am sharing my referral link below for easy reference.
bit.ly/3SuF3wf

コメント

コメントを取得中...

コントロール
設定

使用したサーバー: direct