0

I need to merge two aggregate functions in SQL Server.

Code:

SELECT 
    REPLACE(CONVERT(VARCHAR(8), SYSDATETIME(), 3), '/', '') AS [DDMMYY],
    (count(Sno)+1) AS count 
FROM tbl_demographic_reg

Output:

DDMMYY   count

060114  1

I need the above output to be 06011400001 - how to get it? Thanks

1

2 Answers 2

2
SELECT 
    REPLACE(CONVERT(VARCHAR(8), SYSDATETIME(), 3), '/', '') +
    RIGHT('0000'+ CONVERT(VARCHAR,count(Sno)),6)
FROM tbl_demographic_reg
Sign up to request clarification or add additional context in comments.

Comments

1

You can just concatenate them together, adding necessary zeroes in between:

SELECT 
    REPLACE(CONVERT(VARCHAR(8), SYSDATETIME(), 3), '/', '') + 
    right('0000' + cast((count(Sno)+1) as varchar(5)), 5)
FROM tbl_demographic_reg

4 Comments

its showing error "Incorrect syntax near 'cast', expected 'AS'." what to do...?
Sorry, misplaced brackets. Please try now.
thanks...if sno return 10 then o/p is 000010 but i need it 00010..dynamiclly it should reduce zero's how to do...?
Is it? When you do SELECT right('0000' + cast(10 as varchar(5)), 5) it returns 00010 correctly...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.