I’ve seen a few variations of writing dates in SQL Server as it doesn’t support the more standard literal format of:
DATE '2014-01-01'
Is there a suggested way to write date-literals (or the closest thing to it) in SQL Server? Currently what I do is:
CAST('2014-01-01' AS date)
Whenever I want to use a date. Is this the most common?
>Solution :
SQL Server supports some date formats, but you can use ‘20220101’
CREATE TABLE t1([date] date)
INSERT INTO t1 values ('20220101')
SELECT * FROM t1| date | | :--------- | | 2022-01-01 |
db<>fiddle here
You are missunderstanding sql for TSQL
In your SELECT date, '20220101' FROM t1the second is a string for sql
But as you see in the query below, TSQL will convert the text i8no a date automatically when comparing for example
SELECT CASE WHEN CAST('2014-01-01' AS date) > '20220101' THEN 'TRUE' ELSe 'FaLSE' END| (No column name) | | :--------------- | | FaLSE |
db<>fiddle here