Fred
Fred

Reputation: 378

How to set date format for the result of a SELECT statement in SQL Server

I know how to use CONVERT function in SELECT statement to change the format of the Date column:

   SELECT 
       StationID
       , CONVERT(varchar, [Date], 101) as Date
       , Value 
   FROM my_table 

But I was wondering if I can set the date format in general before running the SELECT statement, when I don't know the name of the date column in the following SELECT statement:

   SELECT * from FROM my_table 

Is any SET statement or other T-SQL that I can run before my SELECT statement so that I can change the Date format temporarily?

Thank you

Upvotes: 1

Views: 6272

Answers (2)

Cade Roux
Cade Roux

Reputation: 89651

No.

In particular, any date columns which you select are not actually formatted at all, but are instead returned down the wire as an actual piece of date data in a binary "format" which is used for dates. If you are seeing them formatted, it's because your client (either management studio or some other tool) is converting them to strings to display.

When you use SELECT *, there is obviously no way to tell SQL Server to do any conversions on any particular columns, so the data is going to be returned in whatever the data types of the underlying query returns. So regardless of whether your data types are really date or not, no manipulation is going to happen at that point anyway.

Upvotes: 4

Nick Rolando
Nick Rolando

Reputation: 26157

I'm pretty sure there's no way to do what you're asking. However, there are ways to format the date string when you output it using your programming language.

Upvotes: 3

Related Questions