Different results between date and Cast As Date - sql-server

I'm new to the T-SQl world and this issue is puzzling me.
I'm running a query against a variable I have created which holds the date of the start of my financial year, which is 2014-04-01. The data I'm querying against holds the date as a date time field, so I have been using CAST AS DATE to remove the timestamp.
If I run this query in this fashion I receive 25 results:
select count(B.WorkOrderNumber) as AWOCount
from TSP1_Dev.AssetDataPortal.EllipseSiteList A left outer join
TSP1_Dev.AssetDataPortal.tblWorkOrdersActive B
on A.EquipmentLocation = B.EquipmentLocation
where
B.EquipmentLocation = 'BISHT'
and A.EquipmentClass = 'SW'
and right(rtrim(A.EquipmentDesc),4) = '(OU)'
and cast(B.CreationDate as Date) >= '2014-04-01';
However, on one occasion I forgot to cast the date, and when I did that I received 32 results:
select count(B.WorkOrderNumber) as AWOCount
from TSP1_Dev.AssetDataPortal.EllipseSiteList A left outer join
TSP1_Dev.AssetDataPortal.tblWorkOrdersActive B
on A.EquipmentLocation = B.EquipmentLocation
where
B.EquipmentLocation = 'BISHT'
and A.EquipmentClass = 'SW'
and right(rtrim(A.EquipmentDesc),4) = '(OU)'
and B.CreationDate >= '2014-04-01';
Can someone please tell me why? Apologies for my ignorance!
Neil

In the query you have string literals and from your comment I can only assume that it is Coldfusion that insert the string literal in your query. (I know nothing about Coldfusion but is there no way to use parameters instead?).
In SQL Server there is a setting for the order of date parts in strings. SET DATEFORMAT
What you see can be reproduced if you have the date format dmy.
The string literal 2014-04-01 is interpreted as yyyy-dd-mm when compared against a datetime. When the new data types date and datetime2 was added that behaviour was changed so string literal on the form yyyy-mm-dd will always be interpreted as just that regardless of set dateformat.
So when you cast to date the string literal is converted to the date 2014-04-01 but when you compare against a datetime the string literal is converted to 2014-01-04.
set dateformat dmy;
declare #T table(D datetime);
insert into #T(D) values
('20140101'),('20140201'),
('20140301'),('20140401'),
('20140501'),('20140601');
select count(*)
from #T
where D >= '2014-04-01';
select count(*)
from #T
where cast(D as date) >= '2014-04-01';
Result:
5
3
One way to avoid the problem for datetime is to use a string literal without dashes. 20140401 will be interpreted as yyyymmdd regardless of set dateformat.

Related

Extracting date from varchar with timezone in snowflake

I have some sample data in snowflake as follows;
created_at
----------
2022-06-10T18::35::57
2022-06-10T18::35::57
The datatype of this column is VARCHAR(16777216), I am trying to filter for the rows with date June 10,2022. Here is my query;
select *
from table
where to_date(created_at) = date('2022-06-10', 'yyyy-mm-dd');
But this gives me following error; Date '2022-06-10T18::35::57' is not recognized. If we replace to_date by try_to_date then we get 0 rows. Unfortunately I can't go to backend and change the properties of the table. Therefore, I need to resort to sql datetime functions.
Can I please get help here, on how to fix above errors? thanx
Using LEFT to get date part:
where to_date(LEFT(created_at, 10), 'YYYY-MM-DD') = date('2022-06-10', 'YYYY-MM-DD');
There are two issues here -
1 - It is not a date but a timestamp
2 - usage of '::' rather then standard ':' in time portion
Below will not work as its not a date -
with date_cte(created_at) as
(select * from values
('2022-06-10T18::35::57'),
('2022-06-10T18::35::57'))
select to_date('2022-06-10T18::35::57') from date_cte;
100040 (22007): Date '2022-06-10T18::35::57' is not recognized
Using timestamp will not work too due to '::' in time portion
with date_cte(created_at) as
(select * from values
('2022-06-10T18::35::57'),
('2022-06-10T18::35::57'))
select to_timestamp('2022-06-10T18::35::57','yyyy-mm-dd"T"hh24:mi:ss') from date_cte;
100096 (22007): Can't parse '2022-06-10T18::35::57' as timestamp with format 'yyyy-mm-dd"T"hh24:mi:ss'
Below takes care of both -
with date_cte(created_at) as
(select * from values
('2022-06-10T18::35::57'),
('2022-06-10T18::35::57'))
select to_timestamp('2022-06-10T18::35::57','YYYY-MM-DD"T"HH24::MI::SS') from date_cte;
TO_TIMESTAMP('2022-06-10T18::35::57','YYYY-MM-DD"T"HH24::MI::SS')
2022-06-10 18:35:57.000
2022-06-10 18:35:57.000
Now to compare - just convert in desired format using to_char -
with date_cte(created_at) as
(select * from values
('2022-06-10T18::35::57'),
('2022-06-10T18::35::57'))
select * from date_cte
where to_char(to_timestamp('2022-06-10T18::35::57','YYYY-MM-DD"T"HH24::MI::SS'),'yyyy-mm-dd')='2022-06-10';
CREATED_AT
2022-06-10T18::35::57
2022-06-10T18::35::57
One possible way is to do this:
SELECT *
FROM table
WHERE CAST(SUBSTRING(created_at, 0, 11) as date) = '2022-06-10'

How to convert a column value which is like 20220401 of number datatype to 2022-04-01 in snowflake

I have a column of number data type in snowflake that stores values like 20220401, now I need to convert it into date format. Could someone please help
You can use to_date, but it works on char datatype, so first need to convert your column to char/string.
with data_cte(col1) as
(select 20220401::number)
select to_date(col1::string,'yyyymmdd') from data_cte
select to_date('02/14/2014', 'MM/DD/YYYY');
create table number_to_date(numbers integer);
insert into number_to_date values (20220401);
select to_date(to_char(numbers),'YYYYMMDD') from number_to_date;
More details: https://docs.snowflake.com/en/sql-reference/functions/to_date.html#examples
Yet more way's of seeing/saying the same thing:
to_date allows you to define format string
select column1 as raw_str,
to_date(column1, 'yyyymmdd') as as_date
from values
('19900101'),
('20220511');
gives:
RAW_STR
AS_DATE
19900101
1990-01-01
20220511
2022-05-11
if you have string that sometimes are not following the format pattern you might want to use try_to_date as this will not generate an error:
select column1 as raw_str,
try_to_date(column1, 'yyyymmdd') as as_date
from values
('19900101'),
('not a date'),
('20220511');
RAW_STR
AS_DATE
19900101
1990-01-01
not a date
null
20220511
2022-05-11
But given you said "number input"
select column1 as raw_number,
to_date(column1::text, 'yyyymmdd') as as_date
from values
(19900101),
(20220511);
now we have numbers, but to_date wants text, so we cast it to text, so the parser can be used.
giving:
RAW_NUMBER
AS_DATE
19,900,101
1990-01-01
20,220,511
2022-05-11
Now if you use TO_TIMESTAMP which accepts the same format string, the naked number by itself it will think you mean epoch seconds, and give you wacky tiny number, like:
select column1 as raw_number,
to_timestamp(column1) as as_timestamp
from values
(19900101),
(20220511);
RAW_NUMBER
AS_TIMESTAMP
19,900,101
1970-08-19 07:48:21.000
20,220,511
1970-08-23 00:48:31.000
which is all to say, cast it to string via ::text or the likes, and specify your format, so that you can handle yyyymmdd or yyyyddmm or how every your data is.

Error converting string, '2015-08-23', to date

I am trying to clean my data prior to loading into PowerBI to create the visuals. I have created my query as:
CREATE VIEW Project2 AS (
Select
p.playerID as ID1,
p.birthYear,
p.birthMonth,
p.birthDay,
p.birthCity,
p.deathYear,
p.nameFirst,
p.nameLast,
p.nameGiven,
p.weight,
p.bats,
p.throws,
p.finalGame,
b.*
from dbo.People as p
LEFT JOIN dbo.Batting as b
ON p.playerID=b.playerID
and b.G >= 50
WHERE (p.finalGame is null or p.finalGame >= 2018));
This works great until attempting to load the view into PowerBI I get this error:
DataSource.Error: Microsoft SQL: Conversion failed when converting the varchar value '2015-08-23' to data type int.
Details:
DataSourceKind=SQL
DataSourcePath=laptop-o4rhi9q7;Baseball
Message=Conversion failed when converting the varchar value '2015-08-23' to data type int.
ErrorCode=-2146232060
Number=245
Class=16
I can't figure out where /how to utilize cast(p.finalGame as date) in correct syntax, any ideas?
You must use a proper date literal for this, which means it must be enclosed in quotes, and ideally use a non-ambiguous format
WHERE (p.finalGame is null or p.finalGame >= '20180101'));
If finalGame is actuall varchar you would need to convert it using an appropriate conversion type. For example
WHERE (p.finalGame is null or CONVERT(date, p.finalGame, 102) >= '20180101'));
That may not be the correct format number, the full list is here.
I urge you to change the column type to date in the first place.
If the data for p.finalGame = '2015-08-23', you'll need to cast that as a date in your WHERE clause.
CREATE VIEW Project2 AS (
Select
p.playerID as ID1,
p.birthYear,
p.birthMonth,
p.birthDay,
p.birthCity,
p.deathYear,
p.nameFirst,
p.nameLast,
p.nameGiven,
p.weight,
p.bats,
p.throws,
p.finalGame,
b.*
from dbo.People as p
LEFT JOIN dbo.Batting as b
ON p.playerID=b.playerID
and b.G >= 50
WHERE (p.finalGame is null or cast(p.finalGame as DATE) >= 2018));
Based on the error message, you date appears to be a string of the form 'yyyy-mm-dd', not a true date type or an integer year. I believe your fix is to quote your year. Either
WHERE (p.finalGame is null or p.finalGame >= '2018');
or
WHERE (p.finalGame is null or p.finalGame >= '2018-01-01');
If your year is a variable, convert it to a string with something like CONVERT(VARCHAR(10), #Year). (This assumes a 4 digit value.)
As already noted, a better approach (if you have the ability to change the schema) is to redefine finalGame as a true DATE or DATETIME type. Then you could compare p.finalGame >= '2018-01-01' or p.finalGame >= DATEFROMPARTS(#Year, 1, 1).

Unexpected result when converting json UTC to timestamp and then date

We receive data in json format with timestamp represented in UTC. When convert json to timestamp_tz field it casts successfully (SQL is executed in Australia : +11h), however when the same result is converting back to date it refers back to UTC and returns result that is not matching timestamp_tz date. This turns out to be issue when using with view and try to add where clause xxx::date = 'yyyy-mm-dd'
Example:
select jj:TransactionDatetime::TIMESTAMP_TZ full_date,
jj:TransactionDatetime::TIMESTAMP_TZ::date round_date,
jj:TransactionDatetime::TIMESTAMP_TZ::date = '2020-11-14' is_it_same_date
from
(
select parse_json('
{
"TransactionDatetime": "2020-11-13 23:26:31+00"
}'
) jj ) dd
Result
FULL_DATE,ROUND_DATE,IS_IT_SAME_DATE
"2020-11-14 10:26:31.000","2020-11-13","false"
FULL_DATE is 14-Nov-2020 with time potion - that is converted from the original json UTC timestamp
ROUND_DATE is supposed to represent date only portion from FULL_DATE. It does that but using UTC representation and this way removes one day.
As result if I have view on top of that data users start using date in "where" clause and receive unexpected result
I expect that ROUND_DATE should be "2020-11-14"
I think that is unexpected behaviour that needs fixing at snowflake.Certainly there is workaround but should work natively.
Thank you
Here:
alter session set timezone = 'Australia/Sydney';
select CURRENT_TIMESTAMP(); -- returns 2020-11-24 23:44:24.013 +1100 as of now
select '2020-11-13 23:26:31+00'::TIMESTAMP_TZ as full_date; -- returns 2020-11-13 23:26:31.000 +0000
select '2020-11-13 23:26:31+00'::TIMESTAMP_TZ::date as date_only; -- return 2020-11-13
select '2020-11-13 23:26:31+00'::TIMESTAMP_LTZ as full_date; -- returns 2020-11-14 10:26:31.000 +1100
select '2020-11-13 23:26:31+00'::TIMESTAMP_LTZ::date as date_only; --returns 2020-11-14

Convert from DateTime to INT

In my SSIS package, I have to converting values from DateTime to a corresponding INTEGER value. The following sample has been provided.
Any ideas as to how I can convert these?
DATETIME INT
--------- ----
1/1/2009 39814
2/1/2009 39845
3/1/2009 39873
4/1/2009 39904
5/1/2009 39934
6/1/2009 39965
7/1/2009 39995
8/1/2009 40026
9/1/2009 40057
10/1/2009 40087
11/1/2009 40118
12/1/2009 40148
1/1/2010 40179
2/1/2010 40210
3/1/2010 40238
4/1/2010 40269
5/1/2010 40299
6/1/2010 40330
EDIT: Casting to a float/int no longer works in recent versions of SQL Server. Use the following instead:
select datediff(day, '1899-12-30T00:00:00', my_date_field)
from mytable
Note the string date should be in an unambiguous date format so that it isn't affected by your server's regional settings.
In older versions of SQL Server, you can convert from a DateTime to an Integer by casting to a float, then to an int:
select cast(cast(my_date_field as float) as int)
from mytable
(NB: You can't cast straight to an int, as MSSQL rounds the value up if you're past mid day!)
If there's an offset in your data, you can obviously add or subtract this from the result
You can convert in the other direction, by casting straight back:
select cast(my_integer_date as datetime)
from mytable
select DATEDIFF(dd, '12/30/1899', mydatefield)
Or, once it's already in SSIS, you could create a derived column (as part of some data flow task) with:
(DT_I8)FLOOR((DT_R8)systemDateTime)
But you'd have to test to doublecheck.

Resources