SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Data type validations


Data type validations

Author
Message
mister.magoo
mister.magoo
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4124 Visits: 7865
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 :-D

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
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • Orlando Colamatteo
    Orlando Colamatteo
    SSChampion
    SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

    Group: General Forum Members
    Points: 14951 Visits: 14396
    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
    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
    THANKS GUYS ...I GOT IT
    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
    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
    Luis Cazares
    Luis Cazares
    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: 16555 Visits: 19098
    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
    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 cannot use parse..

    Using Sql 2008..

    Thanks though it was really great info...
    Orlando Colamatteo
    Orlando Colamatteo
    SSChampion
    SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

    Group: General Forum Members
    Points: 14951 Visits: 14396
    Note that ISDATE() has tons of odd interpretations as well.

    For example, '2013' returns as a valid date:

    SELECT  ISDATE('2013')



    Which makes sense if you consider this returns 2013-01-01:

    SELECT  CAST('2013' AS DATE)



    However, do you think of '2013' by itself as a valid date?

    Meanwhile, TRY_PARSE says '2013' is not a valid date:

    SELECT TRY_PARSE('2013' AS DATE)



    but of course 2013-01-01 is deemed valid:

    SELECT TRY_PARSE('2013-01-01' AS DATE)



    So, choose your validation techniques wisely and do not assume the name of a function matches the behavior that might be implied by your interpretation of that name.

    __________________________________________________________________________________________________
    There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
    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