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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Behaviour of Cast function
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Yak Posting Veteran

95 Posts

Posted - 01/08/2013 :  06:36:54  Show Profile  Reply with Quote
Hi All,

my requirement is need to form a fixed length string by concatenating three columns.
Out of 3 fields if last_name field is null then i need to replace with 20 spaces.

I tried with below statement:

select @data_var=
isnull(lastname,replicate(' ',20)) +

the output of this above statement is:
data of column1+one space+data of column2

"it is returning one space but i am expecting 20 spaces"

If i slightly modify the above statement as below with case statement i am getting correct results:

select @data_var=
cast(isnull(lastname,replicate(' ',20)) as char(20)) +

the output of this above statement is:
data of column1+20 spaces+data of column2

If i have this piece of code(mentioned above) in a block of code,then i am getting above mentioned issue.

If i execute this as a single block even without case atatement i am getting expected results(20 spaces).

@targ char(300),
@col1 char(20),
@col2 char(3)
set @col2='hai'
set @col1=null
select @targ=@col2+isnull(@col1,replicate(' ',20))+@col2
select @targ

Both col1 and clo2 are char data type only..

Please suggest what is happening in first statement(without case) and second(with case)?



Flowing Fount of Yak Knowledge

United Kingdom
3385 Posts

Posted - 01/08/2013 :  07:05:58  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
Maybe there is a single space in lastname?

Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 01/08/2013 :  23:38:19  Show Profile  Reply with Quote
what was dataype of lastname?

SQL Server MVP

Go to Top of Page

Starting Member

17 Posts

Posted - 01/09/2013 :  04:09:28  Show Profile  Reply with Quote
This is due to length of the 'lastname' column referred in your first script, seems it's length is 1. That's why isnull replaces only one space. ISNULL would replace value based on the type\length of the expression you want to replace with.

In your second script you use Cast as char(20), here cast function extends the length(20) no matter what was declared length of column, that's why it adds 20 spaces.

If you look at your last script, 'col1' length set as 20, that's why you get 20 spaces without Cast function. Hope you get it.

I'm here to learn new things everyday..
Go to Top of Page

Yak Posting Veteran

95 Posts

Posted - 01/09/2013 :  05:44:18  Show Profile  Reply with Quote
Thanks alot shan007 for resolution.

Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.04 seconds. Powered By: Snitz Forums 2000