Home > Net >  Cast count(*) to two-digits string in sql server
Cast count(*) to two-digits string in sql server

Time:12-26

I want to count a number of record 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 is '02'.

Please give me a solution, thank alls.

CodePudding user response:

with the Format() function in ms-SQL you can format a number into a digit : 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