Displaying TIME in SQL - sql

How can I display data type TIME in SQL as 09:00 or 09:00AM instead of 09:00:00.000000
For instance, I have a column start_time and its type is TIME
When I insert data in it, INSERT INTO Time(start_time) VALUES('09:00:00')
It displays it as: 09:00:00.0000000

You need to format dates as per your requirement.
have a look at
http://www.sql-server-helper.com/sql-server-2008/sql-server-2008-date-format.aspx
or
http://www.w3schools.com/sql/func_convert.asp
sample
SELECT CONVERT(VARCHAR(10), SYSDATETIME(), 101) AS [MM/DD/YYYY]
SELECT CONVERT(VARCHAR(10), SYSDATETIME(), 103) AS [DD/MM/YYYY]
..........

You need to take a look at the Format function in T-SQL. This will allow you to format date and time values (and another values) any way you want to. The link I provided is for SQL 2014. Format was added in version 2012. You have your question tagged as 2008, so I'm not sure this answer will apply to you. If not, take a look at Cast and/or Convert here.
By the way, you may not know this but how you enter a date/time value into SQL Server has no relationship to how it is displayed. And, generally, the displaying of date and time values is done in some UI tier of an app, and not at the SQL Sever level, even though T-SQL provides methods for formatting dates and times.

Related

Convert YYYYMMDD to MM/DD/YYYY in Snowflake

I need help in figuring out the date conversion logic in Snowflake. The documentation isn't clear enough on this.
In SQL Server, I would try
SELECT CONVERT(DATE, '20200730', 101)
and it gives me '07/30/2020'.
If I try the following in Snowflake,
to_varchar('20200730'::date, 'mm/dd/yyyy')
it gives me '08/22/1970'. Why would it give an entire different date? Need help in getting the logic with the correct date.
The issue with what you are doing is that you are assuming that Snowflake is converting your string of '20200730'::DATE to 2020-07-03. It's not. You need to specify your input format of a date. So, 2 options based on your question being a bit vague:
If you have a string in a table and you wish to transform that into a date and then present it back as a formatted string:
SELECT TO_VARCHAR(TO_DATE('20200730','YYYYMMDD'),'MM/DD/YYYY');
--07/30/2020
If the field in the table is already a date, then you just need to apply the TO_VARCHAR() piece directly against that field.
Unlike SQL Server, Snowflake stores date fields in the same format regardless of what you provide it. You need to use the TO_VARCHAR in order to format that date in a different way...or ALTER SESSION SET DATE_OUTPUT_FORMAT will also work.
Try select to_varchar(TO_DATE( '20200730', 'YYYYMMDD' ), 'MM/DD/YYYY'); which produces 2020-07-30
You may need to refer to https://docs.snowflake.com/en/user-guide/date-time-input-output.html#timestamp-formats

Casting nvarchar to date with a formatting in SQL-Server

I'm using SQL Server Management Studio. There is one table in my database containing dates which are stored as nvarchar(255). I want to migrate the data of this table to a new table which I call Converted_Dates and store this data as date. Also, I want them all to be formatted like this YYYY-MM-DD
Currently the Dates table looks like this:
**Dates**
15/6/2011
16/6/2011
2013-03-30
2013-04-16
...
I want the new table to look like this:
**Converted_Dates**
2011-06-15
2011-06-16
2013-03-30
2013-04-16
...
I execute this query but formatting of dates remains the same, only the data type changes from nvarchar(255) to date.
USE [reporting_database]
GO
INSERT INTO [dbo].[Converted_Dates]
SELECT
cast(Dates as date)
FROM [dbo].[Dates]
GO
Any advice on how to cast the data from the old table to a new one in a preferred format?
The value of date and datetime data type is not stored with format in sql server. If you want to see the date in a different format you can manipulate the way that date and datetime data types are displayed when converted to a varchar (or nvarchar,nchar,char) data type using some built in functions.
You should store your dates as date data type, and if you can format them at the application level, do so there. If you must format them in sql, then use convert() styles.
select convert(char(10),getdate(),120)
returns: 2017-05-01
In sql server 2012+ you can use format()
select format(getdate(),'yyyy-MM-dd')
returns: 2017-05-01
But format() is much slower, take a look here: format() is nice and all, but… - Aaron Bertrand

Need a date instead of datetime in SQL Server 2005 with dateadd

I've got a query that get's a date of a field in a program. This date has to be modified with 10 years.
The query I made is
SELECT DATEADD(yy, +10, '"+thisfield.value+"')
where '"+thisfield.value+"' is coming from the program and is filled in like 01-08-2012.
The result of the query is 2022-07-31 00:00:00.000. The problem I have is that I just need 2022-08-01 but in the format of 01-08-2022 so that I can automatically fill an other field with this result.
In SQL Server 2005 the date function doesn't work only the datetime function and I just don't need that.
I hope this is clear (first time i post something). Can anyone help me?
You can either truncate the result, or cast it to DATE
CAST(DATEADD(yy, +10, '"+thisfield.value+"') AS DATE)"
CONVERT(VARCHAR, DATEADD(yy, +10, '"+thisfield.value+"'), 101)"
CONVERT using style 101 is in the format mm/dd/yyyy, which happens to be the format you want. Keep in mind that you can format the result however you want in your application for display purposes which is better than returning strings from SQL Server.
Also note that you should look into parameterized queries and/or stored procedures.

Change default dateformat stored in a database

I am seeeing my dates are stored in database in this format for a column (datetime datatype) 2011-01-14 10:15:41.787 i.e YYYY-MM-DD way . How could I make the default storage in YYYY-DD-MM format . Do I need to set that for all the DBS, or I can set it for single DB and how ?
I have the column in datetime datatype, right now it is saving as
2011-01-14 10:15:41.787 , my question is how can I set the db to store it as
2011-14-01 10:15:41.787
That is the crux of the confusion. Just because SQL Server Management Studio displays a datetime column in that format does not mean that it is stored AS TEXT YYYY-MM-DD hh:mm:ss.zzz. It is stored as binary, something like 0000101000001010..
Your dates are stored in SQL Server as a series of bytes (bits really) that make up some numeric value that is an offset from 1900-01-01. There is no inherent format the the dates. What you are referring to is that SSMS by default shows [display] datetime columns as YYYY-MM-DD hh:mm:ss.zzz. If you use a front-end programming tool, that too may impose a default [display] format unless you have asked for another one.
There is absolutely NO way to make SSMS show datetime data in another format through options or configuration. If you must, you would have to update the SQL query to convert the datetime column to a VARCHAR column containing the TEXTual equivalent in a particular format. That may be useful in SSMS, but would be bad when used as a data source to front-end GUI/web apps - since the values are not datetime and cannot be used for interval calculation, graphing, bound to date controls etc.
See this example of displaying time (getdate()) as YYYY-DD-MM, a very unusual format. Notice the date field/variable has to be used twice:
select stuff(convert(char(7), getdate(), 120), 5, 0, '-' + convert(char(2), getdate(), 3))
DATETIMEs are stored internally as two 4 byte integers, so firstly you are seeing a formatted representation for the UI - it's not actually stored in a particular date/time format as such.
e.g. if you insert just a date like "2010-01-01" then it will still hold the time element: 2010-01-01 00:00:00.000
If you're only interested in the DATE part, then you can format the DATETIME for output either in your front-end code or via your query:
e.g.
SELECT CONVERT(VARCHAR(8), GETDATE(), 121)
So even if the DATEs you insert contain a time, that will be ignored when returned. You could also ensure you only insert dates without the time specified - you need to handle that in whatever code is doing the INSERTs. e.g. from .NET, instead of passing in DateTime.Now you could pass in DateTime.Now.Date.
In SQL Server 2008, there is a DATE datatype which is there to only store a DATE (without time) which is really what you want in this kind of scenario.

SQL date format issue in select query

I have an ASP page which will fetch records from a SQL server DB table. The table "order_master" has a field called order_date. I want to frame a select query to fetch order date > a date entered by user(ex : 07/01/2008)
I tried with convert and cast, but both are not working. The sample data in order_date column is 4/10/2008 8:27:41 PM. Actually, I dont know what type it is (varchar/datetime).
Is there any way to do that?
I'd check to make sure that the SQL datatype is a DateTime or SmallDateTime first, then I'd check to make sure that you're passing in a Date/DateTime value from the page.
If those are both correct, then you'd probably be better off following Joel's advice and explicitly convert both values to dates before trying the comparison. Also, check the precision of the time values that you're looking at; it seems obvious, but 1/1/2008 12:00:00.001 AM will not be equal to 1/1/2008 12:00:00.000 AM. Yes, I am speaking from experience. :P
the 07/01/2008 date is the British/French annotation, so all you need to do is:
SELECT myColumn FROM myTable WHERE myDateField >= convert(datetime, '07/01/2008 00:00:00', 103)
this code will get all rows where myDateField has the date 7th of January 2008, since 00:00:00 (hh:mm:ss) so, the first second on that day... in simple words, the entire day.
for more info, check Books online on MSDN
You could create a stored procedure like this
CREATE PROCEDURE GetOrders
#OrderDate DATETIME
AS
SELECT
*
FROM order_master
WHERE Order_Date > #OrderDate
GO
Then you can just convert the users input to a date before calling the stored procedure via your ASP code.
Edit
I just noticed the remark about the column type, you can run this command
sp_help order_master
to get column information to find the data type of order_date.
Have you tried CONVERT()'ing both values to a datetime type?
Remember that when comparing dates, 4/10/2008 8:27:41 PM is not equal to 4/10/2008. SQL Server will interpret 4/10/2008 to mean 4/10/2008 12:00:00 AM, and do an exact comparison down to the second. Therefore 4/10/2008 is LESS THAN 4/10/2008 8:27:41 PM
You state you don't know the field type. That would be the first problem to solve, find out. You can do that with:-
SELECT DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Order_Master' AND
COLUMN_NAME = 'Order_Date'
If its not one of the datetime types it should be converted, if that isn't your responsibility then get on to someone who does have the responsibility.
The fact that you are concerned about the 'format' of the date indicates that you may be building the SQL using concatenation. If so stop doing that. Use a command with a parameter and pass in the date as date type.
Now your issue is one of how the date is entered at the client end and getting it into an unambigous format that can be parsed as a date in the ASP code.
If that is not something you have solved add a comment to this answer and I'll expand this answer.