Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Column value based on a condition

Author  Topic 

kishoremcp
Starting Member

41 Posts

Posted - 2013-11-20 : 07:22:35
Hi,

I have a column with serial numbers 1, 2,3,4,....etc
I want to have P1 for first 4 serial numbers to be displayed in a separate column beside Serial number column and for the next four serial numbers (5,6,7,8) it should be P2 and like that I want till P13 and for next again it should start with P1(I mean after P13 it should give again P1 instead of P14) and continue the same process.

Column name of Serial number is [S.No]
table name for example is #temp1

Please help..

Thanks in advance...


Regards
Kishore

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-11-20 : 07:42:58
SELECT 'P'+CAST(ISNULL(NULLIF(((([S.No]
-1)/4)+1) %13, 0), 13) AS VARCHAR(2)), [S.No]
from #temp1


--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-20 : 08:05:46
here you go

SELECT [S.No] ,'P' + CAST((((( [S.No] -1)/4+1)-1)%13)+1 AS varchar(2))
FROM #Temp


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

kishoremcp
Starting Member

41 Posts

Posted - 2013-11-20 : 08:11:36
Thank you. It worked.

Regards
Kishore
Go to Top of Page
   

- Advertisement -