Home > Software engineering >  Cast count(*) to two-digits string
Cast count(*) to two-digits string

Time:12-26

I want to count the number of records from a table and convert it to 2 digits string in SQL Server. For example:

|id|something|
|1 |      abc|
|2 |      def|

I want to have the result as '02'.

Please help me with a solution, Thanks.

CodePudding user response:

With the Format() function in SQL Server you can format a number into a digit: e.g. when you use FORMAT(<Column_Name>, 'D2') you can have numbers in two-digit form.

SELECT FORMAT(Id, 'D2'),*
FROM Test_Table

CodePudding user response:

you can use lpad() function of SQL. check below example

SELECT 
  LPAD(COUNT(id), 2, 0) AS count
FROM
  Database.table
  • Related