Showing posts with label field. Show all posts
Showing posts with label field. Show all posts

Tuesday, March 27, 2012

Difference Between NULL and Blank in SQL

Hi,

My question is, is there any difference between a NULL and a Blank
(Unknown, Not Applicable) field in MS SQL or are they the same?

Awaiting your comments,
RegardsAm 1 Mar 2006 01:57:11 -0800 schrieb Shwetabh:

> Hi,
> My question is, is there any difference between a NULL and a Blank
> (Unknown, Not Applicable) field in MS SQL or are they the same?
> Awaiting your comments,
> Regards

Yes, a very big difference! Be carefully if you have NULL valued fields. If
you do a compare and one or both are NULL, then the result is always NULL,
never true or false. Even comparing two fields which are both NULL will
give NULL as result, not true! Or if you have something like "select
sum(field) from ..." and one or more are NULL, then the result will be
NULL. Use always "if field is NULL ..." for NULL checking and for safety
maybe something like "select sum( IsNull(field,0) ) from ...". Check the
function ISNULL() in the manual.

bye,
Helmut|||I thought aggreators like SUM ignored nulls...|||Please see some corrections inline...

helmut woess wrote:
> Am 1 Mar 2006 01:57:11 -0800 schrieb Shwetabh:
> > Hi,
> > My question is, is there any difference between a NULL and a Blank
> > (Unknown, Not Applicable) field in MS SQL or are they the same?
> > Awaiting your comments,
> > Regards
> Yes, a very big difference! Be carefully if you have NULL valued fields. If
> you do a compare and one or both are NULL, then the result is always NULL,
> never true or false.

No, the comparison "<somevalue> = NULL" will result in UNKNOWN. If the
predicate is part of the WHERE clause, then the row is removed from the
result. If the predicate is part of a CHECK constraint, then the row is
allowed.

> Even comparing two fields which are both NULL will
> give NULL as result, not true!

The result of the comparison "NULL = NULL" also results in UNKNOWN.

> Or if you have something like "select
> sum(field) from ..." and one or more are NULL, then the result will be
> NULL.

NULL values are excluded from aggregates. If one or more NULL values are
encountered, SQL Server will issue a warning stating that these rows are
disregarded. The only exception is the aggregate COUNT(*)

> Use always "if field is NULL ..." for NULL checking and for safety
> maybe something like "select sum( IsNull(field,0) ) from ...".

This only good advice if you want a NULL row to be treated as 0 in an
aggregation (for example the calculation of an average).

> Check the function ISNULL() in the manual.
> bye,
> Helmut

In addition to Helmut's warnings, note that NULLs are promoted in
expressions. So if you write SELECT A + B AS sum_of_A_and_B and either A
or B is NULL, then sum_of_A_and_B will be NULL.

HTH,
Gert-Jan|||Shwetabh (shwetabhgoel@.gmail.com) writes:
> My question is, is there any difference between a NULL and a Blank
> (Unknown, Not Applicable) field in MS SQL or are they the same?

As said in other posts, they are not. I just like to add one thing:

NULL stands for "unknown, not appliable". An empty string, is very much
a defined value as far as SQL is concerned.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

Monday, March 19, 2012

Did I just trip over a golden egg?

I'v been scouring the MSDN library and asking in newsgroups, forums, etc. on a way to extract [only] the time from a DateTime field (for a stored procedure) and no one could answer my question. then I tried this in QA:

selectright(getdate(),7)

and I found an answer. I'm now looking for someone to prove this is either a golden egg, a goose egg, or a documented feature I missed and if so where, so I can learn from it.

Jon

This will work,if you know what the default locale of the Sql Server is set as. However, if you don't, then you could get a nasty shock. For instance, if the server happened to be using ISO8601 dateformats then you would get something meaningless back.

Try

SELECT Right(CONVERT(varchar,GetDate(),126),7)

to see what I mean. The safest way to do what you are doing would be:

SELECT LTRIM(Right(CONVERT(varchar,GetDate(),0),7))

That should guarantee you are getting the correct dateformat string to work with. There's nothing mysterious about the RIGHT function, either - most SQL languages have a substring function.|||

Thank you and I accept the safest way as you suggested. The RIGHT function wasn't mysterious to me but rather the tool I used to get the answer I wanted. I was looking for an easy means to retrieve the time from SQL and was frustrated nobody out there could answer a question I thought would be a standard novice question. I'm realizing the simplest things are the cause of the greatest frustration in the programming world. Thanks for your solution.

Jon

|||Actually, I would suggest this:
SELECT CONVERT(char(12),GETDATE(),114)

For this datetime value:
2005-12-17 20:55:27.060

This would be returned:
20:55:27:060

SeeCAST and CONVERT for more information on how to use these 2 functions.|||

Thank You Terri! I was not familiar with CONVERT and now it seems ALL my date problems can be solved by this new information.

Jon

|||

Hmmm... that's interesting. To tell you the truth, I would have just used a whole bunch of datepart functions myself :)

Diakrieten worden niet opgeslagen

Hi,
When I use the update statement "UPDATE tablename SET field =3D
'As=FBca' WHERE fieldid =3D 1" will generate in the field "Asuca". How
is that possible?
When I cut and paste the field within Enterprise Manager the field
looks like "As=FBca".
So... whats the problem?
Thanks,
Ron
Diver
Try
UPDATE tablename SET field =N'Asca' WHERE fieldid = 1
"Diver" <rnooit@.hotmail.com> wrote in message
news:1121756360.293341.217730@.g47g2000cwa.googlegr oups.com...
Hi,
When I use the update statement "UPDATE tablename SET field =
'Asca' WHERE fieldid = 1" will generate in the field "Asuca". How
is that possible?
When I cut and paste the field within Enterprise Manager the field
looks like "Asca".
So... whats the problem?
Thanks,
Ron
|||Uri,
Thanks. But is there any sqlserver setting that i can set for the whole
database?
Ron

Diakrieten worden niet opgeslagen

Hi,
When I use the update statement "UPDATE tablename SET field =3D
'As=FBca' WHERE fieldid =3D 1" will generate in the field "Asuca". How
is that possible?
When I cut and paste the field within Enterprise Manager the field
looks like "As=FBca".
So... whats the problem?
Thanks,
RonDiver
Try
UPDATE tablename SET field =N'Asca' WHERE fieldid = 1
"Diver" <rnooit@.hotmail.com> wrote in message
news:1121756360.293341.217730@.g47g2000cwa.googlegroups.com...
Hi,
When I use the update statement "UPDATE tablename SET field =
'Asca' WHERE fieldid = 1" will generate in the field "Asuca". How
is that possible?
When I cut and paste the field within Enterprise Manager the field
looks like "Asca".
So... whats the problem?
Thanks,
Ron|||Uri,
Thanks. But is there any sqlserver setting that i can set for the whole
database?
Ron

Diakrieten worden niet opgeslagen

Hi,
When I use the update statement "UPDATE tablename SET field =3D
'As=FBca' WHERE fieldid =3D 1" will generate in the field "Asuca". How
is that possible?
When I cut and paste the field within Enterprise Manager the field
looks like "As=FBca".
So... whats the problem?
Thanks,
RonDiver
Try
UPDATE tablename SET field =N'Asûca' WHERE fieldid = 1
"Diver" <rnooit@.hotmail.com> wrote in message
news:1121756360.293341.217730@.g47g2000cwa.googlegroups.com...
Hi,
When I use the update statement "UPDATE tablename SET field ='Asûca' WHERE fieldid = 1" will generate in the field "Asuca". How
is that possible?
When I cut and paste the field within Enterprise Manager the field
looks like "Asûca".
So... whats the problem?
Thanks,
Ron|||Uri,
Thanks. But is there any sqlserver setting that i can set for the whole
database?
Ron

Friday, February 17, 2012

Develope Pending > Activate

Hi,

I have an application (Asp.net + c#).There is a SQL table called 'Status' it is used to store pending and activated events.

There is a field called 'Complete'.

So when user register I want to add recored to 'Complete' field as 'Pending'

But I cant understand how to do this.

Plese help me

It would have been helpful if you describrd your Registration form page...

One way could be, considering you have a Submit button on your registration form, is to update your Status table while the user presses the submit button during registration.

Hope that helps.