Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Data type validations Expand / Collapse
Author
Message
Posted Wednesday, November 20, 2013 12:52 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, November 21, 2013 9:01 AM
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
Post #1516179
Posted Wednesday, November 20, 2013 1:58 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:32 PM
Points: 13,455, Visits: 12,318
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)
Post #1516194
Posted Wednesday, November 20, 2013 2:04 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, November 21, 2013 9:01 AM
Points: 8, Visits: 7
I could not change the datatype of data_value
Post #1516199
Posted Wednesday, November 20, 2013 2:16 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 7:21 PM
Points: 3,645, Visits: 7,961
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1516206
Posted Wednesday, November 20, 2013 2:31 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, November 21, 2013 9:01 AM
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.
Post #1516217
Posted Wednesday, November 20, 2013 2:32 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:32 PM
Points: 13,455, Visits: 12,318
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. 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)
Post #1516218
Posted Wednesday, November 20, 2013 2:36 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:32 PM
Points: 13,455, Visits: 12,318
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)
Post #1516220
Posted Wednesday, November 20, 2013 2:43 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, November 21, 2013 9:01 AM
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

Post #1516223
Posted Wednesday, November 20, 2013 2:44 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, November 21, 2013 9:01 AM
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

Post #1516225
Posted Wednesday, November 20, 2013 2:52 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:32 PM
Points: 13,455, Visits: 12,318
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)
Post #1516228
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse