Why does the Datediff function show different values? - sql

When I am executing following query I am getting different results.
SELECT Datediff(year, 0, Getdate());
The result was 115
When I use this, I am getting another result:
SELECT Datediff(year, 1900, Getdate());
The result was 110
Actually in SQL Server it will take from 1900-01-01, but why do these show different values?

Try this to explain the logic:
select cast(0 as datetime)
select cast(1 as datetime)
An integer is interpreted as the number of Days since 1900-01-01 whereas a string value such as '1900' will be interpreted as a date format.
1900 Days from Jan 1st 1900 is 1905-03-16, which is five years from 1900 and 110 years from now (2015).

This is because if you cast 0 as datetime, it returns 1900 as the year part, whereas 1900 cast as datetime returns 1905 as the year part.
Demo
From MSDN:
Values with the datetime data type are stored internally by Microsoft SQL Server as two 4-byte integers. The first 4 bytes store the number of days before or after the base date, January 1, 1900. The base date is the system reference date.
That means, casting the literal 0 to datetime is equivalent to getting the datetime value for 0 days after 1/1/1900, which is 1/1/1900. Similarly for 1900. Therefore, as #MartinSmith points out in the comments, your calculation is equivalent to SELECT Datediff(year,dateadd(d,0,'1/1/1900'), Getdate()) which returns 115 as expected.
Possibly worth noting that the MSDN page on Cast and Convert does not specifically cover this scenario i.e. int to datetime.

The number you specified will be added as days which resulted in the difference.
Select DATEADD(dd,0,0)
Select DATEADD(dd,1900,0)
Result1 is 1900
Result2 is 1905.
So using them is equal to:
SELECT Datediff(year,0, Getdate()) = SELECT Datediff(year,DATEADD(dd,0,0), Getdate());
SELECT Datediff(year,1900, Getdate()) = SELECT Datediff(year,DATEADD(dd,1900,0), Getdate());;

Related

SQL Server error in conversion of date from string

NPD.CreatedOn is defined as a datetime datatype column (in SQL Server).
SELECT *
FROM NPDMaster NPD
WHERE DATEDIFF(MONTH, CONVERT(VARCHAR(7), NPD.CreatedOn, 126), CONVERT(VARCHAR(30), GETDATE(), 126)) <= 6
I get this error:
Conversion failed when converting date and/or time from character string.
What can I try to resolve it?
Don't use things like DATEDIFF in the WHERE on your columns, such queries aren't SARGable and thus can (will) perform poorly. If you want rows where the date is on or after the start of the month 6 months ago then do the date logic on GETDATE()/SYSDATETIME()/etc:
SQL Server doesn't have a "start of month" function, but you can use EOMONTH and then add a day:
SELECT *
FROM dbo.NPDMaster NPD
WHERE NPD.CreatedOn >= DATEADD(DAY, 1, EOMONTH(GETDATE(),-7));
You don't need to convert the datetime values to text. DATEDIFF() expects datetime values as second and third argument:
SELECT *
FROM NPDMaster NPD
WHERE DATEDIFF(month, NPD.CreatedOn, GETDATE()) <= 6
The actual reason for the error (as is explained in the documentation), is that ...DATEDIFF implicitly casts string literals as a datetime2 type.

How to get start and end of month in numeric type for SQL where clause

I want to run a query monthly to look for records from the previous month. The where clause will be greater or equal to the 1st day of the month and less or equal to the last day. The difficultly I am having is the dates are stored as numbers in a column with a numeric data type. The format being used is yyyymmdd. We are currently manually changing the where clause, so e.g. show me any records with dates >=20201001 to <=20201031, but we need to automate this process. I have tried a few ways to try and solve this but need some guidance.
So far I've tried:
select concat( CONCAT(cast((Year(DATEADD(month, -1, getdate()))) as numeric),cast((Month(DATEADD(month, -1, getdate()))) as numeric)),'01')
Returns error message operand data type numeric is invalid for concat operator. Works in separate query window returning format yyyymmdd.
SELECT DATEADD(mm, DATEDIFF(mm, 0, (DATEADD(month, -1, getdate()))), 0)
Returns error message arithmetic overflow error converting expression to data type date time. Works partly in separate query window but returns yyyy-mm-dd hh:mi:ss:ms
Any help would be greatly appreciated.
Thanks
The real solution is fix the design; don't store dates in a datatype other than a date and time data type.
What you can do, however, is convert the value of GETDATE() to a numerical value of the same format:
SELECT ...
FROM ...
WHERE NumericalDate >= CONVERT(varchar(8),DATEADD(DAY, 1, EOMONTH(GETDATE(),-1)),112)
AND NumericalDate < CONVERT(varchar(8),DATEADD(DAY, 1, EOMONTH(GETDATE())),112)
For today, this would return rows where NumericalDate is in November 2020 (specifically on or after 20201101 and before but not on 20201201).
Note, I don't "bother" to CAST/CONVERT the varchar to a Numerical data type, as it'll be implicit cast due to data type precedence.
You can use artihmetics like so:
where dates >= year(getdate()) * 10000 + month(getdate()) * 100 + 1
and dates < year(dateadd(month, 1, getdate())) * 10000 + month(dateadd(month, 1, getdate())) * 100 + 1
Compare the month and forget the days.
BETWEEN 20201000 and 20201099 finds the same rows as your example >=20201001 to <=20201031
That means that you do not have to figure out how many days are in the month.

Pervasive dval stores dates as integer in days from 01/01/0001. I need a function to convert it for sql server

I need help. Pervasive DB stored dates in days. For example 719311 would be the amount of days from 01/01/0001 to May 28 1970. So May 28 1970 is the date represented in pervasive when looking at 719311.
You can use SQL Servers dateadd feature for this. However the values in Pervasive are much too large to add to the base date of 0 (1900-01-01).
To work around that I used the known integer/date provided and calculated the delta between 0001-01-01 and 1900-01-01 - which is 693597. This value is static, and can be subtracted from each Pervasive value and used in the dateadd. The result can then be added to 1900-01-01.
-- GET BASE DATE OF 0
SELECT CONVERT(DATETIME,0)
-- CALCULATE DIFF FROM 0 TO A KNOWN DATE...
SELECT DATEDIFF(DD,0,'1970-05-28')
--SUBTRACT ABOVE DIFF FROM KNOWN INTEGER FOR SAID DATE... THIS IS OUR DELTA
SELECT 719311-25714
-- ADD THE INTEGER OF SAID DATE, MINUS THE ABOVE DELTA TO 0 TO CONFIRM WE GET THE KNOWN DATE.
SELECT DATEADD(DD,719311-693597,0)
-- USING VARIABLES, LEAVE #DELTA AS A STATIC VALUE, JUST UPDATE #PERVASIVE
DECLARE #PERVASIVE INT, #DELTA INT
SET #PERVASIVE=719312
SET #DELTA=693597
SELECT DATEADD(DD,#PERVASIVE-#DELTA,0)
select datediff(day,'0001-01-01','1753-01-01') + 2
select 719311 - 639907 -- sql
select dateadd(day,79404,'1753-01-01')
dateadd() function cannot work with a date older than '1753-01-01', so you need the 719311 days minus the 639907 days. The difference you can than plug into the dateadd() function.
You should get this 1970-05-28 00:00:00.000
Another alternative is add days to a date datatype.
When I add 719311 days to '0001-01-01' I get '1970-05-30'
To get '1970-05-28' I have to remove 2 of those days.
select dateadd(day,719311-2,convert(date,'00010101'))
returns '1970-05-28'

timezone in GetDate query is 12 hours out

I am using MS webmatrix and razor.
I have a query that uses the expression CAST(GetDate() as INT) to get the current date integer value. However, even though my server and PC are both set on GMT + 12 (Wellington, Auckland), the value returned is 12 hours out - and at 12.00 pm on my PC (and the server) it jumps ahead one day.
How do I trim 12 hours off the value, without having to set the time 12 hours wrong on my machines?
Grateful for any help.
Coercing a date directly into an INT looks quite wrong.
To properly get just the INTEGRAL value of the date, use DATEDIFF directly.
select cast(cast('20120301' as datetime) as int) -- 40967
select cast(cast('20120301 12:30' as datetime) as int) -- 40968, oh noes!
select datediff(d,0,'20120301') -- 40967
select datediff(d,0,'20120301 12:30') -- 40967, yes!

How do you extract just date from datetime in T-Sql?

I am running a select against a datetime column in SQL Server 2005. I can select only the date from this datetime column?
Best way is:
SELECT DATEADD(day, DATEDIFF(Day, 0, #ADate), 0)
This is because internally, SQL Server stores all dates as two integers, of which the first one is the ****number of days*** since 1 Jan 1900. (the second one is the time portion, stored as the number of seconds since Midnight. (seconds for SmallDateTimes, or milleseconds for DateTimes)
Using the above expression is better because it avoids all conversions, directly reading and accessing that first integer in a dates internal representation without having to perform any processing... the two zeroes in the above expression (which represent 1 Jan 1900), are also directly utilized w/o processing or conversion, because they match the SQL server internal representation of the date 1 jan 1900 exactly as presented (as an integer)..
*NOTE. Actually, the number of date boundaries (midnights) you have to cross to get from the one date to the other.
Yes, by using the convert function. For example:
select getdate(), convert(varchar(10),getdate(),120)
RESULTS:
----------------------- ----------
2010-05-21 13:43:23.117 2010-05-21
You can use the functions:
day(date)
month(date)
year(date)
Also the Datepart() function might be of some use:
http://msdn.microsoft.com/en-us/library/ms174420(SQL.90).aspx
DECLARE #dToday DATETIME
SET #dToday = CONVERT(nvarchar(20), GETDATE(), 101)
SELECT #dToday AS Today
This returns today's date at 12:00am : '2010-05-21 00:00:00.000'
Then you can use the #dToday variable in a query as needed
CONVERT (date, GETUTCDATE())
CONVERT (date, GETDATE())
CONVERT (date, '2022-18-01')
I don't know why the others recommend it with varchar(x) tbh.
https://learn.microsoft.com/de-de/sql/t-sql/functions/getdate-transact-sql