I have a variable as time(7), but it gives me values like: '17:25'. I'd like for it to be in this format: '5:25 PM'. Is there a way to do this in SQL 2012 without doing something like this:
DECLARE #aux NVARCHAR(8)='16:45:00'
SELECT CONVERT(VARCHAR(15),CAST(#aux AS TIME),100)
Code gotten from here
No, you cannot do anything about the format SQL Server uses to store the Time datatype. You can only use tricks like the one you mentioned at query-time to deliver the output in a desired format. Or better yet, do the formatting in the front-end application.
you can try to use DATEPART(HH... with case statements or IF/ELSE to alter the times past noon, and CONCAT am or pm on the end
Related
How do you convert SQL mm/dd/yy datetime to mm/dd only? On Microsoft server.
Thanks all.
With dates and times it is an extremely common mistake to believe that what you see is what is stored. If the field is date, datetime, smalldatetime or datetime2 then what is stored are integers, not strings. So if the field is one of these, then:
convert(varchar(5),[date_field],1)
or
format([date_field],'MM/dd') -- mssql 2012 onward
If the information is a string already then left() will do the job.
Since you have specified an input format, the input must already be a string. Simply truncate with
cast(dateIn as char(5)).
You can use LEFT to just return the day and month:
SELECT LEFT('12/12/2000', 5)
I realize this isn't directly answering your question the way you asked it, but the best advice I can give is: Don't.
Instead, send back the field in its native datetime type. The database is not the place to be doing formatting. Instead, format the date in your application code.
For example, if you are calling SQL Server from a C#/.NET application, you could retrieve the value from a DataReader like this:
DateTime dt = (DateTime) reader["YourDateTime"];
Then you would format it as a string like this:
string s = dt.ToString("MM/dd");
This will ensure that the date is formatted correctly. If you are using a different language to call SQL Server, there are probably similar methods in that language.
One of the problems with the other approach mentioned (trunacating the string) is that the original value might not be formatted in mm/dd/yyyy to begin with. That all depends on the environment settings where the SQL Server is running. If you run the same code on an environment with dd/mm/yyyy settings, you would have unexpected results. This is avoided by using the native data type, the way I described.
Just starting out with SQL, so there's probably a really easy answer here, but I couldn't figure out exactly what i needed to do from a google search.
There's a stored procedure in the database I'm using called dt_char_sp - simply put, the parameters are the date and time as ints, and the stored procedure formats them nicely. I would expect that it works something like this:
EXEC dt_char_sp 20130416, 024356
go
output:
04/06/2013 02:43:56
except the output doesn't happen! I see that there is a variable declared as "#datetime datetime=NULL OUTPUT" and eventually the #datetime variable is filled with the formatted string, but I'm wondering if there's a way to get this output variable?
We cannot be sure without actually seeing the stored procedure, but it sounds like it is using an output parameter.
DECLARE #retval As DATETIME
EXEC dt_char_sp 20130416, 024356, #datetime=#retval OUTPUT
PRINT #retval
go
There is an equivalent way to do this from client code as well.
You need to use a Select statement.
At the end of your query just Select the desired output variable like so :
SELECT #datetime
Another way would be to use an Output Parameter. Declared like so :
#datetime DateTime OUTPUT;
Are you sure that you are doing something to get a return value?
If not, you can select the variable
select #datetime
Please do not burden the SQL Server with lame stuff like formatting strings. It's much too important for that kind of thing. I see this a lot lately, and it's a bad habit.
Please, use your client formatting tools for this...
Something like (for UNIX-type dates)
DateTime dt = new DateTime("1/1/1970").AddDays(myIntDate);
Console.Writeln(dt.ToString("mm/dd/yyyy"));
This is better for a lot of reasons - performance is the big one. You have all the data you need already, there is no reason to make a round-trip to a very expensive server when you don't really need anything from it. All computers can format dates, don't use the server for it.
This IS the correct answer by the way. I don't think Stack Overflow should become the place where if someone asks how to jump off a bridge, we just tell them. When someone is asking how to accomplish something USING THE WRONG METHOD, WE SHOULD TELL THEM THE CORRECT METHOD.
Our application using SQL SERVER 2005
I need to show only DATE from DateTime in GridView.
I don't want to convert it into any other format like Varchar or something.
It should be only in DateTime format itself, without Time.
Please Help.
In SQL Server, the code is
CAST(datediff(d,0,datetimecol) as datetime)
However, I doubt that does any good for a GridView, which will infer it to be a "datetime" column and show a "date + time" formatting, even if the times are ALL "00:00:00".
Bound the DateField coloumn in Grid like below, then it works...
<asp:BoundField DataField="Your_Date_Column"
HeaderText="Date_Column"
DataFormatString="{0:d}" />
DataFormatString="{0:d}" it display the Date like 3/11/2013
In SQL 2005 there is no way of keeping it in datetime format without the time (time is part of the datetime type), the best you could get would be something like:
2013-04-02 00:00:00.000
If you were willing to convert to VARCHAR, you could use something similar to:
SELECT CONVERT(VARCHAR(10),GETDATE(),111)
Which would display the date portion as you want it. Otherwise, you need to move to SQL 2008+ where there is a proper date type that you can use instead.
If you need to stick with SQL 2005, the best way to achive this is to just format it in the datagrid view itself.
If you want to truncate a datetime so that it only returns the date as at midnight, see the accepted answer to this question - the question relates specifically to SQLServer 2008, but the answer includes details of how to achieve this in SQLServer 2005.
If you want to convert a datetime field into a date-only field - you cannot achieve this in SQLServer 2005 in SQL alone, as there is no date-only datatype - see here.
I'm trying to remember the syntax to change a date field into a string. I know I'm close but not 100% correct. Here is what I'm using so far: TO_CHAR(FIELD_NAME). I'm using an Access database. The error I'm getting is: undefined expression. Any help would be very much appreciated.
Use either CStr(dateField) or Format(dateField) to convert.
Additionally you can add parameters to Format() to show it in a different format, such as:
Format(dateField, "general date") 9/12/2010
Format(dateField, "long date") Monday, September 12, 2011
Given that you're using MS Access and its a date field you're probably not just looking to convert to string but to also format the Date. If that is indeed the case you'll want the Format function
SELECT Format ([DateCreate], "yyyy/mm/dd") AS Foo
FROM MSysObjects;
If you're using SQL Server, try out CAST or CONVERT
You can use the CONVERT function, like this:
CONVERT(VARCHAR, DateField, 100)
Here's a link that shows the different date formats you can use:
http://www.sql-server-helper.com/tips/date-formats.aspx
I assume SQL Server as you're questions in the past are .NET Questions.
Use CONVERT
http://msdn.microsoft.com/en-us/library/ms187928.aspx
I have problem. I have script with more insert where is datetime in format 'DD.MM.YYYY'.
Problem is that server understand it as 'MM.DD.YYYY', so half of dates import with wrong value and half of dates do error.
Is any simple way how to say server that what is correct format?
I know that in past, if I worked with Oracle, I solve it by to_date function where I could specify format.
I believe http://support.microsoft.com/kb/173907 holds the answer
eg
set dateformat dmy
Using something like this should work:
convert(datetime, '28.6.2011', 104)
From here:
http://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/