Avatar of RIAS
RIASFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

How to check data in sql table

Hello,

I am trying to convert a datatype of an column in sql table to money .But it fails due to some data which cannot be converted to money.How can  I check in the table which data is causing the failure.

Cheers
Microsoft SQL ServerMicrosoft SQL Server 2005Microsoft SQL Server 2008

Avatar of undefined
Last Comment
Pawan Kumar
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

It will help you if you provide us the table schema and the error message.
Also the code will be good to have so we can check it for you.
Avatar of RIAS
RIAS
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

I am using alter command to change the datatype to money .

ALTER TABLE [SNT]
                   ALTER COLUMN [Total]  money NULL

Error:
Msg 235, Level 16, State 0, Line 1
Cannot convert a char value to money. The char value has incorrect syntax.
The statement has been terminated.
Avatar of RIAS
RIAS
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Just wanted to know which data is causing the failure.

Data is like:

606,759.71
79,040.00
9,922.98
350,680.23
16,997.84
295,522.10
50,000.00
60,000.00
9, 751.51
Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

Switching from CHAR to MONEY will be problematic because CHAR fields will end with added spaces so that all values have the specified character length.

Beyond that, you can query to find which values have invalid conversion values using the ISNUMERIC() function:
SELECT *
FROM SNT
WHERE ISNUMERIC(Total) <> 1

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Please use Try_Cast or Try_Convert or try_parse functions

TRY_PARSE
 
It converts string data type to target data type(Date or Numeric). For example, source data is string type and we need to covert to date type. If conversion attempt fails it returns NULL value.
 
Syntax: TRY_PARSE (string_value AS data_type [ USING culture ])
String_value – This is argument is source value which is NVARCHAR(4000) type.
Data_type – This argument is target data type either date or numeric.
Culture – It is an optional argument which helps to convert the value to in Culture format. Suppose you want to display the date in French, then you need to pass culture type as ‘Fr-FR’. If you will not pass any valid culture name, then PARSE will raise an error.
Examples
DECLARE @ fakeDate AS varchar(10);  
DECLARE @ realDate AS VARCHAR(10);  
SET @fakeDate = 'iamnotadate';  
SET @realDate = '13/09/2015;  
SELECT TRY_PARSE(@fakeDate AS DATE); --NULL  
SELECT TRY_PARSE(@realDate AS DATE); -- 2015-09-13  
SELECT TRY_PARSE(@realDate AS DATE USING 'Fr-FR'); -- 2015-09-13  
First query attempts to convert a non-date to date type, it fails and returns NULL value. Second query successfully converts to date type. Third query also successfully converts but in French forma t(Here it is not showing any difference you can use other culture to see the difference).

TRY_CONVERT

It converts value to specified data type and if conversion fails it returns NULL. For example, source value in string format and we need date/integer format. Then this will help us to achieve the same.

Syntax: TRY_CONVERT ( data_type [ ( length ) ], expression [, style ] )
Data_type - The datatype into which to convert. Here length is an optional parameter which helps to get result in specified length.
Expression - The value to be convert
Style - It is an optional parameter which determines formatting. Suppose you want date format like “May, 18 2013” then you need pass style as 111. More on style visit here.
Examples:
DECLARE @sampletext AS VARCHAR(10);  
SET @sampletext = '123456';  
DECLARE @ realDate AS VARCHAR(10);  
SET @realDate = '13/09/2015’;  
SELECT TRY_CONVERT(INT, @sampletext); -- 123456  
SELECT TRY_CONVERT(DATETIME, @sampletext); -- NULL  
SELECT TRY_CONVERT(DATETIME, @realDate, 111); -- Sep, 13 2015  
First query converts the text to integer successfully, but in second query the conversion fails and it returns NULL. Third query successfully converts string value to date type with specified formats.

TRY_CAST

It converts value to specified data type and if conversion fails it returns NULL. For example, source value in string format and we need it in double/integer format. Then this will help us in achieving it.
 
Syntax: TRY_CAST ( expression AS data_type [ ( length ) ] )
Expression - The source value which will go to cast.
Data_type - The target data type the source value will cast.
Length - It is an optional parameter that specifies the length of target data type.
Examples
DECLARE @sampletext AS VARCHAR(10);  
SET @sampletext = '123456';  
 
SELECT TRY_CAST(@sampletext AS INT); -- 123456  
SELECT TRY_CAST(@sampletext AS DATE); -- NULL  
Firstly, query converts the text to integer successfully, but in second query the conversion fails and it returns NULL.
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Putting a function on top of a column in a where clause will KILL the query performance as will always scan the table.
Avatar of RIAS
RIAS
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Thanks Vitor!
Special thanks to Pawan!
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

@Rias - If my solution helped could you mark my solution as assisted one.? Thanks !
Avatar of RIAS
RIAS
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Pawan,

Thanks.Your suggestion was to how to convert datatype.
But,my question was How can  I check in the table which data is causing the failure.

Cheers
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Fine. Thanks !
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo