Showing posts with label space. Show all posts
Showing posts with label space. Show all posts

Monday, March 12, 2012

Number format to return 0 when empty?

My client has several issues with the number formating in the reports.
They want to have a space between 1 000 and no decimals. First I created a
custom format, but today I noticed I could get the same result with N0.
Anyway, N0 format will not show empty numbers, it just leaves the cell
blank. But my client now wants to have a 0 instead of blank. Is it possible?
Kaisa M. LindahlTry using =IIF(<Your number field> Is Nothing, 0, <Your number field>) as
the value expression.
--
This posting is provided "AS IS" with no warranties, and confers no rights.
"Kaisa M. Lindahl" <kaisaml@.hotmail.com> wrote in message
news:e$Sm76V%23EHA.2076@.TK2MSFTNGP15.phx.gbl...
> My client has several issues with the number formating in the reports.
> They want to have a space between 1 000 and no decimals. First I created a
> custom format, but today I noticed I could get the same result with N0.
> Anyway, N0 format will not show empty numbers, it just leaves the cell
> blank. But my client now wants to have a 0 instead of blank. Is it
> possible?
> Kaisa M. Lindahl
>|||Yes, I've figured that one out.
Problem is, I'd have to do it on aprox. 20 cells in each report (> 10 so
far), all with different field names... If there are any other solutions,
I'd be very happy to hear it.
Kaisa M. Lindahl
"Fang Wang (MSFT)" <fangw@.microsoft.com> wrote in message
news:u907lUd#EHA.936@.TK2MSFTNGP12.phx.gbl...
> Try using =IIF(<Your number field> Is Nothing, 0, <Your number field>) as
> the value expression.
> --
> This posting is provided "AS IS" with no warranties, and confers no
rights.
> "Kaisa M. Lindahl" <kaisaml@.hotmail.com> wrote in message
> news:e$Sm76V%23EHA.2076@.TK2MSFTNGP15.phx.gbl...
> > My client has several issues with the number formating in the reports.
> > They want to have a space between 1 000 and no decimals. First I created
a
> > custom format, but today I noticed I could get the same result with N0.
> > Anyway, N0 format will not show empty numbers, it just leaves the cell
> > blank. But my client now wants to have a 0 instead of blank. Is it
> > possible?
> >
> > Kaisa M. Lindahl
> >
> >
>|||I'm sorry, but there is no "easier" solution than using IIF for this
scenario.
--
This posting is provided "AS IS" with no warranties, and confers no rights.
"Kaisa M. Lindahl" <kaisaml@.hotmail.com> wrote in message
news:ORxJ1Jh%23EHA.3988@.TK2MSFTNGP11.phx.gbl...
> Yes, I've figured that one out.
> Problem is, I'd have to do it on aprox. 20 cells in each report (> 10 so
> far), all with different field names... If there are any other solutions,
> I'd be very happy to hear it.
> Kaisa M. Lindahl
> "Fang Wang (MSFT)" <fangw@.microsoft.com> wrote in message
> news:u907lUd#EHA.936@.TK2MSFTNGP12.phx.gbl...
> > Try using =IIF(<Your number field> Is Nothing, 0, <Your number field>)
as
> > the value expression.
> >
> > --
> > This posting is provided "AS IS" with no warranties, and confers no
> rights.
> >
> > "Kaisa M. Lindahl" <kaisaml@.hotmail.com> wrote in message
> > news:e$Sm76V%23EHA.2076@.TK2MSFTNGP15.phx.gbl...
> > > My client has several issues with the number formating in the reports.
> > > They want to have a space between 1 000 and no decimals. First I
created
> a
> > > custom format, but today I noticed I could get the same result with
N0.
> > > Anyway, N0 format will not show empty numbers, it just leaves the cell
> > > blank. But my client now wants to have a 0 instead of blank. Is it
> > > possible?
> > >
> > > Kaisa M. Lindahl
> > >
> > >
> >
> >
>

Friday, March 9, 2012

NULLs

Do NULLs take up space on the Database? I mean is room allocated for them?so using NULLs also saves space - right?|||I thought it depended on the datatype. Like Char(500) still allocates 500 spots. Null just means those spots aren't taken by anything yet. I think the case is the same for Binary types also.

I could be wrong on this one though. Just wanted a little clarification on the matter other than "no".|||no, there is no allocation for nulls.|||So what you are saying is that varchar and char are basically the same? I mean if I declare char(50) only use the first ten and fill the rest with NULLs - it is the same as varchar(50).|||no, nulls are the same

char(50) is the actual string + spaces (the whole length is 50)
it has nothing to do with what we are talking about here|||If I wrote "abc" in field of type Varchar(50) actually I will store only 3 spaces ,, but if I use char(50) field I will reserve all of the spaces , otherwise I think SQL Server do its best to optimize storage.|||If a variable length field contains a NULL value, it will take up no room in the data row. SQL Server has a data structure known as a 'NULL bitmap' on each record, the NULL bitmap will be long enough to have 1 bit for every column in a row ( 5 columns could be covered by 1 byte - 8 bits). If the bit is set for a column, SQL knows the column value is NULL.

Fixed length columns always use the full number of bytes, even if the record contains NULL for the column. (This wasn't so in SQL 6.5, though).|||I'm with bitmask on this one. mcdba or not, fix length columns always use the full number of bytes and allocate a specific amount of memory regardless of the value being null or not.

Wednesday, March 7, 2012

Null valus in a select

How do I check of null in a select clause and replace them with space/empty
string?SELECT COALESCE(col_x, '') AS col_x
FROM your_table ;
--
David Portas
SQL Server MVP
--|||David Portas wrote:
> SELECT COALESCE(col_x, '') AS col_x
> FROM your_table ;
or
SELECT ISNULL(col_x, '') AS col_x
FROM your_table
robert|||That kind of works, except if the column is a smallint I get 0 instead of ''
"David Portas" wrote:
> SELECT COALESCE(col_x, '') AS col_x
> FROM your_table ;
> --
> David Portas
> SQL Server MVP
> --
>|||That kind of works, except if the column is a smallint I get 0 instead of ''
"Robert Klemme" wrote:
> David Portas wrote:
> > SELECT COALESCE(col_x, '') AS col_x
> > FROM your_table ;
> or
> SELECT ISNULL(col_x, '') AS col_x
> FROM your_table
> robert
>|||Arne,
An expression in the Selection List can only return the predefined data
type. If you want to return a smallint, then by definition, you cannot
return an empty string. If returning a string works for you, then you
can use this:
SELECT COALESCE(CAST(col_x AS VARCHAR(11)), '') AS col_x
FROM your_table
Gert-Jan
Arne wrote:
> That kind of works, except if the column is a smallint I get 0 instead of ''
> "Robert Klemme" wrote:
> > David Portas wrote:
> > > SELECT COALESCE(col_x, '') AS col_x
> > > FROM your_table ;
> >
> > or
> >
> > SELECT ISNULL(col_x, '') AS col_x
> > FROM your_table
> >
> > robert
> >
> >|||Try this.
SELECT ISNULL(CONVERT(VARCHAR(5),col_x), '') AS col_x FROM your_table
"Arne" wrote:
> That kind of works, except if the column is a smallint I get 0 instead of ''
>
> "Robert Klemme" wrote:
> > David Portas wrote:
> > > SELECT COALESCE(col_x, '') AS col_x
> > > FROM your_table ;
> >
> > or
> >
> > SELECT ISNULL(col_x, '') AS col_x
> > FROM your_table
> >
> > robert
> >
> >

NULL Values vs empty string vs space

How do I define a field to have the default value = ''. Not NULL but not a space either in SQL Server 2005?

Use the following in the field (default value) property

('')

Regards

|||I tried that and it didn't work.|||Just to get your point, did you mean that you want to place a default empty string?|||Smiling is correct, that is how you do it. When you say "it didn't work", perhaps you mean it didn't do what you wanted/expected it to, but that is how you define a default of a zero-length string (from within management studio).|||

That's what I thought too. I tried it again and it does work. I think what was going on is I had the table definition open and saved it but it really did not save. So I closed the defintion window and tried it and it works.

Thanks

|||Whats even more interesting it does NOT work when using the FormView control in ASP.NET 2.0 but if I enter the data directly into the table using the server explorer it does work.

???|||Does it show NULL value though?|||

YES, when viewing the data in the database server explorer the value is NULL even though the default value is ''.

Like I said entering data directly in the server explorer it works, when using a FormView control it does not. When using the formview all code is generated by VisualStudio.

Here is the SQLDatasource code generated by Visual Studio...

<asp:SqlDataSource ID="SqlDataSourceUserProfile" runat="server" ConnectionString="<%$ ConnectionStrings:AEISITConnectionString %>"
DeleteCommand="DELETE FROM [UserProfile] WHERE [UserProfile_RecID] = @.UserProfile_RecID"
InsertCommand="INSERT INTO [UserProfile] ([UserID], [UserRole], [FirstName], [LastName],Email, [CampusID], [DistrictID], [RegionID], [SessionID]) VALUES (@.UserID, @.UserRole, @.FirstName, @.LastName, @.Email, @.CampusID, @.DistrictID, @.RegionID, @.SessionID)"
SelectCommand="SELECT UserProfile_RecID, UserID, UserRole, FirstName, LastName, Email, CampusID, DistrictID, RegionID, SessionID FROM UserProfile WHERE (UserProfile_RecID = @.UserProfile_RecID)"
UpdateCommand="UPDATE [UserProfile] SET [UserID] = @.UserID, [UserRole] = @.UserRole, [FirstName] = @.FirstName, [LastName] = @.LastName,Email = @.Email, [CampusID] = @.CampusID, [DistrictID] = @.DistrictID, [RegionID] = @.RegionID, [SessionID] = @.SessionID WHERE [UserProfile_RecID] = @.UserProfile_RecID">
<DeleteParameters>
<asp:Parameter Name="UserProfile_RecID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="UserID" Type="String" />
<asp:Parameter Name="UserRole" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="CampusID" Type="String" />
<asp:Parameter Name="DistrictID" Type="String" />
<asp:Parameter Name="RegionID" Type="String" />
<asp:Parameter Name="SessionID" Type="String" />
<asp:Parameter Name="UserProfile_RecID" Type="Int32" />
</UpdateParameters>
<SelectParameters>
<asp:ControlParameter ControlID="UserListBox" Name="UserProfile_RecID" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="UserID" Type="String" />
<asp:Parameter Name="UserRole" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="CampusID" Type="String" />
<asp:Parameter Name="DistrictID" Type="String" />
<asp:Parameter Name="RegionID" Type="String" />
<asp:Parameter Name="SessionID" Type="String" />
</InsertParameters>
</asp:SqlDataSource>

|||

That would explain it.

If you want the default value on insert, remove all references to the field in the insertcommand. Alternatively, change the parameter's convertemptystringtonull property to false.

|||

Yes, whatMotley said is correct.

Just change the convertemptystringtonull property to false.

Monday, February 20, 2012

Null or empty string use more space?

Hi all,
I have some columns in my database which allows null. I want to know if leaving the field to be NULL or storing an empty string into the field, which will take up more space?? if the field type is varchar(100)
No doubt, fields with empty strings take up way more space than their counterparts with NULL values......... have a look at this link for an example >>http://www.sqljunkies.com/WebLog/amachanic/archive/2004/10/22/4743.aspx|||

Hi Narayanas,

Thanks for your reply and solution. Now that i know storing an empty string will take up more spaces then leaving it Null, but if i have a table which has 4-5 fields that allows Null. Then how can i leave those columns to Null if the user don't enter anything for those string fields? e.g

CREATE PROCEDURE add_row
@.field1 varchar(100),
:
@.field5 varchar(100)

AS
IF @.filed1 = ''
INSERT INTO some_table (field2, ..., field5) VALUES (@.field2, ..., @.field5)
IF @.field2 = ''
INSERT INTO some_table(field1, field3, ..., field5) VALUES (@.field1, @.field3,... , @.field5)

If i do it this way then there will be too many combinations of IF statements. Can you show me some better ways to do this?

|||Are you seriously worried, in this modern age, about the extra space taken up with an empty string vs a null? I'd be more concerned about the problems and general confusion nulls create, let alone some potential performance problems with the database has to look up the null flag. If you want to use NULLs then all well and good, but treat them as a feature not a space saving device.
|||

kakusei wrote:

Hi all,
I have some columns in my database which allows null. I want to know if leaving the field to be NULL or storing an empty string into the field, which will take up more space?? if the field type is varchar(100)


You cannot pass an empty string to a Database NULL because ANSI SQL NULL which is database NULL is in an UNKNOWN value not an empty string. It evaluates mathematically to a NULL but it is an UNKNOWN value determined by the creator of the database. NET value types don't accept NULL the reason for DBNULL but SQL Server value types have to accept NULL to be ANSI SQL compliant. If it is Varchar (100) you use DBNULL. Space is not important but passing a Varchar (100) column an empty string will create data consistency problems in your database. Hope this helps.|||you can set default values at the time of defining the parameters in the stored proc.

CREATE PROCEDURE add_row
@.field1 varchar(100) = NULL,
:
@.field5 varchar(100) = NULL

AS
-- so you would need one insert statement.If there is a value in the variable
-- that will be inserted else null.
INSERT INTO some_table (field2, ..., field5) VALUES (@.field2, ..., @.field5)