By accident I came across an to my knowledge undocumented feature. One of the queries contained an instruction to convert a string to a Sybase date type:
SELECT CONVERT(DATE, '.', 105)
This works as expected with values like '16-11-2017', but one of the values by accident was . (point). This resulted in the (to me) illogical result of '2017-10-31' in November, now in December the result is somehow '2017-11-30'.
What is the meaning of the point character and why is the result the last day of last month?
Related
For an assignment I got, I have to create a request that list the flight numbers (varchar), the date of the flight and if there's a lay over (varchar, return NULL if not). However, the conditions to be put are that the date of the flight must be between September 1st 2005 and December 30th 2006 and the flight number must not end with 8 and/or 9.
I came with the following statement:
SELECT ID_VOL, DATE_DEP, ESCALE
FROM VOL
WHERE ID_VOL NOT LIKE ('%8','%9')
AND DATE_DEP BETWEEN '2005-09-01' AND '2006-12-30'
The query doesn't work. I've tried with only one of the ID_VOL condition and it works fine but it doesn't work when I put the second. It works if I put the same condition twice one for each number, but the assignment specify I can only do it with one condition/operator. So I'm kinda stuck on that one.
Your LIKE Expression is wrong.
SELECT ID_VOL,DATE_DEP,ESCALE
FROM VOL
WHERE ID_VOL NOT LIKE '%[89]' AND
DATE_DEP BETWEEN '20050901' AND '20061230';
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.
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
I'm working with date and float conversion on sql server and excel.
This is my query:
select getdate(),convert(float, getdate())
I get:
2014-11-21 16:38:49.973 41962,6936339506
If I copy this float number to Excel and I change cell type as date I get this value:
19-nov-2014
What's that? Why there is an offest of two days?
SQL server simply calculates the conversion of a date time to a float as the number of days since midnight on 01-Jan-1900 (i.e. select convert(DATETIME, 0) gives 1900-01-01 00:00:00.000)
Excel calculates a similar number, but the zero date is "00/01/1900". This is probably related to the fact that excel uses one based indexing, rather than the more common zero based indexing. The second day of difference comes from a well known bug whereby excel considers 1900 to have been a leap year.
Takeaway message: if you assume that excel is always behind by two days you'll be ok, except for dates on or before the 28th of February 1900.
Ok, I can't understand this thing.
A customer of mine has a legacy Windows application (to produce invoices) which stores date values as integers.
The problem is that what is represented like '01.01.2002' (value type: date) is indeed stored in SQL Server 2000 as 731217 (column type: integer).
Is it an already known methodology to convert date values into integers (for example - I don't know - in order to make date difference calculations easier?)
By the way, I have to migrate those data into a new application, but for as much I googled about it I can't figure out the algorithm used to apply such conversion.
Can anybody bring some light?
It looks like the number of days since Jan 1st 0000 (although that year doesn't really exists).
Anyway, take a date as a reference like Jan 1st 2000 and look what integer you have for that date (something like 730121).
You then take the difference between the integer you have for a particular date and the one for your reference date and you that number of days to your reference date with the DATEADD function.
DATEADD(day, *difference (eg 731217 - 730121)*, *reference date in proper SQLServer format*)
You can adjust if you're off by a day a two.