You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Databricks中CAST函数转换VARCHAR类型日期列返回NULL值的解决求助

Fix CAST Returning NULL When Converting VARCHAR to DATE in Azure Databricks

It sounds like your month column's string format doesn’t align with Databricks’ default date parsing rules, which is why CAST(month AS DATE) is returning NULL. Let’s break down how to fix this and get your dates into the yyyy-MM-dd format you need.

First, Identify Your month Column’s Actual Format

Since you didn’t share the exact string values in your month column, start by checking what formats you’re working with:

SELECT DISTINCT month FROM date;

Common year-month string formats include MM/yyyy (e.g., 05/2023), yyyyMM (e.g., 202305), or MM-yyyy (e.g., 05-2023).

Use to_date() Instead of CAST for Custom Formats

CAST relies on Databricks’ default date format (usually yyyy-MM-dd or yyyyMMdd), so it fails for non-standard year-month strings. The to_date() function lets you specify the exact format of your input string, which is far more flexible.

Here are examples for common formats:

  • If your month values are like MM/yyyy:

    SELECT DISTINCT to_date(month, 'MM/yyyy') AS formatted_date FROM date;
    

    This converts 05/2023 to 2023-05-01 (Databricks automatically uses the first day of the month when no day is specified).

  • If your month values are like yyyyMM:

    SELECT DISTINCT to_date(month, 'yyyyMM') AS formatted_date FROM date;
    
  • If your month values are like MM-yyyy:

    SELECT DISTINCT to_date(month, 'MM-yyyy') AS formatted_date FROM date;
    

Handle Mixed Formats (If Applicable)

If your month column has multiple different string formats, use coalesce() to try parsing with multiple formats until a match is found:

SELECT DISTINCT
  coalesce(
    to_date(month, 'MM/yyyy'),
    to_date(month, 'yyyyMM'),
    to_date(month, 'MM-yyyy')
  ) AS formatted_date
FROM date;

This returns the first valid date conversion, avoiding NULLs for values that match any of the specified formats.

Verify the Output Format

The to_date() function returns a native DATE type, which Databricks displays in yyyy-MM-dd format by default. If you need to convert it to a string in this format explicitly, use date_format():

SELECT DISTINCT
  date_format(to_date(month, 'MM/yyyy'), 'yyyy-MM-dd') AS formatted_date_str
FROM date;

内容的提问来源于stack exchange,提问作者Girish

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 06:56:17