I have the following query:
SELECT DATEADD(DAY, 0 - (DATEPART(weekday, GETDATE()) % 7), GETDATE())
Which displays:
2014-04-19 10:47:46.790
How can I modify the query so it displays 04/19/2014 or 04-19-2014
for 04/22/2014
select convert(varchar(10), getdate(), 101)
for 04-22-2014
select convert(varchar(10), getdate(), 110)
SELECT CONVERT(VARCHAR, DATEADD(DAY, 0 - (DATEPART(weekday, GETDATE()) % 7), GETDATE()), 101)
Outputs
04/19/2014
And
SELECT CONVERT(VARCHAR, DATEADD(DAY, 0 - (DATEPART(weekday, GETDATE()) % 7), GETDATE()), 110)
Outputs
04-19-2014
Alternately, you could format this in the consuming application (in your case, SSRS). This could be done like this
=Format(date_column, "MM/dd/yyyy")
or
=Format(date_column, "MM-dd-yyyy")
Date formatting is generally the responsibility of the presentation layer, not the data layer. DateTime doesn't have a format - it's literally a representation of a date and time of day. When you display the DateTime is when you choose the format (if you don't choose one, the system will choose one by default).
You could convert the DateTime to a varchar, but if the value is getting consumed by some other system then I would strongly recommend leaving it as a DateTime and letting the part of the system that displays the date convert it appropriately. Otherwise, if you need to do any kind of date math/comparison/etc., the system is going to have to convert back to a date value which could cause problems.
Based on your subsequent comment I would format the date in the SSRS report appropriately and let the query produce the data value as-is.
I use this to show date only portion:
SELECT CONVERT(VARCHAR(12), DATEADD(DAY, 0 - (DATEPART(weekday, GETDATE()) % 7), GETDATE()), 110)
Will that work for you?
Related
I'm using SQL Server.
I have a specific date (in dd/mm/yyyy format) i.e. 06/04/2020
However, in a T-SQL View, it needs to be always 1 year from now i.e. if I run today it would return 06/04/2021. And if it executed in 2021 it would return 06/04/2022 - how would I do this?
So I can run the below:
SELECT CONVERT(DATE, DATEADD(year, 1, '06/04/2020'), 103) as MyDate;
Which will give me:
2021-06-04
However, how do I make it self-maintaining?
You can make it generic by extracting the year from GETDATE(), adding 1 and concatenating that to 06/04 (or the date as required), and then converting. For example:
SELECT CONVERT(DATE, CONCAT('06/04/', DATEPART(YEAR, GETDATE()) + 1), 103)
Output:
2021-04-06
I have found a couple of different methods to convert it. However, I still get the yyyy-mm-dd format or yyyy-mm-dd hh:mm:ss. I am currently using SQL Server 2014.
SELECT dateadd(day, convert(int, getdate()), 112)
SELECT DATEADD(YEAR, -2, convert(DATE, GETDATE(), 112))
I am doing a date range of 2 years. Thus I need the codes to the find the date two years back.
You can also use FORMAT:
select FORMAT(getdate(), 'yyyyMMdd')
Try CONVERT(char(8), GETDATE(), 112)
Also check https://technet.microsoft.com/en-us/library/ms187928(v=sql.105).aspx
I need to set up an automated data purging (delete 7 day old data) on my SQL box.
The date column which helps me decide on whether to/not to delete the row set is in YYYYMMDD format.
I understand that in order to use GETDATE() I need to convert it (to YYYYMMDD) and here is the conversion i found-
CONVERT(CHAR(8), GETDATE(), 112)
Below is my delete statement, how do I include the before-mentioned conversion into it to make it work?
DELETE FROM dbo.MyDBName
WHERE StreamDateId < DATEADD (DAY, -7, GETDATE())
Best solution for this problem would be to use proper datatype for this date column. Anyway you can do the following....
DELETE FROM dbo.MyDBName
WHERE CAST(StreamDateId AS DATETIME) < DATEADD(DAY, -7, GETDATE())
OR
DELETE FROM dbo.MyDBName
WHERE StreamDateId < CONVERT(VARCHAR(8), DATEADD(DAY, -7, GETDATE()), 112)
I am trying to get current date - 6 days. That is easy.
Now I am trying to get current date - 6 days + 12:01 AM.
So if today is 3-2-2012 11:14 AM.
I want to get 2-25-2012 12:01 AM
These 2 selects will give me current date - 6, but will not reset the time to 12:01 AM
select getdate()-6
SELECT DATEADD(day, -6, CURRENT_TIMESTAMP);
SELECT DATEADD(minute, 1, DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) - 6, 0))
is equivalent to
SELECT DATEADD(minute, 1, DATEADD(DAY, DATEDIFF(DAY, '19000101', GETDATE()) - 6, '19000101'))
I think you will find this option faster and more flexible than the varchar implementations. by keeping the data types as they are, you don't have to worry about the vagaries of the cast/convert results.
See Louis Davidson for one of the full explainations:
http://sqlblog.com/blogs/louis_davidson/archive/2011/02/09/some-date-math-fun.aspx
Using the following will give you the result in a datetime format:
SELECT CAST(Convert(varchar(10), DateAdd(d, -6, getdate()), 101)
+ ' 12:01 AM' as datetime)
Result: 2012-02-25 00:01:00.000
Once you have the datetime that you want, you can convert it to many different formats.
Or you can do the following which is in a varchar format:
select Convert(varchar(10), DateAdd(d, -6, getdate()), 110) + ' 12:01 AM'
which results in 02-25-2012 12:01 AM
One less conversion that #Phil Helmer's solution:
SELECT DATEADD(DAY, DATEDIFF(DAY, '19000101', GETDATE()), '1899-12-26T12:01:00')
Since some people are apparently unaware that everything "to the right" of the element specified in that DATEADD/DATEDIFF pair is effectively taken from the right-hand constant. Everything "to the left" (and including the actual element) can be used to achieve offsetting effects.
(The above left/right are assuming that the entire datetime value is being interpreted with year to the left and milliseconds to the right, with all intermediate values in "size" order)
Edited - I've also updated my answer to subsume the -6 into the right-hand value. Its possible to create all kinds of offsetting by picking suitable values for the two constants.
The relationship between the two datetime constants specified in the expression ought to be expressed, at least in a comment alongside the usage. In the above, I'm using 1/1/1900 as a base point, and computing the number of midnight transitions between then and now (as DATEDIFF always works). I'm then adding that number of days onto the point in time 6 days earlier (e.g. 26/12/1899) at exactly 00:01 in the morning...
SELECT DATEADD(dd, -6, DATEDIFF(dd, 0, GETDATE())) + '12:01'
I need to write a sybase query that will have a where clause with date within last x days like so -
SELECT *
FROM table_name
WHERE
date_col > [_last_x_days]
I was able to get datetime of last x days using
dateadd(day, -x, getdate())
However, the above method still gives me the time element based on when the query is run. How can I strip down the time part?
i.e. convert 10-10-2011 15:00:45 to 10-10-2011 00:00:00
Also, is there a better way to do this?
Thanks in advance!!!
J
The convert function will return the date without the time component.
dateadd( day, -x, CONVERT(DATE, getdate(), 103) )
See this link for a complete description of CONVERT.
How about this?
convert(datetime, substring(convert(varchar, dateadd(day, -x, getdate()), 20), 1, 11))