How to create a hardcoded date parameter for use in a query?
Solution 1
To avoid troubles with locales, I explicitly do not want to use a string like "yyyy/mm/dd" or similar.
To avoid this, the best way is passing the date as language-neutral like YYYYMMDD
. This way it will be language independent:
SELECT * FROM User
WHERE LastActivity > '19800101';
Solution 2
Use ISO date format
which is yyyymmdd
SELECT * FROM User
WHERE LastActivity > CONVERT(DATE, (CONVERT(VARCHAR(4), @Year) +
RIGHT('0' + CONVERT(VARCHAR(2), @Month),2) +
RIGHT('0' + CONVERT(VARCHAR(2), @Date),2) ))
Marcel
I am an experienced software developer for both technical and business software, mainly in C#/.NET. Most professional projects are web applications or web services in the telecommunications field, for large corporate customers. I work and live in Switzerland. In my spare time I build and hack hardware stuff and occasionally, I blog on https://qrys.ch about it.
Updated on August 19, 2022Comments
-
Marcel over 1 year
Example (not working like this):
SELECT * FROM User WHERE LastActivity > Date(1980,1,1)
Where Date should use the parameters year, month, day as integer numbers. To avoid troubles with locales, I explicitly do not want to use a string like "yyyy/mm/dd" or similar.
I am working with Microsoft SQL Server Management Studio on an MSSQL Database, if this matters.
Note: I am sure this is trivial, but I could not find the solution neither using google or SO.