使用案例陈述来检查日期是否格式正确 [英] using case statment to check that a date is in the right format
本文介绍了使用案例陈述来检查日期是否格式正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个数据质量表。我想使用案例陈述来检查另一个表格中出生日期列中的所有日期是否为(yyyy / mm / dd)格式。如果它是那么我想放一个'n',如果它没有在我的日期
质量表中放'y'。有没有人知道如何在sql代码中执行此操作
感谢legendarycowboy
解决方案
使用try_convert:
DECLARE @tbl TABLE(dob VARCHAR(25));
INSERT INTO @tbl VALUES
('2019/02/01'),
('02 / 01/2019'),
('2019/2 / 1'),
('2/1/2019')
SELECT
dob,
CASE WHY TRY_CONVERT(DATE,dob,111 )IS NULL那么'N'ELSE'Y'END为有效
来自@tbl
I am creating a data quality table. I want to use a case statement to check that all the dates in a date of birth column in another table is in the (yyyy/mm/dd) format. if it is then I want to put a 'n' and if it is not put 'y' in my date quality table. does anyone have any idea how to do this in sql code
thanks legendarycowboy
解决方案
Use try_convert:
DECLARE @tbl TABLE (dob VARCHAR(25)); INSERT INTO @tbl VALUES ('2019/02/01'), ('02/01/2019'), ('2019/2/1'), ('2/1/2019') SELECT dob, CASE WHEN TRY_CONVERT(DATE, dob, 111) IS NULL THEN 'N' ELSE 'Y' END AS Valid FROM @tbl
这篇关于使用案例陈述来检查日期是否格式正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文