Solved

How to check data in sql table

Posted on 2016-09-16
11
51 Views
Last Modified: 2016-09-16
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
0
Comment
Question by:RIAS
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 48

Expert Comment

by:Vitor Montalvão
ID: 41801382
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.
0
 

Author Comment

by:RIAS
ID: 41801388
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.
0
 

Author Comment

by:RIAS
ID: 41801391
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
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 26

Expert Comment

by:Shaun Kline
ID: 41801394
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

0
 
LVL 48

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 41801395
Better way to find it is running the following query:
SELECT *
FROM SNT
WHERE ISNUMERIC(Total)=0

Open in new window

This will return all rows that can't be converted.
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41801396
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.
1
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41801398
Putting a function on top of a column in a where clause will KILL the query performance as will always scan the table.
0
 

Author Closing Comment

by:RIAS
ID: 41801400
Thanks Vitor!
Special thanks to Pawan!
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41801403
@Rias - If my solution helped could you mark my solution as assisted one.? Thanks !
0
 

Author Comment

by:RIAS
ID: 41801407
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
1
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41801412
Fine. Thanks !
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question