Data type validations

  • 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

  • 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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • I could not change the datatype of data_value

  • 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
  • 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.

  • 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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • 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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • 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 (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

  • 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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • 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

    As others have said, this is a nasty kludge that needs reworking, but I applaud your desire to at least constrain the values to being valid 😀

    This may point you in the right direction...

    create table table1

    (

    data_value varchar (50) ,

    sql_data_type varchar (50),

    check (

    case sql_data_type

    when 'DATE' then cast(TRY_PARSE(data_value as date) as sql_variant)

    when 'decimal' then cast(try_parse(data_value as decimal) as sql_variant)

    else data_value

    end is not null

    ),

    check (

    TYPE_ID(sql_data_type) is not null

    )

    )

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

  • TRY_PARSE is the first place my mind went as well. A long-overdue feature of T-SQL. I rolled my own for 2008 using SQLCLR which I use within check constraints to keep the SSIS configurations table tidy. TRY_PARSE simply exposes the static TryParse methods on each of the .NET data type classes.

    I would add one simple refinement. If NULL is unacceptable as a data_value then purge all rows WHERE data_value is NULL and alter the column to be NOT NULL, else, tolerate NULL in the check constraint:

    check (

    case sql_data_type

    when 'DATE' then cast(TRY_PARSE(data_value as date) as sql_variant)

    when 'decimal' then cast(try_parse(data_value as decimal) as sql_variant)

    else data_value

    end is not NULL

    OR data_value is null -- << added to MM's check

    )

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • THANKS GUYS ...I GOT IT

  • thank you guys...

    i finally did it..

    I used isdate to validate date datatypes

    and isnumeric for others

    i used the same syntax as that of the parse statement.....

    Thank you

    once again

  • Beware of ISNUMERIC(), you might have some problems if you don't consider all the possibilities.

    http://www.sqlservercentral.com/articles/IsNumeric/71512/

    If you're using SQL Server 2012 or 2014, go for TRY_PARSE,TRY_CONVERT or TRY_CAST.

    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
  • Viewing 15 posts - 1 through 15 (of 16 total)

    You must be logged in to reply to this topic. Login to reply