SQL query that convert date

Dear expert

I need help with a query please, below query didn't work, well DatetimeInsert is having a format = 2018-10-11 00:24:24.04, if I simply just do a count with DECLARE @ProcessTIme date = 2017-11-11 it returns nothing, so I need to convert it, but I don't really know how.

Please help thanks.

use Prod
DECLARE @ProcessTime CONVERT(VARCHAR(10),[ProcessedTime],120) date = 2018-10-11;
SELECT Count (*)
FROM Invoice.Progress H
WHERE H.DoElectronicInvoiceToReceiver =1 AND H.DatetimeInsert = @ProcessTime 

Open in new window

LVL 1
WeTiAsked:
Who is Participating?
 
ste5anSenior DeveloperCommented:
I just need to convert a date to yyyy-MM-dd format [..]
Why? Read the posts again. There is a difference between value and format. You need only to CONVERT or CAST, when you don't have a DATE or DATETIME.

you did a adddate +1 that is as same as doing: H.DatetimeInsert > '2017-10-11' AND H.DatetimeInsert < '2017-10-12'
No, only the result is the same. My sample took your original post, where you used a parameter to define a single day for filtering. Whereas the second option is using only to date literals.

E.g. '2017-04-05' can be the 4th of May or the 5th of April. Whether this is correctly interpreted depends on your server and database settings. This is bad coding style.
Use unambiguous date literals. Thus use

H.DatetimeInsert > '20171011' AND H.DatetimeInsert < '20171012' 

Open in new window


Is this a limitation of MSSQL[..]
No, it's ANSI-SQL.

For filtering a DATETIME value for a DATE you have two options:
DECLARE @Sample TABLE
    (
        ID INT IDENTITY NOT NULL PRIMARY KEY ,
        PayloadDateTime DATETIME NOT NULL
            UNIQUE
    );

INSERT INTO @Sample ( PayloadDateTime )
VALUES ( GETDATE() + 0 ) ,
       ( GETDATE() + 1 ) ,
       ( GETDATE() + 2 ) ,
       ( GETDATE() + 3 ) ,
       ( GETDATE() + 4 );

DECLARE @FilterDate DATE = '20170101';

SELECT *
FROM   @Sample S
WHERE  S.PayloadDateTime >= @FilterDate
       AND S.PayloadDateTime < DATEADD(DAY, 1, @FilterDate);

SELECT *
FROM   @Sample S
WHERE  CAST(S.PayloadDateTime AS DATE) = @FilterDate;

Open in new window


The difference is  the query plan (which should perform equal on actual SQL Server versions):Capture.PNG
On older versions, the second query let to an unsargable predicate, thus it couldn't use indices on the involved column.
0
 
Geert GOracle dbaCommented:
your date needs to be inside quotes
otherwise it's just a calculation
0
 
WeTiAuthor Commented:
Like this?
SELECT Count (*)
FROM Invoice.Head H
WHERE H.DoElectronicInvoiceToReceiver =1 AND H.DatetimeInsert = '2017-10-11'

Open in new window

Doesn't work, you still need to convert it...
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Geert GOracle dbaCommented:
well off course you do
never make assumptions about a date format

https://docs.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql

H.DatetimeInsert =Convert(Date,  '2018-10-11 00:24:24.04', 121)

Open in new window

0
 
WeTiAuthor Commented:
How can I do it inside the DECLARE? and I want to format it from 2018-10-11 00:24:24.04 to only 2018-10-11 ignore the rest.
0
 
WeTiAuthor Commented:
Well... I could do this:
H.DatetimeInsert > '2017-10-11' AND H.DatetimeInsert < '2017-10-12'  but seriously better with convert...
0
 
Geert GOracle dbaCommented:
did you try it ?

H.DatetimeInsert =Convert(Date,  '2018-10-11', 121)
0
 
WeTiAuthor Commented:
I did but not working, returns 0 result.
0
 
Geert GOracle dbaCommented:
if returns a result, then it works
otherwise you'd get a syntax error

you could also check the data for that month:
https://docs.microsoft.com/en-us/sql/t-sql/functions/datepart-transact-sql

SELECT * 
FROM Invoice.Head H
WHERE H.DoElectronicInvoiceToReceiver =1 
  AND DatePart(year, DatetimeInsert) = 2017
  and  DatePart(month, DatetimeInsert) = 10

Open in new window

0
 
ste5anSenior DeveloperCommented:
You need to distinguish between a date as data (the data type) and the visual formatted output in any tool or program.

Date literals in T-SQL must be enclosed in single quotes. And can use different formats. But the problem is: Formats must be interpreted. Thus we use - good coding style - an unambiguous date format. The most common in T-SQL is 'yyyymmdd' .

DECLARE @ProcessDate DATE = '20181011';

Open in new window


Then, the data types DATETIME et al. having a time portion requires that you consider this. Cause the data type precedence leads to expansion of DATE to DATETIME:

DECLARE @GetDate DATE = GETDATE();
DECLARE @GetDateTime DATETIME = GETDATE();

SELECT IIF(@GetDate = GETDATE(), 1, 0) ,
       IIF(@GetDateTime = GETDATE(), 1, 0);

Open in new window


This means that when using a DATE as predicate on a DATETIME value, you need indeed to do a range check:

USE Prod;
GO

DECLARE @ProcessDate DATE = '20181011';

SELECT COUNT(*)
FROM   Invoice.Progress H
WHERE  H.DoElectronicInvoiceToReceiver = 1
       AND H.DatetimeInsert >= @ProcessDate 
       AND H.DatetimeInsert < DATEADD(DAY, 1, @ProcessDate);

Open in new window

0
 
WeTiAuthor Commented:
I just need to convert a date to yyyy-MM-dd format in a query and it ignore the value behind...  Ste5an you did a adddate +1 that is as same as doing: H.DatetimeInsert > '2017-10-11' AND H.DatetimeInsert < '2017-10-12'

Is this a limitation of MSSQL that it simply query the date as in format 2107-10-11 with the 22:22:22,44? What i want is: sql query ignore the 22:22:22,44 and only match the 2107-10-11.
0
 
WeTiAuthor Commented:
I would like to thank both for the help... now I could think about which query I prefer for the output, thank you both for the effort.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.