Monday, March 19, 2012
CASE and Inserting Commas
on Race. I have the following columns...
RaceAA(American Indian or Alaska Native)
RaceNH(Native Hawaiian)
RaceA(Asian)
RaceW(White)
RaceB(Black)
There are 'X's in these columns designating the Race and more than one may
be 'X'ed.
I want to transform the 'X's into numbers with commas in between. For
Example, if Tiger Woods came into our DataBase, the RaceA would have an 'X'
and needs to be transposed to '2', RaceW would have an 'X' and needs to be
transformed to '5', and RaceB would have an 'X' and needs to be transposed t
o
'3'.
My result should look like...2,5,3
CASE WHEN RaceA = 'X' then '2,'
WHEN RaceW = 'X' then '5,'
WHEN RaceB = 'X' then '3,'
would give me 2,5,3,
My concern here is the trailing ",". Is there any way to CASE the comma if
multiple Race columns are checked and work in the commas appropriately?
Am I asking SQL questions in the right forum?> I have the following columns...
> RaceAA(American Indian or Alaska Native)
> RaceNH(Native Hawaiian)
> RaceA(Asian)
> RaceW(White)
> RaceB(Black)
Why? Why not have a column called "Race" and then provide values to that
column, such as 'AA','NH','A','W','B', etc.
> There are 'X's in these columns designating the Race and more than one may
> be 'X'ed.
Someone can be Asian and Black and White and American Indian?
> I want to transform the 'X's into numbers with commas in between. For
> Example, if Tiger Woods came into our DataBase, the RaceA would have an
> 'X'
> and needs to be transposed to '2', RaceW would have an 'X' and needs to be
> transformed to '5', and RaceB would have an 'X' and needs to be transposed
> to
> '3'.
Why do you want to store this race code mapping in your code? Wouldn't it
make more sense to use a numeric code *in the data* (and have a lookup
table)? Then you don't have to go read stored procedure code to figure out
what the heck kind of person a 3 is, and you don't have to worry about
making sure all queries that use this logic match.
> would give me 2,5,3,
> My concern here is the trailing ",". Is there any way to CASE the comma if
> multiple Race columns are checked and work in the commas appropriately?
Well, you could produce 2,5,3, in a subquery, and in the outer query, say:
SELECT RaceList = CASE RIGHT(List,1)
WHEN ',' THEN LEFT(List, LEN(List)-1)
ELSE List
END
FROM
(
SELECT List = '2,5,3,'
UNION
SELECT List = '4'
) x
Or, you could do something really novel, and inject the commas where they
belong, on the presentation tier.
> Am I asking SQL questions in the right forum?
Yes but you should look at alternative designs. This is far from optimal.|||CASE seems to Mutually Exclusive. Is there any way to continue the CASE
statement so it looks at all the race columns and builds the result column
accordingly. In other words, once it sees that the "White" column has an "X"
,
it goes to the end when I need it to look at ALL the race columns.
Any help you could provide me here would be GREATLY appreciated!
Thanks!
"Aaron Bertrand [SQL Server MVP]" wrote:
> Why? Why not have a column called "Race" and then provide values to that
> column, such as 'AA','NH','A','W','B', etc.
>
> Someone can be Asian and Black and White and American Indian?
>
> Why do you want to store this race code mapping in your code? Wouldn't it
> make more sense to use a numeric code *in the data* (and have a lookup
> table)? Then you don't have to go read stored procedure code to figure ou
t
> what the heck kind of person a 3 is, and you don't have to worry about
> making sure all queries that use this logic match.
>
> Well, you could produce 2,5,3, in a subquery, and in the outer query, say:
> SELECT RaceList = CASE RIGHT(List,1)
> WHEN ',' THEN LEFT(List, LEN(List)-1)
> ELSE List
> END
> FROM
> (
> SELECT List = '2,5,3,'
> UNION
> SELECT List = '4'
> ) x
> Or, you could do something really novel, and inject the commas where they
> belong, on the presentation tier.
>
> Yes but you should look at alternative designs. This is far from optimal.
>
>|||You can string the CASE expressions together:
CASE WHEN X = 1 THEN 'A' ELSE '' END+
CASE WHEN X = 2 THEN 'B' ELSE '' END+
CASE WHEN X = 3 THEN 'C' ELSE '' END+ ...
but I share Aaron's views about the weakness of the design you are
implying. In particular, it's a mistake to design tables to mirror a UI
- columns are not check boxes!
David Portas
SQL Server MVP
--|||or:
CASE WHEN col1 = 1 THEN 'A' ELSE '' END+
CASE WHEN col2 = 1 THEN 'B' ELSE '' END+
CASE WHEN col3 = 1 THEN 'C' ELSE '' END+ ...
David Portas
SQL Server MVP
--
Wednesday, March 7, 2012
carriage return problem..
VB.Net
<%# Eval("MyValue").ToString().Replace(vbcrlf,"<br />") %>
C#
<%# Eval("MyValue").ToString().Replace("\r","<br />") %>
Thanks!
It works great!
I was also able to get it going with the following a few minutes ago:
<%# Eval("MyValue").Replace(Environment.NewLine, "<br />") %>
Which method do you recommend or are they both good?
|||With the .NET framework, there are approximately 63 ways to skin most particular cats. The difference between them is most often negligible, and you should use whatever you prefer so long as your page load doesn't appear to be adversely affected. Occasionally you will get a guru tell you to use one option rather than another, because it shaves nanoseconds off the operation, and they will have benchmark tests to prove it. Personally, I think life is too short. I usually use the option that requires less typing, unless I am informed of a convincing reason to use another.
Environment.NewLine has the benefit that it can be used regardless of page language, so I shall use it in future when I answer this question without knowing the language the poster is using. Quite simply, it meets my desire to do less typing.
Carriage Return in Data
would like to place carriage returns in the text to format the data.
For example:
This is<new line>my data. (Where <new line> is the code for a new line.)
Would display as:
This is
my data.
I tried using VBCrLf and Chr(13) & Chr(10), but neither worked.
Thanks,
MikeYou can add them outside if you wish:
insert <tablename> values ('Here is some text' + char(13) + char(10) + 'and
some additional text on a second line')
Rick Sawtell
MCT, MCSD, MCDBA
"Mike" <mbaith@.yahoo.com> wrote in message
news:etOtCp4iEHA.4092@.TK2MSFTNGP10.phx.gbl...
> I am inserting data into a field that is setup as the datatype ntext and
> would like to place carriage returns in the text to format the data.
> For example:
> This is<new line>my data. (Where <new line> is the code for a new line.)
> Would display as:
> This is
> my data.
> I tried using VBCrLf and Chr(13) & Chr(10), but neither worked.
> Thanks,
> Mike
>|||Rick,
I have tried using Chr(13) & Chr(10), but it doesn't work. When outputing it
all appears on the same line.
Thanks,
Mike|||When outputting it where? I ran this in query analyzer.
========================================
CREATE TABLE Frog (col1 ntext)
GO
INSERT Frog VALUES ('Line 1' + CHAR(13) + CHAR(10) + 'Line 2' + CHAR(13) +
CHAR(10) + 'Line 3')
GO
SELECT col1 FROM Frog
col1
----
---Line 1
Line 2
Line 3
(1 row(s) affected)|||Can you post a repro? Below work just fine in my query analyzer:
SELECT 'Hello ' + CHAR(13) + CHAR(10) + 'there!'
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Mike" <mbaith@.yahoo.com> wrote in message news:Oy39A14iEHA.596@.TK2MSFTNGP11.phx.gbl...[vbco
l=seagreen]
> Rick,
> I have tried using Chr(13) & Chr(10), but it doesn't work. When outputing
it
> all appears on the same line.
> Thanks,
> Mike
>[/vbcol]|||Rick,
I was outputting it to a web page, which apparently ignores char 13 & 10...
I used a replace to replace the char 13 & 10 with a <br> and its working. (I
couldn't put the <br> in the data because the webpage isn't the only
application accessing the data.)
Thanks for your help!
Mike
Carriage Return in Data
would like to place carriage returns in the text to format the data.
For example:
This is<new line>my data. (Where <new line> is the code for a new line.)
Would display as:
This is
my data.
I tried using VBCrLf and Chr(13) & Chr(10), but neither worked.
Thanks,
Mike
You can add them outside if you wish:
insert <tablename> values ('Here is some text' + char(13) + char(10) + 'and
some additional text on a second line')
Rick Sawtell
MCT, MCSD, MCDBA
"Mike" <mbaith@.yahoo.com> wrote in message
news:etOtCp4iEHA.4092@.TK2MSFTNGP10.phx.gbl...
> I am inserting data into a field that is setup as the datatype ntext and
> would like to place carriage returns in the text to format the data.
> For example:
> This is<new line>my data. (Where <new line> is the code for a new line.)
> Would display as:
> This is
> my data.
> I tried using VBCrLf and Chr(13) & Chr(10), but neither worked.
> Thanks,
> Mike
>
|||Rick,
I have tried using Chr(13) & Chr(10), but it doesn't work. When outputing it
all appears on the same line.
Thanks,
Mike
|||When outputting it where? I ran this in query analyzer.
========================================
CREATE TABLE Frog (col1 ntext)
GO
INSERT Frog VALUES ('Line 1' + CHAR(13) + CHAR(10) + 'Line 2' + CHAR(13) +
CHAR(10) + 'Line 3')
GO
SELECT col1 FROM Frog
col1
---Line 1
Line 2
Line 3
(1 row(s) affected)
|||Can you post a repro? Below work just fine in my query analyzer:
SELECT 'Hello ' + CHAR(13) + CHAR(10) + 'there!'
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Mike" <mbaith@.yahoo.com> wrote in message news:Oy39A14iEHA.596@.TK2MSFTNGP11.phx.gbl...
> Rick,
> I have tried using Chr(13) & Chr(10), but it doesn't work. When outputing it
> all appears on the same line.
> Thanks,
> Mike
>
|||Rick,
I was outputting it to a web page, which apparently ignores char 13 & 10...
I used a replace to replace the char 13 & 10 with a <br> and its working. (I
couldn't put the <br> in the data because the webpage isn't the only
application accessing the data.)
Thanks for your help!
Mike
Carriage Return in Data
would like to place carriage returns in the text to format the data.
For example:
This is<new line>my data. (Where <new line> is the code for a new line.)
Would display as:
This is
my data.
I tried using VBCrLf and Chr(13) & Chr(10), but neither worked.
Thanks,
MikeYou can add them outside if you wish:
insert <tablename> values ('Here is some text' + char(13) + char(10) + 'and
some additional text on a second line')
Rick Sawtell
MCT, MCSD, MCDBA
"Mike" <mbaith@.yahoo.com> wrote in message
news:etOtCp4iEHA.4092@.TK2MSFTNGP10.phx.gbl...
> I am inserting data into a field that is setup as the datatype ntext and
> would like to place carriage returns in the text to format the data.
> For example:
> This is<new line>my data. (Where <new line> is the code for a new line.)
> Would display as:
> This is
> my data.
> I tried using VBCrLf and Chr(13) & Chr(10), but neither worked.
> Thanks,
> Mike
>|||Rick,
I have tried using Chr(13) & Chr(10), but it doesn't work. When outputing it
all appears on the same line.
Thanks,
Mike|||When outputting it where? I ran this in query analyzer.
========================================
CREATE TABLE Frog (col1 ntext)
GO
INSERT Frog VALUES ('Line 1' + CHAR(13) + CHAR(10) + 'Line 2' + CHAR(13) +
CHAR(10) + 'Line 3')
GO
SELECT col1 FROM Frog
col1
----
---Line 1
Line 2
Line 3
(1 row(s) affected)|||Can you post a repro? Below work just fine in my query analyzer:
SELECT 'Hello ' + CHAR(13) + CHAR(10) + 'there!'
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Mike" <mbaith@.yahoo.com> wrote in message news:Oy39A14iEHA.596@.TK2MSFTNGP11.phx.gbl...
> Rick,
> I have tried using Chr(13) & Chr(10), but it doesn't work. When outputing it
> all appears on the same line.
> Thanks,
> Mike
>|||Rick,
I was outputting it to a web page, which apparently ignores char 13 & 10...
I used a replace to replace the char 13 & 10 with a <br> and its working. (I
couldn't put the <br> in the data because the webpage isn't the only
application accessing the data.)
Thanks for your help!
Mike
Carriage return /New line in a Text field
Is there any way to force a carriage return/new line when inserting some
data into a text field?
I'm updating a text field with data from other fields, but in order to make
it look nice on a printout, I'd like to split it onto two lines. I've looked
in BOL, but haven't really been able to find anything that seems to be able
to do it.
Regards
Steen
Hi Steen,
Please check CHAR in SQL BOL.
I believe CHAR(13) is what you are looking for.
Cheers,
Des
"Steen Persson" <SPE@.REMOVEdatea.dk> wrote in message
news:u7O7RBvmEHA.3988@.tk2msftngp13.phx.gbl...
> Hi
> Is there any way to force a carriage return/new line when inserting some
> data into a text field?
> I'm updating a text field with data from other fields, but in order to
> make
> it look nice on a printout, I'd like to split it onto two lines. I've
> looked
> in BOL, but haven't really been able to find anything that seems to be
> able
> to do it.
> Regards
> Steen
>
|||Yes - that looks like what I was looking for.
Thanks a lot...
Regards
Steen
Des FitzGerald wrote:[vbcol=seagreen]
> Hi Steen,
> Please check CHAR in SQL BOL.
> I believe CHAR(13) is what you are looking for.
> Cheers,
> Des
>
> "Steen Persson" <SPE@.REMOVEdatea.dk> wrote in message
> news:u7O7RBvmEHA.3988@.tk2msftngp13.phx.gbl...
|||You may actually need to use a combination of char(13) +
char(10) depending on your settings. Windows apps
sometimes look for both (Carriage return/linefeed). UNIX
based systems generally only look for the one control
character (Carriage return is a carriage return, line
feed feeds a line but doesn't change your horizontal
position).
Just a thought.
Matthew Bando
Mattehw.Bando@.remove csctgi.com
[vbcol=seagreen]
>--Original Message--
>Yes - that looks like what I was looking for.
>Thanks a lot...
>
>Regards
>Steen
>Des FitzGerald wrote:
when inserting[vbcol=seagreen]
fields, but in order[vbcol=seagreen]
two lines. I've[vbcol=seagreen]
that seems to
>
>.
>
Carriage return /New line in a Text field
Is there any way to force a carriage return/new line when inserting some
data into a text field?
I'm updating a text field with data from other fields, but in order to make
it look nice on a printout, I'd like to split it onto two lines. I've looked
in BOL, but haven't really been able to find anything that seems to be able
to do it.
Regards
SteenHi Steen,
Please check CHAR in SQL BOL.
I believe CHAR(13) is what you are looking for.
Cheers,
Des
"Steen Persson" <SPE@.REMOVEdatea.dk> wrote in message
news:u7O7RBvmEHA.3988@.tk2msftngp13.phx.gbl...
> Hi
> Is there any way to force a carriage return/new line when inserting some
> data into a text field?
> I'm updating a text field with data from other fields, but in order to
> make
> it look nice on a printout, I'd like to split it onto two lines. I've
> looked
> in BOL, but haven't really been able to find anything that seems to be
> able
> to do it.
> Regards
> Steen
>|||Yes - that looks like what I was looking for.
Thanks a lot...
Regards
Steen
Des FitzGerald wrote:
> Hi Steen,
> Please check CHAR in SQL BOL.
> I believe CHAR(13) is what you are looking for.
> Cheers,
> Des
>
> "Steen Persson" <SPE@.REMOVEdatea.dk> wrote in message
> news:u7O7RBvmEHA.3988@.tk2msftngp13.phx.gbl...
>> Hi
>> Is there any way to force a carriage return/new line when inserting
>> some data into a text field?
>> I'm updating a text field with data from other fields, but in order
>> to make
>> it look nice on a printout, I'd like to split it onto two lines. I've
>> looked
>> in BOL, but haven't really been able to find anything that seems to
>> be able
>> to do it.
>> Regards
>> Steen|||You may actually need to use a combination of char(13) +
char(10) depending on your settings. Windows apps
sometimes look for both (Carriage return/linefeed). UNIX
based systems generally only look for the one control
character (Carriage return is a carriage return, line
feed feeds a line but doesn't change your horizontal
position).
Just a thought.
Matthew Bando
Mattehw.Bando@.remove csctgi.com
>--Original Message--
>Yes - that looks like what I was looking for.
>Thanks a lot...
>
>Regards
>Steen
>Des FitzGerald wrote:
>> Hi Steen,
>> Please check CHAR in SQL BOL.
>> I believe CHAR(13) is what you are looking for.
>> Cheers,
>> Des
>>
>> "Steen Persson" <SPE@.REMOVEdatea.dk> wrote in message
>> news:u7O7RBvmEHA.3988@.tk2msftngp13.phx.gbl...
>> Hi
>> Is there any way to force a carriage return/new line
when inserting
>> some data into a text field?
>> I'm updating a text field with data from other
fields, but in order
>> to make
>> it look nice on a printout, I'd like to split it onto
two lines. I've
>> looked
>> in BOL, but haven't really been able to find anything
that seems to
>> be able
>> to do it.
>> Regards
>> Steen
>
>.
>|||Steen Persson wrote:
> *Hi
> Is there any way to force a carriage return/new line when inserting
> some
> data into a text field?
> I'm updating a text field with data from other fields, but in order
> to make
> it look nice on a printout, I'd like to split it onto two lines. I've
> looked
> in BOL, but haven't really been able to find anything that seems to
> be able
> to do it.
> Regards
> Steen *
fmutale02
---
Posted via http://www.webservertalk.com
---
View this thread: http://www.webservertalk.com/message393908.html|||Use a string funtion to select the fisrt part of a string and separate it
with a char(13) or concatenate multiple fileds with the char(13) in between.
Such as
insert into table
select left(field1,10)+char(13)+right(field1,10) from table
or
insert into table
select field1+char(13)+field2
"fmutale02" wrote:
> Steen Persson wrote:
> > *Hi
> >
> > Is there any way to force a carriage return/new line when inserting
> > some
> > data into a text field?
> >
> > I'm updating a text field with data from other fields, but in order
> > to make
> > it look nice on a printout, I'd like to split it onto two lines. I've
> > looked
> > in BOL, but haven't really been able to find anything that seems to
> > be able
> > to do it.
> >
> > Regards
> > Steen *
>
> --
> fmutale02
> ---
> Posted via http://www.webservertalk.com
> ---
> View this thread: http://www.webservertalk.com/message393908.html
>|||Some printers may need both char(13)+char(10) to cause a linefeed.
--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
"Jeff Ericson" <JeffEricson@.discussions.microsoft.com> wrote in message
news:358376A2-847C-4108-B5AD-3E615E8A7BB9@.microsoft.com...
> Use a string funtion to select the fisrt part of a string and separate it
> with a char(13) or concatenate multiple fileds with the char(13) in
> between.
> Such as
> insert into table
> select left(field1,10)+char(13)+right(field1,10) from table
> or
> insert into table
> select field1+char(13)+field2
>
>
> "fmutale02" wrote:
>> Steen Persson wrote:
>> > *Hi
>> >
>> > Is there any way to force a carriage return/new line when inserting
>> > some
>> > data into a text field?
>> >
>> > I'm updating a text field with data from other fields, but in order
>> > to make
>> > it look nice on a printout, I'd like to split it onto two lines. I've
>> > looked
>> > in BOL, but haven't really been able to find anything that seems to
>> > be able
>> > to do it.
>> >
>> > Regards
>> > Steen *
>>
>> --
>> fmutale02
>> ---
>> Posted via http://www.webservertalk.com
>> ---
>> View this thread: http://www.webservertalk.com/message393908.html
>>
Friday, February 24, 2012
Capture Records That Failed Insertion From XML
insert into a table when inserting from an xml document in SQL Svr
2000?
thanks.
-akAyron (akitchen@.lrs.com) writes:
> Does anyone know an effective way to capture any records that failed to
> insert into a table when inserting from an xml document in SQL Svr
> 2000?
It would certainly help if you told us how you insert your records.
Openxml or XML bulk load?
Particularly in the matter case, I would recommend trying
microsoft.public.sqlserver.xml.
--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Thanks for the reply, Erland. I'm new to the XML bulk insert. I'll look
into that method. I've also transitioned the topic to the group that
you mentioned.
-ak|||"Ayron" <akitchen@.lrs.com> wrote in message
news:1108507580.773174.268630@.l41g2000cwc.googlegr oups.com...
> Does anyone know an effective way to capture any records that failed to
> insert into a table when inserting from an xml document in SQL Svr
> 2000?
I have used sqxmlbulkload.
Not really investigated it in depth but the default behaviour is to crash
and burn if a record contains invalid data.
I wrote an access app which a support user investigates problems with.
They import the xml file to a table and a screen iterates through all fields
comparing values with the datatypes.
It marks records as bad.
The screen shows only bad records and uses conditional highlighting to
highlight the bad field in a record.
--
Regards,
Andy O'Neill
Sunday, February 12, 2012
Cant Update, Insert, or Delete rows
I have recently started an ASP.Net application and am having some issues updating, inserting and deleting rows. When I started working with it, I was getting errors because it could not find any update command. Eventually, I figured out how to automatically generate the commands, by configuring my SQLDataSource control and clicking the "advanced" button. Right now though, I have generated the commands, but I still can not insert, update or delete rows. When I attempt to update anything, I recieve an error that says "The data types text and nvarchar are incompatible in the equal to operator." Nowhere in my table do I have any rows that use the datatype "nvarchar", only "text" and "int". I tried switching all of my text columns to "nvarchar(500)", which did not help.
I am led to believe that the auto generated SQL procedures are trying to do something behind the scenes that is making my database act up, because even when I delete rows, I get the same exception, so the datatypes cannot be messed up there, because all that the datasource is doing is deleting rows, therefore there is no need to worry about data types.
I only get the error when I check the "Use optimistic concurrency" box. When I do not use optimistic concurrency, I can delete, insert, and update rows... but nothing happens. There are no errors, but nothing is deleted, updated or inserted either. Upon postback, nothing has changed.
I may upload a copy of the exact exception page, if someone thinks that it may help.
Here is the update command that was generated:
UPDATE [Record Information] SET [Speed] = @.Speed, [Recording Company] = @.Recording_Company, [Year] = @.Year, [Artist] = @.Artist, [Side 1 Track Title] = @.Side_1_Track_Title, [Side 1 Track Duration] = @.Side_1_Track_Duration, [Side 2 Track Title] = @.Side_2_Track_Title, [Side 2 Track Duration] = @.Side_2_Track_Duration, [Sleeve Description] = @.Sleeve_Description WHERE [Record Database ID] = @.original_Record_Database_ID
Apparently no stored procedures exist for any of these operations, and I am unsure why. The "Record Database ID" is my identity column, and is the only field that is (and is supposed to be) uneditable.
What I recommend is that you click the Learn link at the top of the page and go through some of the videos or quickstart tutorials to familiarise yourself with how the SqlDataSource works. It doesn't, for example, generate stored procedures under any circumstances - which is why you can't find any. You also need to understand what Optimistic Concurrency is, how to manage it and when to use it. Finally, you should understand what datatypes are the most appropriate for your data. Having nothing but ints and text datatypes is not very likely to be appropriate.