Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I had get max value from table is 20
but i want show it like 0020 in 4 digit

Thanks
Posted
Comments
[no name] 18-Sep-13 10:25am    
Okay and did you have a question or maybe there is some sort of a problem? Show it where?
ABCPQR001 18-Sep-13 10:30am    
i written query is select MAX(Empid) from tbl_employee
Output is: 20
but how to show Output like 0020
gvprabu 19-Sep-13 8:31am    
Check the solution 3.... Its simple

select STUFF(MAX(wo_closed), 1, 0, REPLICATE('0', 4 - LEN(MAX(wo_closed)))) as wo_closed from inventory 



Revised to use example provided by hypermellow
 
Share this answer
 
v5
Comments
Mike Meinz 18-Sep-13 10:41am    
It worked on SQL Server 2012. I revised the example using STUFF and REPLICATE as recommended by hypermellow.
ABCPQR001 18-Sep-13 11:09am    
thank you
Have a look at the STUFF function, then you could do something like this?

SQL
SELECT STUFF(MAX(ID), 1, 0, REPLICATE('0', 4 - LEN(MAX(ID)))) FROM YourTable;
 
Share this answer
 
Comments
ABCPQR001 18-Sep-13 10:37am    
thanks
hypermellow 18-Sep-13 10:44am    
I'm glad it helped!
Mike Meinz 18-Sep-13 10:40am    
My 5.
Thanks.
hypermellow 18-Sep-13 10:45am    
Thanks Mike, I'd already +5'd your CONCAT based solution (yes, I'm using MSSQL 2012)
... I wasn't aware of the CONCAT function - so thanks for that!
Hi,

Are you Tried RIGHT Function.....? Its very simple.... check the below Query

SQL
-- Direct from Table 
DECLARE @Test TABLE (ID INT)

INSERT INTO @Test(ID)
SELECT 10 UNION ALL SELECT 16 UNION ALL
SELECT 20 UNION ALL SELECT 15 UNION ALL
SELECT 5 UNION ALL SELECT 7

SELECT MAX(ID) , RIGHT('0000'+CAST(MAX(ID)AS VARCHAR(10)),4)
FROM @Test

-- Using Variables
DECLARE @MaxValue INT
SELECT @MaxValue=20
SELECT @MaxValue, RIGHT('0000'+CAST(@MaxValue AS VARCHAR(10)),4)

Regards,
GVPrabu
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900