Been meaning to post this for a while. It does a very limited job of only allowing [0-9], but could be extended to allow negative numbers, or numeric values that are suitable for numeric types other than INT, but avoiding the pitfalls of IsNumeric() which might allow through data not suitable for some of the numeric datatypes
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[kk_fn_UTIL_IsINT]') AND xtype IN (N'FN', N'IF', N'TF'))
DROP FUNCTION dbo.kk_fn_UTIL_IsINT
GO
CREATE FUNCTION dbo.kk_fn_UTIL_IsINT
(
-- String to be tested - Must only contain [0-9], any spaces are trimmed
@strINTvarchar(8000)
)
RETURNS int-- NULL = Bad INT encountered, else cleanedup INT returned
/* WITH ENCRYPTION */
AS
/*
* kk_fn_UTIL_IsINTCheck that a String is a valid INT
*SELECT dbo.kk_fn_UTIL_IsINT(MyINTColumn)
*IF dbo.kk_fn_UTIL_IsINT(MyINTColumn) IS NULL ... Bad INT
*
* Returns:
*
*int valueValid integer
*NULLBad parameter passed
*
* HISTORY:
*
* 30-Sep-2005 Started
*/
BEGIN
DECLARE@intValueint
SELECT@strINT = LTRIM(RTRIM(@strINT)),
@intValue = CASE WHEN @strINT NOT LIKE '%[^0-9]%'
THEN CONVERT(int, @strINT)
ELSE NULL
END
RETURN @intValue
i am migrating data from a legacy system with a not nice front-end. as a result, i have all sorts of garbage stored on the tables.
i am trying to convert values from varchar(12) to float, but i have an error during selecting data that says that data can not be converted eventhough i am using the ISNUMERIC() function to check.
case when isNumeric( myCol01 ) = 0 then null else convert( float , myCol01 ) end
but my error occours when ISNUMERIC() encounters the value '. ' ; that is a dot with spaces after it.
2/1/2008 1 2/1/2008 1 2/1/2008 0 2/1/2008 x 2/1/2008 0
The grpMiscError can contain 0, 1 or x only. I need to sum up this column for all the zeros by a particular date. I have the following but doesn't work: SELECT SUM(CASE ISNUMERIC(grpMiscError) WHEN 0 THEN 1 ELSE 0 END)AS MiscError FROM TableA WHERE GrpDate = '2/1/2008'
I get back an answer of 1 MiscError instead of 2 What am I doing wrong here?
HiHere's the problem:I need to search a postcode database by the first one or two letters.Problems occur for example when i want to search north London postcodes (N) when using:postcode LIKE @postcode + '%' As this picks up everything beginning with N, eg, NG for Nottingham, or NE for Newcastle. So i need a like statement which searches for the first one or two digits followed by a number!I've found the ISNUMERIC() function but not sure what the best way to use it with the like statement - or even if there is a better way altogether - can you use regular expressions in MSSQL?thanks
sneaky, sneaky, sneaky ISNUMERIC returns 1 when the input expression evaluates to a valid integer, floating point number, money or decimal type; otherwise it returns 0. A return value of 1 guarantees that expression can be converted to one of these numeric types.
thanks, but which one??
numeric as far as float is concerned, is not the same thing as numeric as far as money is concerned
create table isnumerics ( id integer not null identity , txtfld varchar(11) )
insert into isnumerics (txtfld) values ( '1' ) insert into isnumerics (txtfld) values ( '937' ) insert into isnumerics (txtfld) values ( '937.0' ) insert into isnumerics (txtfld) values ( '$937' ) insert into isnumerics (txtfld) values ( '$937.00' ) insert into isnumerics (txtfld) values ( 'free' ) insert into isnumerics (txtfld) values ( '.50' ) insert into isnumerics (txtfld) values ( '1,000' ) insert into isnumerics (txtfld) values ( '' )
select id , txtfld , isnumeric(txtfld) from isnumerics
select id , txtfld , isnumeric(txtfld) , case when isnumeric(txtfld) = 1 then cast(txtfld as money) else cast(null as money) end as case1 from isnumerics
select id , txtfld , isnumeric(txtfld) , case isnumeric(txtfld) when 1 then cast(txtfld as money) else cast(null as money) end as case2 from isnumerics
select id , txtfld , isnumeric(txtfld) , case isnumeric(txtfld) when 1 then cast(txtfld as float) else cast(null as float) end as case2 from isnumerics
1111.0 29371937.0 3937.01937.0
6free0 7.5010.5 the others got "Error converting data type varchar to float"
no, there wasn't a question here, but yes, i'd love to hear your comments
I'm casting a varchar field to a decimal field using the format
CASE ISNUMERIC(GrossMktCapGbp) WHEN 1 THEN CONVERT(DECIMAL(18,6),GrossMktCapGbp) ELSE NULL end
Thinking this would ensure that any spurious rows got set to null.
However I had a problem with some values that were set to '.', it seems that isnumeric thinks these are numbers but casting them to decimal produces an error.
SELECT ISNUMERIC('.') SELECT CAST('.' AS DECIMAL(18,6))
Should I have been doing something different in my check possibly.
The above expression seems to work fine if Fields!Accreditation.Value is a number. However, if Fields!Accreditation.Value is not a number, it gives an #Error. Why is the true part evaluated when the expression is false?
I would like to validate datatype using Derived Column.My data type are such as numeric(X,X),datetime,int, and varchar.How do I do this using Derived Column.Example if row does not qualify as ISNUMERIC()...throw it in ERROR table else send it to SUCCESS table.Any Idea ?
Hello, I have searched the forum, and have discovered that the DTS method using IsNumeric to check for numierc values (ActiveX) is not valid in SSIS. Most of what I have seen prescribes using the script component to handle this.
So formerly, I checked to see if a column was numeric. If it was, then I needed to use the numeric value as is, or in some cases, I needed to perform a calculation on the value and use the result. If the value was not numeric, then whatever the value was needed to be changed to zero.
Here is an example of how I would use the current value, or set the value to zero:
If IsNumeric(DTSSource("Col003")) Then DTSDestination("ADepTrnx") = CLng(DTSSource("Col003")) Else DTSDestination("ADepTrnx") = 0 End If
This is an example of how I would use the current value in a calculation, or set the value to zero:
If IsNumeric(DTSSource("Col012")) Then DTSDestination("AlliStdFee") = CLng(DTSSource("Col012"))/100 Else DTSDestination("AlliStdFee") = 0 End If
Does anyone have an example of how I would handle both situations in a script component?
I've wrote a small query for SQL 2005 and it's doesn't seem to work.
I have a table that contains two columns (X and Y), X is an int and Y is an nvarchar(50). I've populated this table with some data where Y contains numbers and some strings (e.g. "1", "2", "foo", etc). I've then got a view which only returns the rows where Y is numeric - now, I then query this table stating I only want numbers greater than 0 (i've casted the column) but this throws an error stating "foo" can't be casted. This is strange because the view doesn't return that.
What's going on? All of this works fine in SQL 2000 but not in SQL 2005 - looks like it's looking at the underlying table rather than the view. Sample code below to help you all out: -
Create Table ============ CREATE TABLE [dbo].[tblTest]( [X] [int] NOT NULL, [nvarchar](50) NOT NULL ) ON [PRIMARY]
Insert Data =========== INSERT INTO tblTest(X, Y) VALUES(1, '1') INSERT INTO tblTest(X, Y) VALUES(1, '2') INSERT INTO tblTest(X, Y) VALUES(2, 'foo') INSERT INTO tblTest(X, Y) VALUES(2, 'bar')
Create View =========== CREATE VIEW [dbo].[vwTest] AS SELECT X, Y FROM dbo.tblTest WHERE (ISNUMERIC(Y) = 1)
Finally ======= SELECT X, Y FROM dbo.vwTest WHERE (CONVERT(int, Y) >= 0)
Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the nvarchar value 'foo' to data type int.
I have a task (Derived Column Task) and I want to write something like this :
IsNumeric(aColumnOfString) == true ? "All numbers" : "there are some characters"
Here aColumnOfString can be something like "123a5" or 12345". I do not want to simply check if the left-most character is a number or not. I want to check the entire expression and return me a TRUE or false.
A TRUE is returned if the entire expression contains ONLY numbers, and FALSE otherwise.
I read some posting using regular expression. But that is not a solution for this situation.
Hi - Please excuse me if this is really simple, but I'm fairly new to this lark.
My (made up) code is below... I'd be grateful for any pointers.
insert into [tblInvoices] (full_period, supplier_no, account_code, tran_amount, function) select full_period, supplier_no, account_code, tran_amount case when substring(account_code,1,2) = 'FY' then '-' when isNumeric(account_code) then left(account_code, 2) when not isNumeric(substring(account_code,1,2)) then left(account_code, 1) else 'oops' end as function, from tblLoadMSV900_i end
Is this even close?
I'm using a stored proc to insert the data from tblLoadMSV900_i into tblInvoices and at the same time insert some data into the function field.
In plain english I want make sure that: If the first 2 chars of account_code are 'FY' then function='-', If the first char of account_code is numeric then function=left(account_code, 2), If the the first char is not numeric (and if first two chars are not 'FY' i.e. first char could be 'F') then function=left(account_code, 1)
And there's plenty more where this came from! But if I can crack this with your help then I should have a better idea about the rest of the proc.
There is a MSSQL function that check the value. Like ISNULL(), ISDATE() and ISNUMERIC(). I don't see a function that check for decimal. If there isn't any then is there an user-defined function for it? I need to be able to validate the string value for decimal before it get assigned to a decimal datatype or T-SQL will run into an error.
SELECT uri, evFieldUri, evFieldVal , CAST(evFieldVal AS BIGINT) FROM TSEXFIELDV
[Code] ....
It Returns:
uri          evFieldUri          evFieldVal          (No column name) 224016 3267      +000089243829 89243829 224019 2717      +000089243825 89243825 224472 3333      +000000000000000000000017    17 225052 3267      +000089243829 89243829 225055 2717      +000089243825 89243825
So, then I went back to:
SELECT uri, evFieldUri, evFieldVal , CAST(evFieldVal AS BIGINT) FROM TSEXFIELDV
[Code] ....
And it returns this error:
Msg 8114, Level 16, State 5, Line 1 Error converting data type varchar to bigint.
So, I tried again, and this worked…
SELECT uri, evFieldUri, evFieldVal,CAST(evFieldVal AS BIGINT), ISNUMERIC(evFieldVal) FROM TSEXFIELDV WHERE URI > 0 AND evFieldUri IN ( SELECT URI FROM TSEXFIELD WHERE exFieldFormat IN (1,11))
I logged out and came back and tried again, and it still worked. So then I tried…
SELECT uri, evFieldUri, evFieldVal,CAST(evFieldVal AS BIGINT) FROM TSEXFIELDV WHERE URI > 0
I've got a SQL Server hardware Replacement to do. This server has a merge replication setup on it which was setup by a contractor.
Do i have to do the configurations manually when moving the replication bits across two servers or is there a different process involved ? I am just not sure how to approach this.
Hello all, I get some dates entered manually in french format in a varchar(255) field and want to convert that field in datetime. Since, Enterprise manager is warning me about a risk to loose information, I'm thinking about a way to update the french text in the corresponding english text before doing the conversion. Example: 25-fév-08 (varchar(255)) has to become 25-feb-08 (datetime)
I have a job that we wrote in SQL 2000 that loops through and finds all the orders pending verification and distributes them via emal to 100+ sales people every night. Each email is sent to the individual sales rep. I did this using sp_makewebtask and attaching the html file to an email and using a WHILE statement to loop thorugh all pending entries in sql.
I recently learned that sp_makewebtask is not going to be around for the next release and that I should use RSS for all future development.
Question: How can I achieve the same result outlined above using RS? How can I dynamically create and distribute HTML files?
I know that RS is xml based, but some pointer on how/where to start would be much appreciated.
Dear all,I need some help from all my Transact SQL Guru friends out there..Here is the scenario in its most simplified form.. ..I have two tables.. A(Lookup table) and B(Transaction Table)TableA FieldsEmployeeLocationIDEmployeeLocation (This could have values say"B","BO","BOM","C","CA","CALC") etc...TableB FieldsEmployeeIDEmployeeName.......EmployeeLocationID (will have null initially when rows are populatedfirst time)EmployeeLocation (This could have values"BA123","BOMBAY","BOTS123","BRACK".... etc)I hope you get where I am leading this to, from my examples..Requirement is to populate the EmployeeLocationID in Table B withEmployeeLocationID from TableA by matching the field EmployeeLocationin both tables.Please note that table B's EmployeeLocation could be A'sEmployeeLocation + some additionalcodes like "123","RACK" etc in theabove example...Therefore, this is what I had wrote initially..update Bset B.EmployeeLocationID =A.EmployeeLocationID
Help, I have a vendor file with the vendors name. I need to add afield that has alternate captionseg. :In my vendor file I have records for AT&T with three differentnames/spellings for AT&T.AT&TLucent TechnologiesA T & TI want to add a new field that would have AT&T for the above records.I know I could hardcode a queryUpdate F_VENDORSet VENDOR_CAPTION_ALT = 'AT&T'where VENDOR_CAPTION like '%Lucent%' orVENDOR_CAPTION like '%A T & T%'however, how can I use another table that has the my search keywordsfor the "like" and the replacement captions so I dont need literalqueries to update all the diffent vendors?I have identified 96 instances of where I want to have an alternativename that I can use for a type of grouping. This gets really wildwith the military such as Army and Navy.TIARob
In the BOL, it states to replace DB_ID() with a valid database name when the compatibility level is 80 or below.
The original statement is:
SELECT object_id AS objectid, index_id AS indexid, partition_number AS partitionnum, avg_fragmentation_in_percent AS frag INTO #work_to_do FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, 'LIMITED') WHERE avg_fragmentation_in_percent > 10.0 AND index_id > 0;
In VS 2003 I used SQLDMO (Com Object) to list all available SQL Servers. Is in SQL Server 2005 a managed .net Component that can do that task?Thanks,Rainer.
shailendra writes "Hi,everbody lets come to my problem.I have one variable of type "ntext" which contain character "". i want to remove it or replace it with a blank character.
Hi,It seems to be simple, however, it stumbles me.how to replace all the double quotes (") within the followingsentence (or a column) with single quotes ('),colA = this is a freaking "silly" thing to dointocolA this is a freaking 'silly' thing to doSelect Replace(colA,'"',''')[color=blue]>From tblXYZ[/color]won't work,Select Replace(colA,'"',"'")[color=blue]>From tblXYZ[/color]won't work neither.How come? Thanks.
Just wondering if anyone has any suggestions for a replacement for Access Forms once I move the tables etc to SQL 2005? Does SQL 2005 have any form building functionality like Access?