473,320 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

select cast error

name1 field is is nvarchar(40).

(1)select case when isnumeric(name1) = 1 then
cast(name1 as int) else null end as name

In (1) when name1 is not numeric, name is null and
its type becomes number not string(40).

And,

(2)select case when isnumeric(name1) = 1 then
cast(name1 as int) else name1 end as name

In (2) when name1 is Not numeric it gives error:
can not cast name to int.

Basically, i like to convert name1 to Integer if it is
numeric or keep it in its origianl nvarchar(40) if it
is Not numeric. how?.

--
Sent by 3 from yahoo element from com
This is a spam protected message. Please answer with reference header.
Posted via http://www.usenet-replayer.com
Jul 20 '05 #1
2 8894
case when name1 is null then ....

when a column is null, it has no contents/value , so you'll have to evaluate
null !

jobi
"al********@yahoo.com" <u1********@spawnkill.ip-mobilphone.net> wrote in
message news:l.*********************@[63.127.215.130]...
name1 field is is nvarchar(40).

(1)select case when isnumeric(name1) = 1 then
cast(name1 as int) else null end as name

In (1) when name1 is not numeric, name is null and
its type becomes number not string(40).

And,

(2)select case when isnumeric(name1) = 1 then
cast(name1 as int) else name1 end as name

In (2) when name1 is Not numeric it gives error:
can not cast name to int.

Basically, i like to convert name1 to Integer if it is
numeric or keep it in its origianl nvarchar(40) if it
is Not numeric. how?.

--
Sent by 3 from yahoo element from com
This is a spam protected message. Please answer with reference header.
Posted via http://www.usenet-replayer.com

Jul 20 '05 #2
al********@yahoo.com (u1********@spawnkill.ip-mobilphone.net) writes:
name1 field is is nvarchar(40).

(1)select case when isnumeric(name1) = 1 then
cast(name1 as int) else null end as name

In (1) when name1 is not numeric, name is null and
its type becomes number not string(40).
Note that isnumeric may return 1, and yet the cast may fail. This
would happen if name has a value like '$5' or '3.34'. isnumeric()
is basically a useless function, because you cannot find out to
which numeric types the string is converible to.

To test for integer value, you can try:

name1 NOT LIKE '%[^0-9]%'

although neither this is fool-proof. This experssion does not handle
signed numbers, and but will accept numbers which does not fit an int.
(2)select case when isnumeric(name1) = 1 then
cast(name1 as int) else name1 end as name

In (2) when name1 is Not numeric it gives error:
can not cast name to int.
Yes, the result of a CASE expression is always the same type, and the
type is defined by the first type of the expression of the first WHEN
branch. All other WHEN branches are converted to that type.
Basically, i like to convert name1 to Integer if it is
numeric or keep it in its origianl nvarchar(40) if it
is Not numeric. how?.


I'm afraid that this request does not make that much sense. All values
in a column must have the same type.

However, there is the type sql_variant. If you use

cast(cast(name1 as int) as sql_variant)

all values will be of the type sql_variant, but as a property retain the
original type.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Robin | last post by:
In an asp.net page that uses a cast function to convert a date, the code works in a development environment but in production it displays the following error. Cast from string "31/03/2005" to...
1
by: .Net Sports | last post by:
I have a datagrid codebehind script that takes data from sql dbase and displays it in a footer row as a total. One column has amount_dollars (which works fine), while another has new sales (which...
4
by: T.M. | last post by:
What is the correct syntax for querying the first 20 characters of a memo field? I'm trying to SELECT CAST (varchar(20) fieldname) as newfieldname and i get a bunch of errors Can someone...
2
by: T.M. | last post by:
What is the correct syntax for querying the first 20 characters of a memo field? I'm trying to SELECT CAST (varchar(20) fieldname) as newfieldname and i get a bunch of errors Can someone...
2
by: Adam J Knight | last post by:
Hi all, I am gettting the following error with this line of code: DbDataRecord test = (DbDataRecord)e.Item.DataItem; System.InvalidCastException: Specified cast is not valid. It is...
4
by: Martin | last post by:
using VS2005 - VB.Net I noticed unexpected behaviour when using a combobox which has it's datasource set to a datatable. Also the Valuemember and Displaymember are being set. I'm getting a cast...
0
by: kcm | last post by:
Hello all, Not sure if this is the right place to post this. I ran up the samples for adventureworks for MSSQL2005. I setup the shop front. The web site is up. But when I try to register a new...
0
by: buccsailor | last post by:
Hell there, I tried to post a reply to a message thread created back in July 2006 regarding the override of WebRequest, Closed Conenctions and setting KeepAlive to false,but it's been over 60...
1
by: Rahul | last post by:
I am getting following error: 1) For a xml file "Request.xml" we created a schema "Request.xsd". 2) With the help of xsd.exe we got the C# file Request.cs. 3) We tried to send the object of...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.