How to validate user if input formate no yyyy-MM-dd for created date ?

ahmed salah 3,216 Reputation points
2022-05-10T23:34:38.7+00:00

I work on sql server 2017 i need to validate user input of temp table
if formate of created date not YYYY-MM-DD then reject by update status field of temp table to be Notvalid
and if formate of created date is YYYY-MM-DD then update status by Valid

 CREATE TABLE #TempPC
            (
              [ID] INT IDENTITY ,
              CreatedDate varchar(12),
              status varchar(200)
            )


        insert into #TempPC 
( 
        CreatedDate
 ) 

select '2022-05-09'
union
select '12-04-2022'

Expected result

CreatedDate   Status
2022-05-09     Valid
12-04-2022     NotValid
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,321 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,597 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jingyang Li 5,891 Reputation points
    2022-05-11T00:42:28.567+00:00
    CREATE TABLE #TempPC
                 (
                   [ID] INT IDENTITY ,
                   CreatedDate varchar(12),
                   status varchar(200)
                 )
    
    
             insert into #TempPC  ( CreatedDate ) 
    
     select '2022-05-09'
     union all
     select '12-15-2022'
      union all
     select '13-04-2022'
    
    
     Select CreatedDate
     ,Case when try_Convert(date,CreatedDate,120) is null then 'NotValid' else  'Valid' end Status
     from #TempPC
    
     drop TABLE #TempPC
    
    0 comments No comments

0 additional answers

Sort by: Most helpful