Showing posts with label fields. Show all posts
Showing posts with label fields. Show all posts

Thursday, March 29, 2012

Case statement in Order by clause

Hi

I need a case statement with in the order clause,

There are fields like below

Feild A FieldB LevelA LevelB

xxxx xxxx

xxxx xxxx

xxxx xxxx 3 2

xxxxx xxxx 2 1

xxxxxx xxxxx 1 1

I need to sort LevelA and LevelB first leaving out the null or blank values in the top rows

Feild A FieldB LevelA LevelB

xxxx xxxx 1 1

xxxx xxxx 2 1

xxxx xxxx 3 2

xxxxx xxxx

xxxxxx xxxxx

Thanks

like this:

Code Snippet

create table #t (FieldA varchar(10), FieldB varchar(10), LevelA int, LevelB int)

insert into #t

select 'xxxx', 'xxxx', null, null

union all select 'xxxx', 'xxxx', null, null

union all select 'xxxx', 'xxxx', 3, 2

union all select 'xxxxx', 'xxxx', 2, 1

union all select 'xxxxxx', 'xxxxx', 1, 1

union all select 'xxxxxx', 'xxxxx', 1, 2

union all select 'xxxxxx', 'xxxxx', 1, 3

select *

from #t

order by case when LevelA is not null then 1 else 9 end,

LevelA,

case when LevelB is not null then 1 else 9 end,

LevelB

|||

You can use a CASE expression like below:

order by coalesce(LevelA, power(2., 31)-1), coalesce(LevelB, power(2., 31)-1)

|||Umachandar,

Some readers might why you said "CASE expression," so I'll remark that COALESCE(a,b) is basically the same as CASE WHEN a IS NULL THEN b ELSE a END.

Sowree,

I think the suggestion below will work, and here I've added the requirement that blank values appear last. (If Angel or Beer are number types, they can't be blank, and this code will incorrectly put values of 0 last, because '' is implicitly converted to 0 in the comparison.)

ORDER BY
CASE WHEN a IS NULL OR a = '' THEN 1 ELSE 0 END,
a,
CASE WHEN b IS NULL OR b = '' THEN 1 ELSE 0 END,
b

Your question isn't entirely clear, so you may have to adapt the suggestions to your exact need.

Steve Kass
Drew University
http://www.stevekass.com

Tuesday, March 27, 2012

CASE statement

I am trying to write a case statement that will look at a relationship between two fields. I have done this in the past but not one that needs to look at various options in one of the fields.

Example. I am comparing a location code to a code where an employee's check is mailed.

CASE
WHEN EjLocation ='AGACES'
THEN '3410'

But I need to also include 3415 and any check code that begins with 83 (i.e. 8315, 8350). How do I include this in the THEN statement?

Thanks!Could you post a small sample set and the result set you would like to see?|||Originally posted by Teddy
Could you post a small sample set and the result set you would like to see?

Here is the query that I have started. Just need to work on the CASE statement.
My goal is to just show employees whose EjLocation doesn't match the PrbCheckSeqNo listed in the THEN statement.

IE
Name Location CheckCode
John Smith AGACES 8500

This person would show up because they don't equal 3410, 3415 or a code starting with 83##.

SELECT EpLastName, EpFirstName, EjLocation, PrbCheckSeqNo
FROM EJob, EBase, EPayrollBase
/* Check codes not matching facility code- excludes virtuals
Dev. T.Rucker 1/04 */
WHERE EpFlxideb = EjFlxideb AND Ebflxid = Prbflxideb AND PrbDateend is null AND EjDateEnd IS NULL AND Epflxideb = EeFlxideb AND
EeDateEnd IS NULL AND NOT PrbCheckSeqNo = '0000' AND
CASE
WHEN EjLocation ='AGACES'
THEN '3410'
WHEN EjLocation = 'BALCES'
THEN '2300'
ELSE 'O'
END <> PrbCheckSeqNo
ORDER BY EjLocation, PrbCheckSeqNo

Tuesday, March 20, 2012

Case in Join

Can you put a Case statement in a Join?
My problem is that I have 2 possible fields I want to join to the same
table. If one is null, use the other.
For example:
Create Table Position
(
UserID1 int,
UserID2 int
)
Create Table Logon
(
UserID int,
UserName
)
I want to do something like (I know this doesn't work, but you should get
the idea of what I am looking for from it).
Select UserName From Position P Join on Logon L (CASE WHEN UserID1 is not
null Then (P.UserID1 = L.UserID) ELSE (P.UserID2 = L.UserID) END)
I am trying to get the User Name from UserID1, unless it is null. If that
is the case, then get the User Name from UserID2.
Thanks,
TomSelect UserName From Position P Join Logon L
-- the join condition implies that UserID1 is not null
on P.UserID1 = L.UserID
union all
Select UserName From Position P Join on Logon L on P.UserID2 = L.UserID
where UserID1 is null|||The case statement will work, but the isnull function is better.
select isnull(columnA, columnB).
If you have multiple columns to compare for nulls use coalesce. select
coalesce(columnA,columnB,columnC...)|||"Gary Gibbs" <ggibbs@.aahs.org> wrote in message
news:1135974528.225930.160070@.g44g2000cwa.googlegroups.com...
> The case statement will work, but the isnull function is better.
> select isnull(columnA, columnB).
I tried to use the Case statement, but got an error on the "=" sign.

> If you have multiple columns to compare for nulls use coalesce. select
> coalesce(columnA,columnB,columnC...)
I don't see how the isnull or coalesce.helps me. If I were looking for the
IsNull from the data that would be fine, but in my case I have 2 UserID,
which would be different records in the Logon table. I need to use a Join
to get the correct UserName, I believe. Using the isnull would get me the
correct UserID, but I still don't have the User Name for that UserID.
Perhaps 2 separate joins.
Thanks,
Tom|||tshad wrote:

> Can you put a Case statement in a Join?
Of course...

> Create Table Position
> (
> UserID1 int,
> UserID2 int
> )
> Create Table Logon
> (
> UserID int,
> UserName
> )
> I want to do something like (I know this doesn't work, but you should get
> the idea of what I am looking for from it).
> Select UserName From Position P Join on Logon L (CASE WHEN UserID1 is not
> null Then (P.UserID1 = L.UserID) ELSE (P.UserID2 = L.UserID) END)
Select UserName
From Position P Join Logon L
on coalesce(P.UserID1, P.UserID2) = L.UserID
Dieter|||Using Case:
Select UserName From Position P Join Logon L On L.UserId = Case When
P.UserId1 Is Not Null Then P.UserId1 Else P.UserId2 End
Using Coalesce:
Select UserName From Position P Join Logon L On L.UserId =
Coalesce(P.UserId1, P.UserId2)
Using Isnull:
Select UserName From Position P Join Logon L On L.UserId =
Isnull(P.Userid1, P.UserId2)
tshad wrote:
> Can you put a Case statement in a Join?
> My problem is that I have 2 possible fields I want to join to the same
> table. If one is null, use the other.
> For example:
> Create Table Position
> (
> UserID1 int,
> UserID2 int
> )
> Create Table Logon
> (
> UserID int,
> UserName
> )
> I want to do something like (I know this doesn't work, but you should get
> the idea of what I am looking for from it).
> Select UserName From Position P Join on Logon L (CASE WHEN UserID1 is not
> null Then (P.UserID1 = L.UserID) ELSE (P.UserID2 = L.UserID) END)
> I am trying to get the User Name from UserID1, unless it is null. If that
> is the case, then get the User Name from UserID2.
> Thanks,
> Tom
>|||Is performance a concern for you? Are the tables big?|||tshad (tscheiderich@.ftsolutions.com) writes:
> "Gary Gibbs" <ggibbs@.aahs.org> wrote in message
> news:1135974528.225930.160070@.g44g2000cwa.googlegroups.com...
> I tried to use the Case statement, but got an error on the "=" sign.
CASE is not a statement in T-SQL, it is an expression. And just like
any other expression it returns a value. Thus what you had:
Select UserName
From Position P
Join on Logon L (CASE WHEN UserID1 is not null
Then (P.UserID1 = L.UserID)
ELSE (P.UserID2 = L.UserID)
END)
Does not cut it, because
1) ON appears to eaarly, it should come after the table with its alias.
2) The JOIN operator is followed by a boolean expression, but CASE
can never return boolean, because there is no boolean datatype in
SQL.
3) And therefore the value of one branch in the CASE cannot be
"P.UserID1 = L.UserID".
This you can write:
Select UserName
From Position P
Join Logon L ON L.UserUD = CASE WHEN P.UserID1 is not null
Then P.UserID1
ELSE P.UserID2
END
coalesce(P.UserID1, P.UserID2) is a short-hand notation for the same
thing.
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|||"Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
news:Xns973CF3D7CD071Yazorman@.127.0.0.1...
> tshad (tscheiderich@.ftsolutions.com) writes:
> CASE is not a statement in T-SQL, it is an expression. And just like
> any other expression it returns a value. Thus what you had:
That makes sense now.

> Select UserName
> From Position P
> Join on Logon L (CASE WHEN UserID1 is not null
> Then (P.UserID1 = L.UserID)
> ELSE (P.UserID2 = L.UserID)
> END)
> Does not cut it, because
> 1) ON appears to eaarly, it should come after the table with its alias.
> 2) The JOIN operator is followed by a boolean expression, but CASE
> can never return boolean, because there is no boolean datatype in
> SQL.
> 3) And therefore the value of one branch in the CASE cannot be
> "P.UserID1 = L.UserID".
> This you can write:
> Select UserName
> From Position P
> Join Logon L ON L.UserUD = CASE WHEN P.UserID1 is not null
> Then P.UserID1
> ELSE P.UserID2
> END
That works great.

> coalesce(P.UserID1, P.UserID2) is a short-hand notation for the same
> thing.
This also would work. I misunderstood what Gary was saying.
Thanks,
Tom

>
> --
> 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

Case expressions may only be nested to level 10

I'm running into this issue when I try to query a table (well a view) that was poorly designed and has many fields in the table as YesNo fields instead of a single field that just contains a value. (Fields A-N in the table for example)

So my query looks like:

CASE
WHEN A = 1 THEN 'A'
WHEN B = 1 THEN 'B'
WHEN C = 1 THEN 'C'
WHEN D = 1 THEN 'D'
WHEN E = 1 THEN 'E'
WHEN F = 1 THEN 'F'
WHEN G = 1 THEN 'G'
WHEN H = 1 THEN 'H'
WHEN I = 1 THEN 'I'
WHEN J = 1 THEN 'J'
WHEN K = 1 THEN 'K'
WHEN L = 1 THEN 'L'
WHEN M = 1 THEN 'M'
WHEN N = 1 THEN 'N'
END AS GeneratedField

Is there some other way I can approach this? I can't change the table as it isn't mine, but I need to run a report that looks at those fields and in GeneratedField places the result for the field that was checked.

Thanks.

Yes. Case Expression can be nested upto 10 Level.

But your expression is not looking like a nested expression.

Can you post the exact expression you are using.. Is any one of these A-N columns hold the value as 1 and reset 0.

|||

Ars_maxer,

There is another way of doing it, it's complicated than simpe case statement, though. Use Unpivot.

Here it is.

Code Snippet


declare @.tb table (
id int,
A int,
B int,
C int,
D int
);
insert into @.tb values(1, 1,0,0,0)
insert into @.tb values(2, 0,1,0,0)
insert into @.tb values(3,0,0,1,0)
insert into @.tb values(4,1,0,0,0)

select id, genvalue, vvalue from
( select id, A,B,C,D from @.tb)p
UNPIVOT
(vvalue for genvalue in (A,B,C,D) ) as unpvt
where vvalue =1


id genvalue vvalue
1 A 1
2 B 1
3 C 1
4 A 1

|||

This is a little hoakie, but here we go:

Code Snippet

declare @.a int, @.b int, @.c int, @.d int, @.e int

set @.a = 0

set @.b = 0

set @.c = 1

set @.d = 0

set @.e = 0

selectcoalesce(replace(convert(char(1),nullif(@.a,0)),'1','A'),

replace(convert(char(1),nullif(@.b,0)),'1','B'),

replace(convert(char(1),nullif(@.c,0)),'1','C'),

replace(convert(char(1),nullif(@.d,0)),'1','D'),

replace(convert(char(1),nullif(@.e,0)),'1','E')

)

|||

I'm not doing anything that isn't shown in my SQL statement there (Other than a SELECT and FROM and WHERE). However, nothing unusual.

Now if I run that SQL with say 8 statements then it works just fine.

I've read that this appears to be an issue and that others have run into it before. In some cases they wrapped the whole thing in a stored procedure on the linked server or their server and ran it that way.

In my case I'm executing this against a linked server and can't make a sproc there.

I'll play with the examples provided above and see if those will address the issue for me or not.

Thanks!

Monday, March 19, 2012

Case / Switch function help

Starting to play around with SQL server at work and this is my question:

In the query design mode in access I can make one of the fields an expression that is driven by a built-in switch function.
i.e. Switch([CategoryName] Like "Beverage","Drink",[CategoryName] Like "Cheese","Dairy")
This results in the additional column field I created to display "Drink" for each record that has the CategoryName = "Beverage", and "Dairy" for "Cheese".

Can I do something like this in SQL server in the view designer itself, or do I need to make a user defined function and call it?

Thanks in advance for any help.Use the CASE statement in SQL Server.|||Can I do this in the view design mode or do I need to make a user defined function?
Any sample code would be really appreciated.|||CASE WHEN [CategoryName] = 'Beverage' THEN 'Drink'
WHEN [CategoryName] = 'Cheese' THEN 'Dairy'
ELSE 'Unknown'
END AS DerivedColumnName

Thursday, March 8, 2012

Cascaded SELECT for multiple fields

Consider 2 tables Tabe1, Table2 which both have int fields fld1, fld2.
I can do a Cascaded Select on 1 field:
SELECT fld1 FROM Table1 WHERE fld1 IN (SELECT fld1 FROM Table2)
or
SELECT fld1 FROM Table1 WHERE fld1 NOT IN (SELECT fld1 FROM Table2)
and I can rewrite it as a INNER JOIN, no problem. So far so good.
But now what happens when it is _BOTH_ pair of fields I wish to check
whether they exist/not exist in Table 2?
For Cascaded Selects, I am stuffed. There is no (at least I am unaware)
SELECT fld1, fld2 FROM Table1 WHERE fld1,fld2 IN (SELECT fld1, fld2 FROM
Table2)
I have to write this is a INNER JOIN which will work. At least there is a
solution here.
But consider the 4th case. I am stuffed here regardless of doing Cascaded
Select or Join. You want to say
SELECT fld1, fld2 FROM Table1 WHERE fld1,fld2 NOT IN (SELECT fld1, fld2 FROM
Table2)
And I cannot rewrite this as an inner join because rows are only considered
when both fields, _jointly_ are absent from Table2.
There must be a solution here for multiple fields.
In real life I do have 2 tables. And I can gurantee that fld1 is present on
both tables. But I am interested in listing the pairs of fields (fld1, fld2)
present on one table but absent on the other.
Question1) Can it be done with Cascade Selects? How?
Question2) Can it be done with Joins? How?
Thanks
Stephen HoweThis should work:
SELECT Table1.fld1, Table1.fld2
FROM Table1 Table1
LEFT JOIN Table2 Table2
ON Table1.fld1 = Table2.fld1 AND Table1.fld2 = Table2.fld2
WHERE Table2.fld1 IS NULL
If the pair exists in table2, then Table2.fld1 won't be NULL.|||> This should work:
> SELECT Table1.fld1, Table1.fld2
> FROM Table1 Table1
> LEFT JOIN Table2 Table2
> ON Table1.fld1 = Table2.fld1 AND Table1.fld2 = Table2.fld2
> WHERE Table2.fld1 IS NULL
> If the pair exists in table2, then Table2.fld1 won't be NULL.
I was not clear in my labeling of cases. They are
Case 1: SELECTing on single fields present on Table 1 that should be
present on Table 2
Case 2: SELECTing on single fields present on Table 1 that should not be
present on Table 2
Case 3: SELECTing on multiple fields present on Table 1 that should be
present on Table 2
Case 4: SELECTing on multiple fields present on Table 1 that should not be
present on Table 2
As far as I know, JOINs can handle Cases 1-3 but not case 4
As far as I know, cascaded Selects can handle Cases 1-2 but not cases 3-4
Hrrrmmm after examination, you are right. Thanks
Stephen Howe|||Does this do what you want?:
select t1.fld1, t1.fld2
from Table1 t1
left outer join Table2 t2
on t1.fld1 = t2.fld1
and t1.fld2 = t2.fld2
where t2.fld1 is null;
HTH
Vern
"Stephen Howe" wrote:

> Consider 2 tables Tabe1, Table2 which both have int fields fld1, fld2.
> I can do a Cascaded Select on 1 field:
> SELECT fld1 FROM Table1 WHERE fld1 IN (SELECT fld1 FROM Table2)
> or
> SELECT fld1 FROM Table1 WHERE fld1 NOT IN (SELECT fld1 FROM Table2)
> and I can rewrite it as a INNER JOIN, no problem. So far so good.
> But now what happens when it is _BOTH_ pair of fields I wish to check
> whether they exist/not exist in Table 2?
> For Cascaded Selects, I am stuffed. There is no (at least I am unaware)
> SELECT fld1, fld2 FROM Table1 WHERE fld1,fld2 IN (SELECT fld1, fld2 FROM
> Table2)
> I have to write this is a INNER JOIN which will work. At least there is a
> solution here.
> But consider the 4th case. I am stuffed here regardless of doing Cascaded
> Select or Join. You want to say
> SELECT fld1, fld2 FROM Table1 WHERE fld1,fld2 NOT IN (SELECT fld1, fld2 FR
OM
> Table2)
> And I cannot rewrite this as an inner join because rows are only considere
d
> when both fields, _jointly_ are absent from Table2.
> There must be a solution here for multiple fields.
> In real life I do have 2 tables. And I can gurantee that fld1 is present o
n
> both tables. But I am interested in listing the pairs of fields (fld1, fld
2)
> present on one table but absent on the other.
> Question1) Can it be done with Cascade Selects? How?
> Question2) Can it be done with Joins? How?
> Thanks
> Stephen Howe
>
>
>|||Stephen Howe (stephenPOINThoweATtns-globalPOINTcom) writes:
> I can do a Cascaded Select on 1 field:
> SELECT fld1 FROM Table1 WHERE fld1 IN (SELECT fld1 FROM Table2)
> or
> SELECT fld1 FROM Table1 WHERE fld1 NOT IN (SELECT fld1 FROM Table2)
> and I can rewrite it as a INNER JOIN, no problem. So far so good.
> But now what happens when it is _BOTH_ pair of fields I wish to check
> whether they exist/not exist in Table 2?
> For Cascaded Selects, I am stuffed. There is no (at least I am unaware)
> SELECT fld1, fld2 FROM Table1 WHERE fld1,fld2 IN (SELECT fld1, fld2 FROM
> Table2)
There is in ANSI SQL I believe, but ont implemented in SQL Server.
Anyway, all you need to learn is the EXISTS clause:
SELECT fld1, fld2
FROM Table1 a
WHERE EXISTS (SELECT *
FROM Table2 b
WHERE a.fld1 = b.fld1
AND a.fld2 = b.fld2)
For the corresponding to NOT IN, use NOT EXISTS.
EXISTS/NOT EXISTS is in my opinion superior to IN/NOT IN. There is a
trap with nullable columns that you easily can fall into with NOT IN.
Does not happen with NOT EXISTS.
Performancewise, IN/NOT IN queries are usually rewritten internally
to EXISTS /NOT EXISTS, so there is no difference.
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|||> Performancewise, IN/NOT IN queries are usually rewritten internally
> to EXISTS /NOT EXISTS, so there is no difference.
Thanks Erland and everyone else.
My mind has been opened.
Stephen Howe

Cascade Update and Delete Related Fields..

I am building tables using the database diagram with enterprise manager. One of the options available is to "Cascade Update and Delete Related Fields". I think this means that if I change a PK Identifier in a table all related FK's will be updated to the new value and if I delete the PK all related FK records will be deleted. Is this correct? Is this a good idea to do something like this?

I have always handled what needs to be changed using SP's following business rules. A friend has been preaching the use of the "Cascade Update and Delete Related Fields" functionality provided with the database.

Please let me know what you think.

ThanksAs you already mention, I think this is a matter of opinion. The advantage for cascading is that you don't have to do any checking and programming to avoid data inconstency. Disadvantage is that you don't have any control about the updates/deletes of the related records. For the manual programming through sp's it's offcourse the other way around.
In other words: you have to see in every situation what's the best solution.

Cartesian Join

Hello,
I have a table tblCustomer with fields CustomerName and Revenue. I have a
second table with Revenue ranges
LowRange HighRange Bucket
0 10 1
10 20 2
20 30 3
Is there a way to use a Cross Join to create the following output
Customer Revenue Bucket
Cust1 5 1
Cust2 28 3
--
Thanks in advance,
Steven"Steven K" <skaper@.troop.com> wrote in message
news:e5ySMdqvDHA.2148@.TK2MSFTNGP12.phx.gbl...
> Hello,
> I have a table tblCustomer with fields CustomerName and Revenue. I have a
> second table with Revenue ranges
> LowRange HighRange Bucket
> 0 10 1
> 10 20 2
> 20 30 3
> Is there a way to use a Cross Join to create the following output
> Customer Revenue Bucket
> Cust1 5 1
> Cust2 28 3
>
> --
Sure. It's not even a Cross Join, since each customer is matched with one
Bucket.
Select Customer, Revenue, Bucket
from Customer join Bucket
on Customer.Revenue >= Bucket.LowRange
and Customer.Revenue < Bucket.HighRange
David|||It's not a cross join. Use a LEFT OUTER, RIGHT OUTER or INNER join depending
on your exact requirements:
SELECT C.customer, C.revenue, R.bucket
FROM Customers AS C
LEFT JOIN RevenueRanges AS R
ON C.revenue >= R.lowrange AND C.revenue < R.highrange
I'm assuming that none of the ranges in RevenueRanges will overlap.
--
David Portas
--
Please reply only to the newsgroup
--

Cart data row

I have a SQL table with summary data. I want to display a pie chart for fields in each row and a table that displays values from the corresponding row.

I have a table and pie chart defined. However the pie chart only displays for the first row in the table. The table repeats ok for each row in the table. I have tried placing a rectangle on the report and putting the chart and the table in the rectangle but it still works the same. The table and pie chart use the same dataset.

I have read about nesting data regions but I cannot see how to make a link between the table and pie chart.

You need to add a grouping (on RowNumber() for example) to the chart in order to display each row in the dataset. The difference between chart and table is that table displays details when there's no group expressions, while chart also shows groups and aggregated data.

Wednesday, March 7, 2012

Carriage returns

I'm migrating from Crystal. When I inserted address fields which contained
carriage returns, Crystal seemed to recognise them automatically, putting
the second line beneath the first line, etc. RS doesn't do this - obviously
I have to do something more explicit. Cam anybody help?
ThanksFirst: You need SP1. Second: I've only had success with carriage returns
when there physically was a CRLF pair. With the HTML output, the <BR> tags
are not rendered unless said CRLF pair exist.
--
TIM ELLISON
"Microsoft" <mitchellpaul@.blueyonder.co.uk> wrote in message
news:%23Kkhi714EHA.524@.TK2MSFTNGP09.phx.gbl...
> I'm migrating from Crystal. When I inserted address fields which contained
> carriage returns, Crystal seemed to recognise them automatically, putting
> the second line beneath the first line, etc. RS doesn't do this -
obviously
> I have to do something more explicit. Cam anybody help?
> Thanks
>|||Seem to have got this sorted with an expression:
=replace((Fields!AD_ADDRESS.Value),chr(13),vbcrlf)
Thanks for the help anyway, will get SP1 installed pronto...
"Tim" <TimmyDotNet@.direcway.com> wrote in message
news:eINaxi24EHA.1976@.TK2MSFTNGP09.phx.gbl...
> First: You need SP1. Second: I've only had success with carriage returns
> when there physically was a CRLF pair. With the HTML output, the <BR>
> tags
> are not rendered unless said CRLF pair exist.
> --
> TIM ELLISON
> "Microsoft" <mitchellpaul@.blueyonder.co.uk> wrote in message
> news:%23Kkhi714EHA.524@.TK2MSFTNGP09.phx.gbl...
>> I'm migrating from Crystal. When I inserted address fields which
>> contained
>> carriage returns, Crystal seemed to recognise them automatically, putting
>> the second line beneath the first line, etc. RS doesn't do this -
> obviously
>> I have to do something more explicit. Cam anybody help?
>> Thanks
>>
>

Carriage Return Bug in Management Studio?

I'm using SQL Server 2005 Manage Studio to edit some varchar and text fields
I have in my database. I want to insert carriage returns in my text, and I
tried CTRL+Enter which works fine in SQL 2000, but it doesn't seem to work in
Management Studio. Further more, when I import data from my SQL 2000 and then
open the table for editing in Management Studio I find that all the carriage
returns are not visible. I say not visible, because I know they're still
there because my application reads them fine. Is this a UI bug in Management
Studio?!!! Anyone knows of a fix or work around? Thanks.Hi, Waleed,
+char(13)+char(10) could insert a carriage return and line feed.
Ta,
Yifei
"Waleed Abdulla xrules org>" <waleed_abdulla <atdot> wrote in message
news:E59CC994-B548-45CE-837A-E6C71A366C5F@.microsoft.com...
> I'm using SQL Server 2005 Manage Studio to edit some varchar and text
fields
> I have in my database. I want to insert carriage returns in my text, and I
> tried CTRL+Enter which works fine in SQL 2000, but it doesn't seem to work
in
> Management Studio. Further more, when I import data from my SQL 2000 and
then
> open the table for editing in Management Studio I find that all the
carriage
> returns are not visible. I say not visible, because I know they're still
> there because my application reads them fine. Is this a UI bug in
Management
> Studio?!!! Anyone knows of a fix or work around? Thanks.|||Yifei,
Thanks, but you're answering the wrong question. As I mentioned, reading
and inserting carriage returns programmatically (char 13 + char 10) is no
problem. My issue is in the UI when trying to type in carriage returns in the
data directly in the table view in Management Studio.
Waleed
"Yifei Jiang" wrote:
> Hi, Waleed,
> +char(13)+char(10) could insert a carriage return and line feed.
> Ta,
> Yifei
> "Waleed Abdulla xrules org>" <waleed_abdulla <atdot> wrote in message
> news:E59CC994-B548-45CE-837A-E6C71A366C5F@.microsoft.com...
> > I'm using SQL Server 2005 Manage Studio to edit some varchar and text
> fields
> > I have in my database. I want to insert carriage returns in my text, and I
> > tried CTRL+Enter which works fine in SQL 2000, but it doesn't seem to work
> in
> > Management Studio. Further more, when I import data from my SQL 2000 and
> then
> > open the table for editing in Management Studio I find that all the
> carriage
> > returns are not visible. I say not visible, because I know they're still
> > there because my application reads them fine. Is this a UI bug in
> Management
> > Studio?!!! Anyone knows of a fix or work around? Thanks.
>
>

carraige return

This is an easy question, I have two address fields and I have combine them
together but I want address_2 to show up on the next line instead of have a
sapce between address1 and address2
Any Idea
Thank in Advance
=Fields!BILLING_ADDRESS.Value & " " Fields!BILLING_ADDRESS_2.Value=Fields!BILLING_ADDRESS.Value & ControlChars.CrLf &
Fields!BILLING_ADDRESS_2.Value
--
Med Bouchenafa
"superQ" <superQ@.discussions.microsoft.com> a écrit dans le message de news:
5E9BA72C-7B82-447F-BA54-4F8251A3E9A8@.microsoft.com...
> This is an easy question, I have two address fields and I have combine
> them
> together but I want address_2 to show up on the next line instead of have
> a
> sapce between address1 and address2
> Any Idea
> Thank in Advance
> =Fields!BILLING_ADDRESS.Value & " " Fields!BILLING_ADDRESS_2.Value

Saturday, February 25, 2012

Capturing results from SPROC

I need to execute a SPROC from another SPROC that returns 1 row with three fields. How do I capture the data out of those?

Have sproc #1 create a #Temp table,

then have sproc #2 populate that #Temp table.

The data will then be available in sproc #1.

|||If the stored procedure will always return 1 or less rows another good alternative is to have the stored procedure return three OUTPUT parameters instead of a row with three columns.|||

Here you can use either Insert Into ...Exec or OUTPUT param.

Code Snippet

--Option 1: If you are not autorized to change the Procedure code.

Create Proc #TestProc1

as

Select 1 Col1, 'Test data' Col2, Getdate() Col3

Go

Create Table #TempData (

Col1 int,

Col2 Varchar(100),

Col3 Datetime

)

Insert Into #TempData

Exec #TestProc1

Select * from #TempData

Code Snippet

--Option 2: Best Fit

Create Proc #TestProc2(

@.Col1 as Int OUTPUT,

@.Col2 as Varchar(100) OUTPUT,

@.Col3 as DateTime OUTPUT

)

as

Select

@.Col1 = 1

,@.Col2 = 'Test data'

,@.Col3 = Getdate()

Go

Declare @.Col1 as Int,

@.Col2 as Varchar(100) ,

@.Col3 as DateTime

Exec #TestProc2 @.Col1 OUTPUT, @.Col2 OUTPUT, @.Col3 OUTPUT

Select @.Col1, @.Col2, @.Col3

Thursday, February 16, 2012

Capitalisation

Hi Guys

Anybody tell me where of if there is a setting to set
whether or not the fields and table names need to be case
specific on SQL 2000.

I've been using SQL Server 7 where it has not been the case that the table and field names had had to be the same capitalisation-wise in running.

ie select * from tbl_example would work if the table name was Tbl_Example

I've just been trying to run the same queries on a different
server, which is running SQL 2000, and they won't execute unless the
capitalisation on the table and field names are exactly
the same.

There must be a simple setting somewhere please!You must change the collation settings. To change one or more of these settings, you must rebuild the master and user databases.

How to rebuild the master database (Rebuild Master utility)
To rebuild the master database

Shutdown Microsoft SQL Server 2000, and then run Rebuildm.exe. This is located in the Program Files\Microsoft SQL Server\80\Tools\Binn directory.

In the Rebuild Master dialog box, click Browse.

In the Browse for Folder dialog box, select the \Data folder on the SQL Server 2000 compact disc or in the shared network directory from which SQL Server 2000 was installed, and then click OK.

Click Settings. In the Collation Settings dialog box, verify or change settings used for the master database and all other databases.
Initially, the default collation settings are shown, but these may not match the collation selected during setup. You can select the same settings used during setup or select new collation settings. When done, click OK.

In the Rebuild Master dialog box, click Rebuild to start the process.
The Rebuild Master utility reinstalls the master database.

Note To continue, you may need to stop a server that is running.

Needless to say, you have to be extremely careful if you are trying to re-build your master or user databases. So I would suggest you do it only if it is abosolutely necessary. Refer to BOL for more information.

All the best

Originally posted by garethimh
Hi Guys

Anybody tell me where of if there is a setting to set
whether or not the fields and table names need to be case
specific on SQL 2000.

I've been using SQL Server 7 where it has not been the case that the table and field names had had to be the same capitalisation-wise in running.

ie select * from tbl_example would work if the table name was Tbl_Example

I've just been trying to run the same queries on a different
server, which is running SQL 2000, and they won't execute unless the
capitalisation on the table and field names are exactly
the same.

There must be a simple setting somewhere please!|||Originally posted by sbaru
You must change the collation settings. To change one or more of these settings, you must rebuild the master and user databases.

How to rebuild the master database (Rebuild Master utility)
To rebuild the master database

Shutdown Microsoft SQL Server 2000, and then run Rebuildm.exe. This is located in the Program Files\Microsoft SQL Server\80\Tools\Binn directory.

In the Rebuild Master dialog box, click Browse.

In the Browse for Folder dialog box, select the \Data folder on the SQL Server 2000 compact disc or in the shared network directory from which SQL Server 2000 was installed, and then click OK.

Click Settings. In the Collation Settings dialog box, verify or change settings used for the master database and all other databases.
Initially, the default collation settings are shown, but these may not match the collation selected during setup. You can select the same settings used during setup or select new collation settings. When done, click OK.

In the Rebuild Master dialog box, click Rebuild to start the process.
The Rebuild Master utility reinstalls the master database.

Note To continue, you may need to stop a server that is running.

Needless to say, you have to be extremely careful if you are trying to re-build your master or user databases. So I would suggest you do it only if it is abosolutely necessary. Refer to BOL for more information.

All the best

Brilliant, thanks!|||sbaru's suggetion is dead on but remember to make a backup of everything first.