How can i get the hour between two datetime in sql? - sql-server

Below sql showing 57 hours, But,it's 44 hrs. How can i solve.
SELECT DATEDIFF(Hour,'2018-11-20 2:26:38.000','2018-11-22 11:00:29.367')

Use 24 hr format in both dates
SELECT DATEDIFF(Hour,'2018-11-20 14:26:38.000','2018-11-22 11:00:29.367')

Not prefixing the hours to be two digits looks like it could be the issue, making it unambiguously in the afternoon gives your result.
/*------------------------
SELECT DATEDIFF(Hour,'2018-11-20 14:26:38.000','2018-11-22 11:00:29.367')
------------------------*/
45
You're default Date Time format is probably 12 hour, and "2:…" is being treated as pm.
Using two digits for the hour should help.
/*------------------------
SELECT DATEDIFF(Hour,'2018-11-20 02:26:38.000','2018-11-22 11:00:29.367')
------------------------*/
57
(SQL Server has a lot of backward compatibility which may be triggered is the input is not precisely formatted, ISO Date/Time formats always use two digits for hours, minutes, and seconds.)
If you are using a client (rather than fixed code in a Stored Proc/Function/Trigger/…) parameterising your queries avoids this issue: pass data as Date-Time type directly without any need to convert into a string.

Related

How to convert custom string date format to desired output SQL

In SQL server, I have a date string that looks something like 09/08/2021. Representing dd/MM/YYYY. So in this case its 09 August 2021. Every method I attempt converting this explicitly into a date format automatically converts this to 2021-09-08. So SQL incorrectly converts to 08 September 2021.
SELECT TRY_CAST(Convert(VARCHAR(15),'09/08/2021',103) as date);
SELECT TRY_CAST(Convert(VARCHAR(15),'09/08/2021',111) as date);
Both incorrectly give:
2021-09-08
Desired result is:
2021-08-09
I've tried the various different versions of the above but keep getting the incorrect conversion. I don't really want to go down the road of changing the source data either.
I am amazed no similar question has been previously asked regarding this.
How can this be converted explicitly using functions in SQL Server?
As per the comments, it was the conversion that was the mistake. Adjusted to
TRY_CONVERT(DATE, '09/08/2021', 103)
This fixes the issue.

SQL Server query that returns data between two date times with format 01/07/2020 01:01:01 a. m

I've been having problems with a query that returns data between two date times, the query that I'm trying to fix is this one
pay.date BETWEEN '01/06/2020 00:28:46 a. m.' AND '01/06/2020 10:38:45 a. m.'
That query does not detect the a. m. part and if I have a payment at 10 am and 10 pm it will detect both payments as the t. t. part is not detected, I've been searching for a while now with no luck, thanks in advance :)
Do the filtering by an actual datetime type:
cast(replace(replace(pay.date, ' a. m.', 'am'), ' p. m.', 'pm') as datetime)
It might be better to use convert() so you can specify the proper format. If you can't supply the date literals in a readily convertible format then do a similar replace and cast on those too.
Use a literal format that is unambiguous and not dependent on runtime or connection settings. More info in Tibor's discussion.
In this case:
where pay.date between '20200601 00:28:46' and '20200601 10:38:45'
Notice that I assume June, not January - adjust as needed. Between is inclusive and be certain that you understand the limitations of the datatype for pay.date. If datetime, the values are accurate to 3ms. Verify that your data is consistent with your assumption about accuracy to seconds.

formatting date (saved in DB as datetime)

I have a bunch of records in SQL SERVER and i'm having an issue with one of the fields.
The datatype is datetime. The system was only inserting a date with no time in it, so '1/2/2017' - so when it was inserted in SQL SERVER it would show only as 1/2/2017 00:00:00. Now, what I'm trying to do is to display it just as it is saved in the DB.
I query the DB and display it like this...
If Not IsDBNull(dr("ReceivedOn")) Then
txtReceivedOn.Text = Format(dr("ReceivedOn"), "MM/dd/yyyy hh:mm tt")
End If
The mask on my masked textbox is liek this.....
00/00/0000 90:00 aa
When it does get displayd in the masked textbox is shows the date and 12:00 AM
1/2/2017 12:00:00 AM
Is there to get rid of it, and only show 0's instead of a incorrect time? However I'd like to only see a 12 hr time rather than 24.
You need to change the format specifier for hours from hh to HH
If Not IsDBNull(dr("ReceivedOn")) Then
txtReceivedOn.Text = Format(dr("ReceivedOn"), "MM/dd/yyyy HH:mm tt")
End If
On MSDN at the DateTime Custom Date and Time strings you can read
The "HH" custom format specifier (plus any number of additional "H"
specifiers) represents the hour as a number from 00 through 23; that
is, the hour is represented by a zero-based 24-hour clock that counts
the hours since midnight. A single-digit hour is formatted with a
leading zero.
Note that, with this format, the tt is meaningless.

SQL Server Convert datetime to mm/dd/yyyy format but still be a datetime datatype

I would like to keep my dates as datetime datatype by also be in MM/DD/YYYY format. I know how to do this by converting them to a varchar, but want to keep the datetime format. Can anyone help with this?
Currently I have tried
SELECT CONVERT(datetime, GETDATE(), 101)
which is not working...
There is a basic misunderstanding in your question. Repeat after me: Datetimes don't have a format.
It helps if you think of them as just an array of seven integers (year, month, day, hours, minutes, seconds, milliseconds) with certain constraints. That's not in any way accurate, but it helps to get the notion out of your head that something akin to 12/31/2015 is stored in your database.
Datetimes only get a format when (implicitly or explicitly) being converted to strings. You already know how to set the format when explicitly converting to string, now all that is left to do is to find the implicit conversion that is obviously bothering you and replace it with an explicit one.
Date and datetime Values stored in the database are NOT in any recognizable format. They are stored in binary (1s and 0s) in a proprietary format where one part represents the number of days since a defined reference date (1 jan 1900) in SQL server). and the other part represents the time portion of the value. (in sql server, its the number of 1/300ths of a second since midnight.)
ALL formatting of dates and date times, no matter what format you wish for, is done only after the values have been extracted from the database, before you see them on screen, in whatever application you are using.
You can find all the formats that the SQL Server convert function can use on this MSDN Convert Link

formatted date and time from postgresql

I am using server time for one of my process. For that I am taking date and time using postgresql. The time format I want is 2 digit day,month,hour,minute,second and 4 digit year (eg: 05/01/2015 16:05:30). I am using SELECT to_char(now()::timestamp,'DD/MM/YYYY HH24:MI:SS') I want to make sure that the number of digits for each will be as like I want. Because its very important for my processing. I have refered the following link Link. There it is saying, day of month (01-31) for DD's decription. Is there any possibility to get day as 1 instide of 01
Feel safe.
The table ad your link (http://www.postgresql.org/docs/9.4/static/functions-formatting.html#FUNCTIONS-FORMATTING-DATETIME-TABLE)
is right.
Numbers in date converted with to_char, are 0 padded.
Is there any possibility to get day as 1 instide of 01
You can use regular expression to remove leading 0:
select regexp_replace(to_char(now()::timestamp,'DD/MM/YYYY HH24:MI:SS'), '^0(\d)', '\1', 'g');

Resources