Showing posts with label null. Show all posts
Showing posts with label null. Show all posts

Monday, March 19, 2012

How to right justify table field

How do i right justify data in a nchar or nvarchar field? e.g.

now:

2bbb

3bbb

desired :

bbb2

bbb3

where bbb is either blank or null, not certain.

I would appreciate any help

Pauley

Hi,

see if you come around with that:

CREATE FUNCTION dbo.fn_removetrailingchars
(
@.strValue VARCHAR(200),
@.TrailingChar VARCHAR(200),
@.RemoveLeading BIT
)
RETURNS VARCHAR(200)
AS
BEGIN

DECLARE @.intCount int
SET @.intCount = 0

WHILE @.intCount <= LEN(@.strValue)
BEGIN
SET @.intCount = @.intCount +1
IF SUBSTRING(@.strValue, @.intCount, 1) NOT LIKE @.TrailingChar
BREAK
ELSE
CONTINUE
END
IF @.RemoveLeading = 1
SET @.strValue =
REVERSE(dbo.fn_removetrailingchars_drkw(REVERSE(RIGHT(@.strValue,
LEN(@.strValue) - @.intCount +1 )),@.TrailingChar,0))
ELSE
SET @.strValue = RIGHT(@.strValue, LEN(@.strValue) - @.intCount +1
)

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

|||

hi Pauley,

actually I'd not, as this only seems to me a "presentation/redering" problem that should be managed at the presentation level, thus your app...

I'm asking: are you really sure you want to "pad" data on the right? this could require handling query statements differently if your user just entries "2" instead of " 2" ...

regards

|||

Andrea, thanks for responding. Below shows the situation better. The field is a nchar field sorted in ascending sequence using Visual Studio express 's Database Manager. I think nvarchar fields does the same. As u can see the the third field (1013 ) and the 14th field (1014) should be the first 2 in the sorted records. Pure alpha fields like Names, City Names etc are ok but pure numbers come out like this. Is this because the nchar and nvarchar field are intended to be alphabetic ? So how can i get a proper sequence , Use an integer or numeric type field ?

Pauley

10128

10129

1013

10130

10131

10132

10133

10134

10135

10136

10137

10138

10139

1014

10140

|||

Jens, thanks for responding. Below shows the situation better. The field is a nchar field sorted in ascending sequence using Visual Studio express 's Database Manager. I think nvarchar fields does the same. As u can see the the third field (1013 ) and the 14th field (1014) should be the first 2 in the sorted records. Pure alpha fields like Names, City Names etc are ok but pure numbers come out like this. Is this because the nchar and nvarchar field are intended to be alphabetic ? So how can i get a proper sequence , Use an integer or numeric type field ?

Pauley

10128

10129

1013

10130

10131

10132

10133

10134

10135

10136

10137

10138

10139

1014

10140

|||

hi Pauley,

in your case it seems to me your choice for an nchar datatype is not correct as your attribute's values are all numeric in the integer domain.. the integer datatype should be the best choice...

but you could argue that, in the future, your attribute could also be set with a mix of alpha + numeric values, like a trailing letter and some digits: "A110", "C99" and the result should be ordered according to alpha + numeric rules so that A110 sorts before B1..
in this cases you can play a little with the ORDER BY clause, casting or alligning part of the attribute's value like the 3rd sample enclosed..

SET NOCOUNT ON;

USE tempdb;

GO

CREATE TABLE #test (

Id nchar(5) NOT NULL PRIMARY KEY

);

GO

INSERT INTO #test VALUES ( '10' );

INSERT INTO #test VALUES ( '1' );

INSERT INTO #test VALUES ( '100' );

INSERT INTO #test VALUES ( '11' );

INSERT INTO #test VALUES ( '110' );

INSERT INTO #test VALUES ( '2' );

INSERT INTO #test VALUES ( '9' );

INSERT INTO #test VALUES ( '22' );

INSERT INTO #test VALUES ( '99' );

GO

PRINT 'Default sort for alphabetic columns';

SELECT * FROM #test ORDER BY Id;

GO

PRINT 'simple solution casting to integer';

SELECT * FROM #test ORDER BY CONVERT(int,Id);

GO

PRINT 'rigth aligning so that " 10" sorts after " 2"';

SELECT *

FROM #test ORDER BY SPACE((DATALENGTH(Id)/2 - LEN(Id))) + Id;

GO

TRUNCATE TABLE #test;

GO

INSERT INTO #test VALUES ( 'A10' );

INSERT INTO #test VALUES ( 'B1' );

INSERT INTO #test VALUES ( 'C100' );

INSERT INTO #test VALUES ( 'A11' );

INSERT INTO #test VALUES ( 'A110' );

INSERT INTO #test VALUES ( 'A2' );

INSERT INTO #test VALUES ( 'A9' );

INSERT INTO #test VALUES ( 'B22' );

INSERT INTO #test VALUES ( 'B99' );

GO

PRINT 'trailing Alpha + numeric';

SELECT Id AS [Id sorted by current collation]

FROM #test ORDER BY LEFT(Id,1), SPACE(((DATALENGTH(Id)/2 -1) - (LEN(Id)-1))) + RIGHT(Id, LEN(Id)-1);

PRINT 'specifying a specific collation';

PRINT 'for the particular/required sort rules';

SELECT Id AS [Id sorted by Latin1_General_BIN]

FROM #test ORDER BY LEFT(Id,1) + SPACE(((DATALENGTH(Id)/2 -1) - (LEN(Id)-1))) + RIGHT(Id, LEN(Id)-1) COLLATE Latin1_General_BIN;

GO

DROP TABLE #test;

--<--

Default sort for alphabetic columns

Id

--

1

10

100

11

110

2

22

9

99

simple solution casting to integer

Id

--

1

2

9

10

11

22

99

100

110

rigth aligning so that " 10" sorts after " 2"

Id

--

1

2

9

10

11

22

99

100

110

trailing Alpha + numeric

Id sorted by current collation

A2

A9

A10

A11

A110

B1

B22

B99

C100

specifying a specific collation

for the particular/required sort rules

Id sorted by Latin1_General_BIN

-

A2

A9

A10

A11

A110

B1

B22

B99

C100

obviously, you should be aware that when "sorting" values you have to consider the collation you are dealing with, as sort rules can be quite different among different collations.. my results depends (except the last one where a specific sort rule has been set via an explicit COLLATE [collation_name] clause) on my collation settings,

SELECT SERVERPROPERTY('Collation') AS [My default collation];

--<-

My default collation

Latin1_General_CI_AS

regards

|||Ok, you don′t need numeric fields as you don′t wan to have decimal values within your table in this column, right ? So you better use integer or any smaller integer data type. If you just want to use it for adhoc reporting and you cannot change the data type due to the consuming application, you could either use a cast within your order ORDER BY CAST(Thecolumn as int) or use another column to maintain the data, as the on-the-fly approach would not be very performant on heavy queries.

HTH, jens K. Suessmeyer.

http://www.sqlserver2005.de|||

Andrea, thank u very much. That resolved my question.

Pauley

|||

Jens, thank u very much. I will use the data type properly.

pauley

How to right justify table field

How do i right justify data in a nchar or nvarchar field? e.g.

now:

2bbb

3bbb

desired :

bbb2

bbb3

where bbb is either blank or null, not certain.

I would appreciate any help

Pauley

Hi,

see if you come around with that:

CREATE FUNCTION dbo.fn_removetrailingchars
(
@.strValue VARCHAR(200),
@.TrailingChar VARCHAR(200),
@.RemoveLeading BIT
)
RETURNS VARCHAR(200)
AS
BEGIN

DECLARE @.intCount int
SET @.intCount = 0

WHILE @.intCount <= LEN(@.strValue)
BEGIN
SET @.intCount = @.intCount +1
IF SUBSTRING(@.strValue, @.intCount, 1) NOT LIKE @.TrailingChar
BREAK
ELSE
CONTINUE
END
IF @.RemoveLeading = 1
SET @.strValue =
REVERSE(dbo.fn_removetrailingchars_drkw(REVERSE(RIGHT(@.strValue,
LEN(@.strValue) - @.intCount +1 )),@.TrailingChar,0))
ELSE
SET @.strValue = RIGHT(@.strValue, LEN(@.strValue) - @.intCount +1
)

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

|||

hi Pauley,

actually I'd not, as this only seems to me a "presentation/redering" problem that should be managed at the presentation level, thus your app...

I'm asking: are you really sure you want to "pad" data on the right? this could require handling query statements differently if your user just entries "2" instead of " 2" ...

regards

|||

Andrea, thanks for responding. Below shows the situation better. The field is a nchar field sorted in ascending sequence using Visual Studio express 's Database Manager. I think nvarchar fields does the same. As u can see the the third field (1013 ) and the 14th field (1014) should be the first 2 in the sorted records. Pure alpha fields like Names, City Names etc are ok but pure numbers come out like this. Is this because the nchar and nvarchar field are intended to be alphabetic ? So how can i get a proper sequence , Use an integer or numeric type field ?

Pauley

10128

10129

1013

10130

10131

10132

10133

10134

10135

10136

10137

10138

10139

1014

10140

|||

Jens, thanks for responding. Below shows the situation better. The field is a nchar field sorted in ascending sequence using Visual Studio express 's Database Manager. I think nvarchar fields does the same. As u can see the the third field (1013 ) and the 14th field (1014) should be the first 2 in the sorted records. Pure alpha fields like Names, City Names etc are ok but pure numbers come out like this. Is this because the nchar and nvarchar field are intended to be alphabetic ? So how can i get a proper sequence , Use an integer or numeric type field ?

Pauley

10128

10129

1013

10130

10131

10132

10133

10134

10135

10136

10137

10138

10139

1014

10140

|||

hi Pauley,

in your case it seems to me your choice for an nchar datatype is not correct as your attribute's values are all numeric in the integer domain.. the integer datatype should be the best choice...

but you could argue that, in the future, your attribute could also be set with a mix of alpha + numeric values, like a trailing letter and some digits: "A110", "C99" and the result should be ordered according to alpha + numeric rules so that A110 sorts before B1..
in this cases you can play a little with the ORDER BY clause, casting or alligning part of the attribute's value like the 3rd sample enclosed..

SET NOCOUNT ON;

USE tempdb;

GO

CREATE TABLE #test (

Id nchar(5) NOT NULL PRIMARY KEY

);

GO

INSERT INTO #test VALUES ( '10' );

INSERT INTO #test VALUES ( '1' );

INSERT INTO #test VALUES ( '100' );

INSERT INTO #test VALUES ( '11' );

INSERT INTO #test VALUES ( '110' );

INSERT INTO #test VALUES ( '2' );

INSERT INTO #test VALUES ( '9' );

INSERT INTO #test VALUES ( '22' );

INSERT INTO #test VALUES ( '99' );

GO

PRINT 'Default sort for alphabetic columns';

SELECT * FROM #test ORDER BY Id;

GO

PRINT 'simple solution casting to integer';

SELECT * FROM #test ORDER BY CONVERT(int,Id);

GO

PRINT 'rigth aligning so that " 10" sorts after " 2"';

SELECT *

FROM #test ORDER BY SPACE((DATALENGTH(Id)/2 - LEN(Id))) + Id;

GO

TRUNCATE TABLE #test;

GO

INSERT INTO #test VALUES ( 'A10' );

INSERT INTO #test VALUES ( 'B1' );

INSERT INTO #test VALUES ( 'C100' );

INSERT INTO #test VALUES ( 'A11' );

INSERT INTO #test VALUES ( 'A110' );

INSERT INTO #test VALUES ( 'A2' );

INSERT INTO #test VALUES ( 'A9' );

INSERT INTO #test VALUES ( 'B22' );

INSERT INTO #test VALUES ( 'B99' );

GO

PRINT 'trailing Alpha + numeric';

SELECT Id AS [Id sorted by current collation]

FROM #test ORDER BY LEFT(Id,1), SPACE(((DATALENGTH(Id)/2 -1) - (LEN(Id)-1))) + RIGHT(Id, LEN(Id)-1);

PRINT 'specifying a specific collation';

PRINT 'for the particular/required sort rules';

SELECT Id AS [Id sorted by Latin1_General_BIN]

FROM #test ORDER BY LEFT(Id,1) + SPACE(((DATALENGTH(Id)/2 -1) - (LEN(Id)-1))) + RIGHT(Id, LEN(Id)-1) COLLATE Latin1_General_BIN;

GO

DROP TABLE #test;

--<--

Default sort for alphabetic columns

Id

--

1

10

100

11

110

2

22

9

99

simple solution casting to integer

Id

--

1

2

9

10

11

22

99

100

110

rigth aligning so that " 10" sorts after " 2"

Id

--

1

2

9

10

11

22

99

100

110

trailing Alpha + numeric

Id sorted by current collation

A2

A9

A10

A11

A110

B1

B22

B99

C100

specifying a specific collation

for the particular/required sort rules

Id sorted by Latin1_General_BIN

-

A2

A9

A10

A11

A110

B1

B22

B99

C100

obviously, you should be aware that when "sorting" values you have to consider the collation you are dealing with, as sort rules can be quite different among different collations.. my results depends (except the last one where a specific sort rule has been set via an explicit COLLATE [collation_name] clause) on my collation settings,

SELECT SERVERPROPERTY('Collation') AS [My default collation];

--<-

My default collation

Latin1_General_CI_AS

regards

|||Ok, you don′t need numeric fields as you don′t wan to have decimal values within your table in this column, right ? So you better use integer or any smaller integer data type. If you just want to use it for adhoc reporting and you cannot change the data type due to the consuming application, you could either use a cast within your order ORDER BY CAST(Thecolumn as int) or use another column to maintain the data, as the on-the-fly approach would not be very performant on heavy queries.

HTH, jens K. Suessmeyer.

http://www.sqlserver2005.de|||

Andrea, thank u very much. That resolved my question.

Pauley

|||

Jens, thank u very much. I will use the data type properly.

pauley

Monday, March 12, 2012

How to return sets by filtering those tuples with null values in a MDX statement?

Hi, dear friends,

How can we return the result by filtering tuples with null values? (e.g. I want to return the result for tuples specified by dimention DimA, measures B,C. therefore I want to filter the result with nulls values of B,C for DimA).

Hope it is clear for your help.

And I am looking forward to hearing from you shortly for your help.

Thanks a lot.

With kind regards,

Yours sincerely,

It depends what you mean by "filter" - do you want the null values included or excluded?

And what does B,C mean? Do you mean if either is null or both?

The following is one solution if you want to return members from DimA where both measures are empty (null).

Filter(DimA.members, IsEmtpy(Measures.B) AND IsEmpty(Measures.C)

but I would expect somthing like this could be faster as it could use block computation instead of cell-by-cell evaluation.

INTERSECT(NonEmpty(DimA.Members, Measures.B),NonEmpty(DimA.members, Measures.C))

|||

Hi, Darren,

Thanks a lot for your help.

With kind regards,

Yours sincerely,

How to return sets by filtering those tuples with null values in a MDX statement?

Hi, dear friends,

How can we return the result by filtering tuples with null values? (e.g. I want to return the result for tuples specified by dimention DimA, measures B,C. therefore I want to filter the result with nulls values of B,C for DimA).

Hope it is clear for your help.

And I am looking forward to hearing from you shortly for your help.

Thanks a lot.

With kind regards,

Yours sincerely,

It depends what you mean by "filter" - do you want the null values included or excluded?

And what does B,C mean? Do you mean if either is null or both?

The following is one solution if you want to return members from DimA where both measures are empty (null).

Filter(DimA.members, IsEmtpy(Measures.B) AND IsEmpty(Measures.C)

but I would expect somthing like this could be faster as it could use block computation instead of cell-by-cell evaluation.

INTERSECT(NonEmpty(DimA.Members, Measures.B),NonEmpty(DimA.members, Measures.C))

|||

Hi, Darren,

Thanks a lot for your help.

With kind regards,

Yours sincerely,

Friday, March 9, 2012

How to return 0 instead of null when using a sum function?

Hi,

I basically do not want to return a null value as a result of using a sum function (using sum against 0 rows).

Is there a common way to avoid this?

Thanx

Interesting you should ask. I was looking at the same problemyesterday. My results are displayed in a DataGrid and thankfully itdoesn't barf on the NULL values. It displays -1 instead. Of coursethat's still not desirable as the correct answer for SUM(nothing)should be 0 IMO.
Let's hope someone has a suggestion.
|||You could use the ISNULL function to calculate a 0 whenever the SUM contains a NULL:
SELECT ISNULL(SUM(myColumn,0)) FROM myTable WHERE 0=1

|||

Or, as a slight variation, use ISNULL to replace the result of the SUM for instances where no rows result in an answer of NULL:

SELECT ISNULL(SUM(myColumn),0) FROM myTable WHERE 0 = 1

(Note the different parenthesis placement: tmorton's will replace any NULL value with zero BEFORE aggregation; mine will replace theentire result with 0 AFTER aggregation in the event of a NULL sum).

|||

pjmcb wrote:


Or, as a slight variation, use ISNULL to replace the result of the SUM for instances where no rows result in an answer of NULL:
SELECT ISNULL(SUM(myColumn),0) FROM myTable WHERE 0 = 1
(Note the different parenthesis placement: tmorton's will replace anyNULL value with zero BEFORE aggregation; mine will replace the entireresult with 0 AFTER aggregation in the event of a NULL sum).


Thank you for catching my typo pjmcb! What I actually posted is not even syntactically correct. :-)
I should have copied-and-pasted from Query Analyzer instead of retyping.



|||I didn't think you could do that, but I was too lazy to open up QA to test it out...

Wednesday, March 7, 2012

How to Retrieve the Rows where particular Column Data equals NULL

Hi all,

I have a datatable Employee which is containg columns Emp_ID, Emp_Name, Manager_ID

I want to retrieve all the rows whose Manager_ID=NULL

I wirte the query select * from Employee where Manager_ID=NULL

but it is not working. How to do this. Please help me.

with regards

Ravinder Reddy.K

You have to use IS operator on NULL..

select * from Employee where Manager_ID is NULL

remember,

NULL = NULL => always False

NULL IS NULL = > True

|||

>NULL = NULL => always False

Not always... this is dependent on the setting of ANSI_NULLS. Try the following as an example:

SET ANSI_NULLS ON
GO
SELECT CASE WHEN NULL = NULL THEN 'YES'
ELSE 'NO'
END
GO
--Result: 'No'


SET ANSI_NULLS OFF
GO
SELECT CASE WHEN NULL = NULL THEN 'YES'
ELSE 'NO'
END
GO
--Result: 'Yes'

Chris

||| Forget the Settings BUDDY

Friday, February 24, 2012

how to retrieve a col definition NULL or NOT NULL

Hello,
I usually retreive information on columns using:
SELECT * FROM syscolumns order by name asc
SELECT * FROM systypes
systypes.names defines the type for instance.
But I am trying to find what defines NULL or NOT NULL for each columns.
Would someone know what defines it?
thanks for your helpDid you check the isnullable column in syscolumns?
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
"Wilfrid" <grille11@.yahoo.com> wrote in message news:479723a7$0$7041$426a74cc@.news.free.fr...
> Hello,
> I usually retreive information on columns using:
> SELECT * FROM syscolumns order by name asc
> SELECT * FROM systypes
> systypes.names defines the type for instance.
> But I am trying to find what defines NULL or NOT NULL for each columns.
> Would someone know what defines it?
> thanks for your help
>
>
>|||Next time I will open my eyes, sorry to bother.
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
message news:eVhTlfbXIHA.1188@.TK2MSFTNGP04.phx.gbl...
> Did you check the isnullable column in syscolumns?
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://sqlblog.com/blogs/tibor_karaszi
>
> "Wilfrid" <grille11@.yahoo.com> wrote in message
> news:479723a7$0$7041$426a74cc@.news.free.fr...
>> Hello,
>> I usually retreive information on columns using:
>> SELECT * FROM syscolumns order by name asc
>> SELECT * FROM systypes
>> systypes.names defines the type for instance.
>> But I am trying to find what defines NULL or NOT NULL for each columns.
>> Would someone know what defines it?
>> thanks for your help
>>
>>
>>
>|||No worries. Sometimes the obvious isn't so ... obvious. :-)
Also, if you are on 2005, consider using the catalog views instead, for instance sys.columns.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
"Wilfrid" <grille11@.yahoo.com> wrote in message news:479736f1$0$10823$426a74cc@.news.free.fr...
> Next time I will open my eyes, sorry to bother.
>
> "Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in message
> news:eVhTlfbXIHA.1188@.TK2MSFTNGP04.phx.gbl...
>> Did you check the isnullable column in syscolumns?
>> --
>> Tibor Karaszi, SQL Server MVP
>> http://www.karaszi.com/sqlserver/default.asp
>> http://sqlblog.com/blogs/tibor_karaszi
>>
>> "Wilfrid" <grille11@.yahoo.com> wrote in message news:479723a7$0$7041$426a74cc@.news.free.fr...
>> Hello,
>> I usually retreive information on columns using:
>> SELECT * FROM syscolumns order by name asc
>> SELECT * FROM systypes
>> systypes.names defines the type for instance.
>> But I am trying to find what defines NULL or NOT NULL for each columns.
>> Would someone know what defines it?
>> thanks for your help
>>
>>
>>
>>
>