Search code examples
sqlsql-serversql-server-2000

Append corresponding brackets automatically using sql server


I am trying to build an append function. Base query is

SELECT @concatentated_value = 
     ISNULL(@concatentated_value + ',', '') + 
     ISNULL(@append_stringfront, '') + 
     CONVERT(VARCHAR, DATEADD(DAY, number, @start_date), 101) + 
     ISNULL(@append_stringend, '')

Now in this function if I pass like front append '[' then it should generate ']' as end automatically so that I don't have to pass both the value params explicitly, I know casing can do this stuff but is there any embedded way of doing this instead of different ways of defining those value explicitly and getting the corresponding end values.

Any links would be good

I am currently using this

SELECT @concatentated_value = ISNULL(@concatentated_value + ',', '') + ISNULL(@append_string, '') + CONVERT(VARCHAR, DATEADD(DAY, number, @start_date), 101) + CASE @append_string WHEN '[' THEN ']' WHEN '(' THEN ')' WHEN '{' THEN '}' ELSE '' END

Solution

  • This will handle [],{},<>. But not this ()

    Declare  @concatentated_value varchar(max)='aSD',
    @append_stringfront varchar (12)='<',
    @start_date date='01/06/2013'
    
    SELECT 
         ISNULL(@concatentated_value + ',', '') + 
         ISNULL(@append_stringfront, '') + 
         CONVERT(VARCHAR, DATEADD(DAY, 1, @start_date), 101) + 
         ISNULL(char(ASCII(@append_stringfront)+2), '')