Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Data type validations


Data type validations

Author
Message
shravan.gavva
shravan.gavva
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 7
Hi,

I have two columns in the same table
One is data_value (varchar) and the other is sql_data_type (varchar). Data_value holds info like dates, decimals, etc..
sql_data_type holds info about what type of data should be stored in data_value. How can i write a code so that specific_data_type goes into data_value as mentioned in sql_data_type???


Thank you
gs
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16580 Visits: 17024
shravan.gavva (11/20/2013)
Hi,

I have two columns in the same table
One is data_value (varchar) and the other is sql_data_type (varchar). Data_value holds info like dates, decimals, etc..
sql_data_type holds info about what type of data should be stored in data_value. How can i write a code so that specific_data_type goes into data_value as mentioned in sql_data_type???


Thank you
gs


There is one and only one right way to deal with this. Drop the idea of using varchar to hold all kinds of datatypes. Use datetime and decimal datatypes to hold that kind of data.

Something tells me you are working on creating an EAV system?

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
shravan.gavva
shravan.gavva
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 7
I could not change the datatype of data_value
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8550 Visits: 18142
As Sean pointed out, this is a really bad idea. However, if you need help, please post DDL, sample data in the form of INSERT statements, expected results and what you have tried so far. This will give us a better idea of what you need and an environment on which we can work.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
shravan.gavva
shravan.gavva
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 7
create table table1
(
data_value varchar (50),
sql_data_type varchar (50)
)

insert into table1(data_value,sql_data_type)
values ('11/02/2013', 'DATE')

insert into table1(data_value,sql_data_type)
values ('', 'DATE')

insert into table1(data_value,sql_data_type)
values (11.3, 'decimal')

insert into table1(data_value,sql_data_type)
values ('not assigned', 'DATE')


insert into table1(data_value,sql_data_type)
values (null, 'varchar')

insert into table1(data_value,sql_data_type)
values ($, 'varchar')


From these information i need to check wether data_value follow the sql_data_type shown data type.If not they should be moved to another table.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16580 Visits: 17024
shravan.gavva (11/20/2013)
I could not change the datatype of data_value


Then you are going to have a long uphill battle validating all your datatypes. This is truly a nightmare.

Are you planning on validating these values one column at a time? What about processing multiple rows at a time?

Here is an example of just what an absolute nightmare this type of thing is. This will attempt to cast 2 rows in a table like into their "appropriate" datatypes. It requires things like dynamic sql and some trickery to avoid cursors.


create table #nightmare
(
data_value nvarchar(100),
sql_data_type varchar(100)
)

insert #nightmare
select '1900-01-01', 'datetime' union all
select '123.45', 'decimal(5,2)'

declare @sql nvarchar(max) = ''

;with sqlData as
(
select distinct stuff((
select 'cast(''' + data_value + ''' as ' + sql_data_type + '), '
from #nightmare
for xml path('')), 1, 0 , '')
as MyValues
from #nightmare
)

select @sql = LEFT(MyValues, len(MyValues) - 1)
from sqlData

set @sql = 'select ' + @sql
exec sp_executesql @sql
drop table #nightmare



Now the next questions is...how do you validate the sql_data_type column? The short answer, you can't. w00t If you have an invalid datatype, this will come to a grinding halt. Even worse is you can't use foreign keys to sys.systypes because you will have to specify the length, scale and precision depending on the datatype.

In short, run away from the type of architecture as fast as you possibly can. It is nothing but painful to work with and will prove to horribly slow once you start adding any amount of data to the system.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16580 Visits: 17024
shravan.gavva (11/20/2013)
create table table1
(
data_value varchar (50),
sql_data_type varchar (50)
)

insert into table1(data_value,sql_data_type)
values ('11/02/2013', 'DATE')

insert into table1(data_value,sql_data_type)
values ('', 'DATE')

insert into table1(data_value,sql_data_type)
values (11.3, 'decimal')

insert into table1(data_value,sql_data_type)
values ('not assigned', 'DATE')


insert into table1(data_value,sql_data_type)
values (null, 'varchar')

insert into table1(data_value,sql_data_type)
values ($, 'varchar')


From these information i need to check wether data_value follow the sql_data_type shown data type.If not they should be moved to another table.


UGH. With what you have here you are almost forced to use a loop/cursor to read this row by agonizing row with a try/catch block inside each iteration.

Let's consider a couple other real world issues with dates. You have '11/02/2013'. This is fine and will be able to be cast a date with most settings. But what about if you dmy instead of mdy. '13/02/2013' is perfectly valid IF your dateformat is dmy but fails if you dateformat is mdy.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
shravan.gavva
shravan.gavva
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 7
I tried to use try-catch

it says i have a syntax error...doesnt matter how many times I checked..

I have hardly 129 datatypes. I checked it has in the format of mm/dd/yyyy




The first problem is the database is around 5-8 years old even the methods used to create...i am a fresher here and am totally confused by their methods...

still...
Thanks for the help guys
shravan.gavva
shravan.gavva
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 7
shravan.gavva (11/20/2013)
I tried to use try-catch

it says i have a syntax error...doesnt matter how many times I checked..

I have hardly 129 rows. I checked it has in the format of mm/dd/yyyy




The first problem is the database is around 5-8 years old even the methods used to create...i am a fresher here and am totally confused by their methods...

still...
Thanks for the help guys


Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16580 Visits: 17024
shravan.gavva (11/20/2013)
I tried to use try-catch

it says i have a syntax error...doesnt matter how many times I checked..



Can you post what you tried?


I have hardly 129 datatypes.


YIKES!!! 129 datatypes????


The first problem is the database is around 5-8 years old even the methods used to create...i am a fresher here and am totally confused by their methods...

still...
Thanks for the help guys


I don't blame you for being confused by these methods. They are truly awful. We can certainly try to help you get your bearing a little bit though.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search