我一直在努力弄清楚如何使用PostgreSQL在WHERE子句中放置CASE表达式。我需要将字符串转换为日期(如第3行所示)。这工作得很好。当我试图在WHERE子句中提取CURRENT_DATE时,我遇到了错误。这是最好的方法吗?任何建议都是非常欢迎的。
SELECT
CASE WHEN
multi_app_documentation.nsma1_code = 'DATE' THEN TO_DATE(multi_app_documentation.nsma1_ans, 'MMDDYYYY') END AS "Procedure Date",
' ' AS "Case Confirmation Number",
ip_visit_1.ipv1_firstname AS "Patient First",
ip_visit_1.ipv1_lastname AS "Patient Last",
visit.visit_sex AS "Patient Gender",
TO_CHAR(visit.visit_date_of_birth, 'MM/DD/YYYY') AS "DOB",
visit.visit_id AS "Account Number",
visit.visit_mr_num AS "MRN",
' ' AS "Module",
' ' AS "Signed off DT",
CASE WHEN
multi_app_documentation.nsma1_code = 'CRNA' THEN multi_app_documentation.nsma1_ans END AS "Primary CRNA",
' ' AS "Secondary CRNA",
' ' AS "Primary Anesthesiologist",
' ' AS "Secondary Anesthesiologist",
' ' AS "Canceled Yes/No"
FROM
multi_app_documentation
INNER JOIN ip_visit_1 ON multi_app_documentation.nsma1_patnum = ip_visit_1.ipv1_num
INNER JOIN visit ON ip_visit_1.ipv1_num = visit.visit_id
WHERE
CASE
WHEN ( multi_app_documentation.nsma1_code = 'DATE' AND TO_DATE( multi_app_documentation.nsma1_ans, 'MMDDYYYY' ) = CURRENT_DATE END )
AND multi_app_documentation.nsma1_ans IS NOT NULL
ORDER BY
ip_visit_1.ipv1_lastname ASC
1条答案
按热度按时间lymnna711#
你的圆括号不对称。而且,你甚至不需要在
WHERE
子句中使用CASE
表达式。使用这个版本:此外,不希望在
nsma1_ans
列中存储文本日期,最好使用真正的日期列。如果您必须将日期存储为文本,那么至少使用YYYY-MM-DD
,然后可以使用nsma1_ans::date
将其转换为日期。