I'm doing the date conversion the following way:
CONVERT(VARCHAR, CONVERT(DATE, ZA2_DTFIM), 103) AS DATA
How can I add 5 days to that system date?
I tried several methods but so far nothing, I'm a beginner in SQL Server.
In TSQL the function GETDATE() provides the system date (and time).
To add 5 days to that use DATEADD(day,5,GETDATE())
To display this as a string you can use FORMAT() or CONVERT() e.g. both of these will add 5 days to the system date and then display that in day/month/year style:
select
convert(varchar,dateadd(day,5,getdate()),103)
, format(dateadd(day,5,getdate()),'dd/MM/yyyy')
Note: you do not need to convert to date because converting (or formatting) GETDATE() to dd/MM/yyyy will suppress display of time anyway.
Related
This question already has answers here:
SQL Server date format function
(3 answers)
How to get a date in YYYY-MM-DD format from a TSQL datetime field?
(25 answers)
Closed 3 years ago.
I've just been setting my dates as VARCHARs instead of dates because they need to maintain a MMDDYYYY format and I was wondering it there was a better way to be doing this. The standard date seems to just do it as YYYYMMDD
Store dates as dates (or datetime) in the database. You should be formatting dates in the presentation layer (on-screen, reports, etc.). That is the point where you use your tools' formatting capabilities to display dates in the desired format. Remember, the dates in most DBMS are stored as long integers. You're query tools are actually formatting those values into a formatted date that is displayed.
Here is a quick query to display today as a numeric:
SELECT 'Yesterday', CAST(DATEADD(DAY, -1, #Today) AS DECIMAL(32,20))
UNION
SELECT 'Today', CAST(#Today AS DECIMAL(32,20))
UNION
SELECT 'Tomorrow', CAST(DATEADD(DAY, 1, #Today) AS DECIMAL(32,20))
Output is as follows (the fraction should represent the time element, if I'm correct):
Today 43717.61447052469200000000
Tomorrow 43718.61447052469200000000
Yesterday 43716.61447052469200000000
Here is a query-level method of converting the presentation of GETDATE() in SQL Server:
SELECT CONVERT(VARCHAR, GETDATE(), 110)
Output:
09-11-2019
I want to do the correct way to get a date from a #parameter that contains the complete datetime, that user gets from a calendar from TFS File.
In the select I would want to use something like CONVERT(varchar(10), #FechaHasta.Value, 120) AS DATE01 and then get the only date, throwing out the time from the parameter.
For the next step, I would want to compare it with another date in WHERE clause, having this code :
Then, I would want to make this work on for looking for between two dates, and the last one, throwing out the time from the datetime.
Thanks.
I am assuming #FetchHasta is a datetime
CONVERT(VARCHAR(10), #FetchHasta, 101)
is what gets you just the date part. i.e. 10/28/2014 1:10 PM would simply become 10/28/2014.
Source
EDIT: Alternatively, How to return the date part only from a SQL Server datetime datatype
If I use this
SELECT CONVERT(DATE, '26/03/2014', 101)
I get an error:
Conversion failed when converting date and/or time from character string.
But if I use this
SELECT CONVERT(DATE, '26/03/2014', 103)
There's no error and this is the result returned:
2014-03-26
I don't understand why the first code is not working, as far as I searched and understand is that 101 is for US date and 103 is for UK/French Date.
This:
SELECT CONVERT(DATE, '26/03/2014', 101)
will be interpreted in the US way (mm/dd/yyyy) : the 26th month, 3rd day of 2014 - this obviously fails (no 26th month).
This however:
SELECT CONVERT(DATE, '26/03/2014', 103)
will be interpreted the European way (dd/mm/yyyy): the 26th day of the 3rd month (March) of 2014.
You need to very careful with parsing strings to date! Check out all the defined styles for CONVERT here
If you want to be sure it works always, use the ISO-8601 format: YYYYMMDD or in your case:
SELECT CAST('20140326' AS DATE)
will always work, no matter what language/regional settings you have
Thats becuase of the format specifier(101) which you are using.
101 is mm/dd/yyyy
So 26 cannot be a month. Hence resulting in error.
103 is dd/mm/yy
And hence it is working correctly. if the day would have been less than 13, it would have taken it as month and there would be logical error.
The convert signature is as follows
CONVERT(data_type(length),expression,style)
for the date conversion, the styles are as follows
101 mm/dd/yy USA
103 dd/mm/yy British/French
more formats here http://www.w3schools.com/sql/func_convert.asp
Use language neutral date representations for literals. In case with style 101, SQL Server assumed the MM/dd/yyyy instead of dd/MM/yyyy.
Here's a nice link with more info from MVP Tibor Karaszi:
http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes
I'm trying to make my SQL Server table datetime columns save datetime with AM/PM. How to make SQL Server to save datetime with AM/PM format?
Right now it saves date like this: 2012-01-23 14:47:00.000
Is it possible to save it 2012-01-23 02:47:00.000 PM ??
Or does SQL Server save the date and time in this format (2012-01-23 14:47:00.000) all the time and I need to convert it just on output and input?
Is it even possible to save it in this format (2012-01-23 02:47:00.000 PM)? Or does SQL Server save datetime in 24 hour format?
thanks indeed for any help. sorry for language. ;)
Internally the date and time are stored as a number.
Whether it's displayed in a 12 or 24 hour clock is up to the program formatting it for display.
As Andrew said, Datetime format is stored not as string. so, you can use CONVERT function to get the datetime value in approprate format. for example,
SELECT CONVERT(VARCHAR(20), GETDATE(), 100)
to learn more about datetime formatting, see this article
AM/PM serves only for visualization, if you need to display them, use CONVERT keyword:
SELECT CONVERT(varchar, YourDateTimeField, 109)
FROM YourTable
If you need to store AM/PM - it is makes no sense for datetime type, use varchar type instead.
You can simply use CONVERT function as following:
select CONVERT(VARCHAR,GETDATE(),108)
http://www.fmsinc.com/free/NewTips/SQL/AM_PM_time_format_in_SQL.asp
http://msdn.microsoft.com/en-us/library/Aa226054
http://blogs.msdn.com/b/kathykam/archive/2006/09/29/773041.aspx
Depending on the accuracy of the datetime you are storing you might be able to clean it up with
REPLACE(CONVERT (varchar, YourDateTimeField, 109), ':00.0000000', ' ')
This will not work if your date field is populated with GETDATE() as that means it will contain seconds and milliseconds but it will work if the field is populated by a user and seconds and milliseconds are all zeros
Hey fellas, I'm having difficulty obtaining only the date from the system and inserting it into a column, is there a built-in function that can acquire it?
On top of that, how do I add years to the current date?
I know I'm pushing it right now, but I'm also wondering what's the format for the date datatype?
Because sometimes I'd like to manually insert values into a column with that type in mind.
Any help would greatly be appreciated.
Thanks.
To get date only (SQL Server 2008 only) CAST to date type
SELECT CAST(GETDATE() AS date)
To add years, use DATEADD
SELECT DATEADD(year, 2, CAST(GETDATE() AS date))
Formats: use yyyymmdd or ISO yyyy-mm-dd (for newer datetime types) for safety.
Read this for everything about date+time in SQL Server
To add a year to the current date, look at the dateadd() function.
To just get the date from sql w/o the time, you can do this:
DECLARE #Date DATETIME
SELECT #Date = CONVERT(VARCHAR, GETDATE(), 101)
SELECT #Date
Sql will implicity convert the VARCHAR back to DATETIME. Look up the CONVERT function in BOL and it will give you all kinds of different styles for the 3rd parameter.
Bender