sql server - How to check whether (var)char is a valid ISO date (112) -
sql server - How to check whether (var)char is a valid ISO date (112) -
i have char(8) field should contain value in yyyymmdd dateformat. given (hypothetical) table id(int)|datestring(char(8)) able
select id, isvaliddate(datestring) my_hypothetical_table it's of import me can run query (so could, example, select * othertable inner bring together hypothetical_table on hypothetical_table.id = othertable.hypothetical_fk isvaliddate(hypothetical_table.datestring) = 1). catching errors doesn't seem viable.
note isdate() function works slash delimited dates, , not yyyymmdd formats.
you may want add together check length if unsure values beingness 8 digits in length.
declare @mytable table (id int, datestring char(8)) insert @mytable values (1, '20110711') insert @mytable values (2, '2011') insert @mytable values (3, '20110228') insert @mytable values (4, '20110229') insert @mytable values (5, '2011071') insert @mytable values (6, '201107') select id, datestring, isdate(datestring) isdate @mytable running produces output:
id datestring isdate ----------- ---------- ----------- 1 20110711 1 2 2011 1 3 20110228 1 4 20110229 0 5 2011071 0 6 201107 1 sql-server tsql datetime
Comments
Post a Comment