Re: Format a datetime columns output?



CK (c_kettenbach@xxxxxxxxxxx) writes:
I used , Convert(varchar(25), L.FromDate, 101) AS FromDate in the View
and then CAST(FromDate AS DateTime)>= '03/27/2004' in the query. This
is an internal application but I see why it should be done in code.

This looks just wrong to me.

If you have a view, and need a condition on it with >=, why one Earth would
you could convert the date to a string? And why would you convert it to a
string that only works sometimes? When working with dates in literals in
SQL Server you should use the format YYYYMMDD, because this format is always
interpreted the same. Most other format depends on language and date format
settings.

If you are passing dates from the application as parameters, you should use
parameterised statements and pass datetime values as such not as strings.


--
Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
.



Relevant Pages

  • Re: Moving a VBA function to SQL server
    ... "String Functions " in Books Online. ... The SQL server database contains a table named 'WorkOrders'. ... This will produce a job header formatted like this: ... The format he specifies is stored in table 'Settings' - field 'JobHdrMask' ...
    (comp.databases.ms-access)
  • Re: The third time to ask in here ... Mobile Web form
    ... You would be better directing sql server questions to the ... > i can't change its format, ... > format in the DB to string or the string that the user ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Datatype-convertion in TSQL
    ... Can't find a code for datetime-conversion from nvarchar format DD/MM/YYYY ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ... Books Online for SQL Server 2005 at ...
    (comp.databases.ms-sqlserver)
  • Re: Undocumented CONVERT styles
    ... The string value will not be interpreted ... independently of the date format: ... SQL Server changes this to something else when you add it ...
    (microsoft.public.sqlserver.programming)
  • Re: Bulk Import with header in text file
    ... and there is no way to specify a footer or a header. ... The one exception is when you can hide the header in the format somehow. ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ... Books Online for SQL Server 2005 at ...
    (comp.databases.ms-sqlserver)