I have the following table create definition in SQL Server:
CREATE TABLE [dbo].[EventLog]
(
[RawEvent] NVARCHAR(MAX) NOT NULL,
[ReceivedDate] AS CAST(JSON_VALUE(RawEvent, '$.ReceivedTime') AS DATETIME)
)
When I receive a JSON string, that string has ReceivedTime which I parse out and use that as the ReceivedDate column's value when inserting this record.
This is currently working. However, when RawEvent string doesn't have the property ReceivedTime, I want to use the current SQL date time as the default value instead of NULL which is what it is doing now.
Is this possible to do in the table definition?
CASE WHEN ReceivedDate IS NULL THEN ReceivedDate2 ELSE ReceivedDate ENDor something similar. This may prove additionally useful as you will have a timestamp for the row, always.AS COALESCE(CAST(JSON_VALUE(RawEvent, '$.ReceivedTime') AS DATETIME, GETDATE()))work? (It should but I don't have any valid JSON to try with.)