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
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 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)
This question already has answers here:
Best approach to remove time part of datetime in SQL Server
(23 answers)
Closed 8 years ago.
I am trying to show only date here and not the time but whatever i do still is showing the time like this format 4/4/2014 12:00:00 AM. I would like to remove the time portion of the date and only show the date. What am i doing wrong here?
here is my sql
SELECT
DATEADD(DD,
CONVERT(INT, (DATEDIFF(DD, '1/1/1900', t.DT)/7)) * 7, '1/1/1900') [WeekBeginDate],
SUM(HOURS) AS TOTAL_HOURS
FROM [DA2].[PMO].[TASK_TIME_TRACKER] t
WHERE UID = 'John07'
AND DT >= DATEADD(WEEK, -4, GetDate())
GROUP BY CONVERT(INT, DATEDIFF(DD, '1/1/1900', t.DT)/7)
DATEADD(DD, CONVERT(INT, (DATEDIFF(DD, '1/1/1900', CONVERT(DATE,t.DT))/7)) * 7, '1/1/1900')
You also need to change your group by expression to include the conversion. that will group each day together regardless of time, to display just the time you may need to wrap the whole line in an outer convert.
GROUP BY CONVERT(INT, DATEDIFF(DD, '1/1/1900', CONVERT(DATE,t.DT))/7)
What are you using to display the results? if it is SSRS you can change the format on the cell to date to drop the 0's that the timestamp would display.
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?
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))