issues with showing date with time format in sql [duplicate] - sql

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.

Related

Specific Date but increase Year based on GETDATE()

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

Pulling data from Sybase SQL database using rolling window

I wish to pull three years of data from a database but right now I have to specify the dates in a number of sections in my code using a between statement:
BETWEEN '2015-10-01' AND '2018-09-30'
Since the database only contains valid data from the previous month backwards, I wish to take the end of the last month and go back three years.
I found this tutorial where the author shows how to do this in SQL server and I've tried to adapt it but my RDBMS is throwing errors in the datediff function
----Last Day of Previous Month
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))
LastDay_PreviousMonth
My code looks as follows but I the error I am seeing is about converting 0 to a date.
DECLARE #date DATE
SET #date = getdate()
SELECT dateadd(second,-1,dateadd(mm, DATEDIFF(m,0,GETDATE()),0))
If anyone has any suggestions I would be very grateful for your guidance.
In your WHERE clause, you could use this condition:
DATEDIFF(month, [YourDateColumn], GETDATE()) BETWEEN 1 AND 36
Your current code looks good without any error, but you can use EOMONTH() instead.
However, the difference would be in return types your code would return date-time while eomonth() would return date only.
DECLARE #date DATE
SET #date = getdate()
SELECT EOMONTH(#date, -1)
Don't use between and the job becomes far easier. Use less than the first day of the current month which accurately locates everything before that date (instead of last day of previous month). Subtract 3 years from that same date and use >= for the starting point.
Select *
From yourtables
where datecol >= dateadd (year,-3,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0) )
And datecol < DATEADD(mm,DATEDIFF(m,0,GETDATE()),0)
Please don't use any solution that use 23:59:59 as the end of a day. It is not the end of a day and several data types now support sub-second time precision.
If you really cannot use zero in the date functions simply use the base date of '1900-01-01' instead
SELECT
DATEADD(YEAR, -3, DATEADD(mm, DATEDIFF(m, 0, GETDATE()), '1900-01-01'))
, DATEADD(mm, DATEDIFF(m, 0, GETDATE()), '1900-01-01')
;
This should also work (it does in this demo here):
SELECT
DATEADD(YEAR, -3, DATEADD(mm, DATEDIFF(m, 0, GETDATE()), 0))
, DATEADD(mm, DATEDIFF(m, 0, GETDATE()), 0)
;

TSQl: Select Current Date - 6 Days and set time to 12:01 AM

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'

Sybase- sql query where date in last x days without considering time part

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))

Find between with separated date fields (year,month,day)

I have the following dates in my table in separated fields. How can I write a query to show the values between two dates.
For example: values between 2/1/2011 and 2/6/2011:
day month year value
--------------------------------------------------
2 6 2011 120
3 7 2011 130
5 5 2011 100
6 1 2011 50
As others have said, my first suggestion would be to use Date. Or if you need more detailed information than your example, Datetime or Timestamp with Time Zone.
But in case you actually have to work with this data, I think something like this should work, depending on your flavor of SQL:
SELECT value, CONVERT(DATE,CONCAT(CONCAT(CONCAT(CONCAT(day, "-"), month), "-"), year), 105) date FROM table_name where (2/1/2011) <= date and date <= (2/6/2011);
(with Oracle SQL, you can use to_date instead of convert and optionally use the || concatenation operator; with SQL server you have to use the + concatenation operator; with MySQL this should be right)
(2/1/2011) and (2/6/2011) could either be strings that you convert similar to the other convert, or inputted using a PreparedStatement or something like it as dates directly (this would be preferable).
I had the same scenario but with Month column displaying Month name . With slight modification on the given query i was able to fetch the data.
SELECT *
FROM Table_Name AS Tbl_Date
WHERE (Year * 10000 + DATEPART(mm, CAST(Month + Year AS DATETIME)) * 100 + 1
BETWEEN 20111101 AND 20121201)
Hope this will help
To convert to Date for easier comparisons without worrying about dmy or mdy, in a standard fashion:
DATEADD(year, year-1900, DATEADD(month, month-1, DATEADD(day, day-1, 0)))
So, something like this. The safest date format to use is yyyymmdd (especially with SQL Server)
SELECT
value,
DATEADD(year, year-1900, DATEADD(month, month-1, DATEADD(day, day-1, 0))) AS realdate
FROM Mytable_name
WHERE
'20110201' <= DATEADD(year, year-1900, DATEADD(month, month-1, DATEADD(day, day-1, 0)))
and
DATEADD(year, year-1900, DATEADD(month, month-1, DATEADD(day, day-1, 0))) <= '20110206'
if you are using oracle database then you can use TO_DATE and TO_CHAR functions to achive this target...
as follow-
select * from table_name
where to_date(day||month||year,'DDMMYYYY')
between &mindate and &maxdate
min date you can put 2-jan-2011 and max date as 2-jun-2011
I hope it should work for you :)
well i found the answer that i wanted thanks guys
SELECT Tbl_Date.Value,Tbl_Date.Year,Tbl_Date.Month,Tbl_Date.Day from Tbl_Date
where ((Tbl_Date.Year*10000)+(Tbl_Date.Month*100)+Tbl_Date.Day) between 110102 and 110602