Results 1 to 6 of 6

Thread: replace function and varchar

  1. #1
    Join Date
    Oct 2005
    Location
    Ireland
    Posts
    92

    replace function and varchar

    if (as per books online) the replace function returns a varchar, why do these commands give different results?

    print Replace(Convert(varchar(30),cast('2010-01-01 00:00:00' as datetime) , 120), ' 00:00:00','') + '.'
    print Replace(Convert(varchar(30),cast('2010-01-01 00:00:00' as datetime) , 120), '00:00:00','') + '.'

  2. #2
    Join Date
    Sep 2002
    Posts
    5,938
    Sql doesn't remove space in middle of the string.

  3. #3
    Join Date
    Oct 2005
    Location
    Ireland
    Posts
    92
    It's not in the middle. forget about the dot at the end. the replace function returns a space as the last character in the second example.

  4. #4
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    REPLACE is not trimming the last blank space, it could be a bug.

  5. #5
    Join Date
    Sep 2002
    Posts
    5,938
    It works ok if you take out the dot at the end, try following:

    select len(Replace(Convert(varchar(30),cast('2010-01-01 00:00:00' as datetime) , 120), ' 00:00:00','')+ '.')
    select len(Replace(Convert(varchar(30),cast('2010-01-01 00:00:00' as datetime) , 120), '00:00:00','')+ '.')
    select len(Replace(Convert(varchar(30),cast('2010-01-01 00:00:00' as datetime) , 120), ' 00:00:00',''))
    select len(Replace(Convert(varchar(30),cast('2010-01-01 00:00:00' as datetime) , 120), '00:00:00',''))

  6. #6
    Join Date
    Jan 2010
    Posts
    1
    The replaces that you are performing are different, the first the space and the time ' 00:00:00',
    the second just the time '00:00:00'.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •