Get date from int (YYYYMMDD) - sql-server

By doing a mistake a few months ago I got myself in a situation, where I have to find a workaround for the following problem:
I am saving dates as integer in a common format inside my SQL Server database (YYYYMMDD). I want to have my select statement giving me the integer in a format, that looks like a date (or even is in a true date type) to the user, so I can use the received datatable directly as datasource for my DataGridView.
I do not want to use clientside formatting
Let's call the table myTable and the integer/date column myIntDate
myIntDate can be NULL
sample myIntDate-value : 20160803 for the 3rd of August 2016

Select cast(cast(20160729 as varchar(10)) as date)
Returns
2016-07-29
Or
Select cast(left(20160729,8) as date)

Related

T SQL Conversion failed when converting date and/or time from character string from VARCHAR(MAX)

I'm using SQL Server 2014. I have a date stored as varchar(MAX) in the format of:
2019-02-18
However, I want it in the British format dd/mm/yyyy (103).
This is my SQL:
SELECT CONVERT(DATE, DateField, 103) AS "JobStartDate"
FROM tblTest
However, I keep getting this error:
Conversion failed when converting date and/or time from character string.
What am I missing?
Update: The date is initially stored as varchar max as it is coming from a 3rd party system. I have no control over this and I completly understand this is the wrong format, but this is what I have been given.
I have a date stored as varchar(MAX)
There's your problem right there.
Not only you are using the wrong data type to store dates, you are also using max which is a known performance killer.
The solution to the problem is to alter the table and store dates in a Date data type - but first, you must look up all the objects that depends on that column and make sure they will not break or change them as well.
Assuming this can't be done, or as a temporary workaround, you must first convert the data you have to Date, and then convert it back to a string representation of that date using the 103 style to get dd/mm/yyyy.
Since yyyy-mm-dd string format is not culture dependent with the date data type, you can simply do this:
SELECT CONVERT(char(10), TRY_CAST(DateField As Date), 103) As [JobStartDate]
FROM tblTest
Note I've used try_cast and not cast since the database can't stop you from storing values that can't be converted to dates in that column.
You want to format the DateField column and not convert it to date.
So first convert it to DATE and then apply the format:
SELECT FORMAT(CONVERT(DATE, DateField, 21), 'dd/MM/yyyy') AS JobStartDate
See the demo.

Get date in date format from SQL Server

I have a table in SQL Server with a date column set to default date, however, when I fetch the data from the server using VBA I get the date column in string format. Is there any way I get the date as in date format?
Can you you advise how you are bringing in the data? EG from inbuilt connectors or via a recordset via VBA objects?
A quick hack which I wouldn't advise without really testing would be this :
Select DateDiff(DD, '1899-12-30', date_column) AS [column] from TABLE_x
What is this doing?
In Excel dates start 01-01-1900. When you change a date to 'General' you find it's actually a number with a format. The above date difference will give you the excel number version of the Date.
EG to get today 29/11/2019 which will be 43798 when cell format is 'General'.
Select DateDiff(DD, '1899-12-30', '2019-11-29') AS [column]
More info here
Non MS site explaining the situation
MS Office Info touching on the subject
Please note this basing your question on a SQL formatted date.

SQL DateTime Inserted Incorrectly From VB.NET

Have a table I'm logging information from a .NET program into.
The VB.NET app explicity dictates the format of the DATETIME string like below
responsedt = Date.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")
I then pass this into an INSERT statement that updates my table, however even though the entire setup of the SQL Server is en-GB (British English) the DateTime has gone in the following format:
2019-09-05 19:09:34.823
This was done yesterday so actually should be
2019-05-09 19:09:34.823
The day and month should be switched around, I have tried performing an update on the table post process to get it to update using the following code
FORMAT (xa.daterequested, 'yyyy-dd-MM HH:MM:ss.fff', 'en-gb')
How while this works in a SELECT statement it doesn't seem to work when I do the UPDATE statement.
It is not ideal to have to update all the records dates after the initial INSERT so a solution to either the .NET side of the issue or the SQL would be appreciated as its pickling my head.
You have 2 options to prevent the error from happening again:
Keep dates as date/time data types instead of converting them to strings.
Use formats that are not language or settings dependent. In SQL Server these would be YYYYMMDD hh:mm:ss.msss OR YYYY-MM-DDThh:mm:ss.mss (notice the T between date and time)
To correct the dates already inserted you could use the format codes in the CONVERT function.
UPDATE t SET
daterequested = STUFF( STUFF( StringDate, 5, 0, SUBSTRING(StringDate,7,2)), 9, 2, '')
FROM YourTable t
CROSS APPLY( SELECT CONVERT( varchar(25), '20190905 19:09:34.823', 121) AS StringDate) AS x;

Compare dates in MS SQL using Convert

I want to do the correct way to get a date from a #parameter that contains the complete datetime, that user gets from a calendar from TFS File.
In the select I would want to use something like CONVERT(varchar(10), #FechaHasta.Value, 120) AS DATE01 and then get the only date, throwing out the time from the parameter.
For the next step, I would want to compare it with another date in WHERE clause, having this code :
Then, I would want to make this work on for looking for between two dates, and the last one, throwing out the time from the datetime.
Thanks.
I am assuming #FetchHasta is a datetime
CONVERT(VARCHAR(10), #FetchHasta, 101)
is what gets you just the date part. i.e. 10/28/2014 1:10 PM would simply become 10/28/2014.
Source
EDIT: Alternatively, How to return the date part only from a SQL Server datetime datatype

How to set date format for the result of a SELECT statement in SQL Server

I know how to use CONVERT function in SELECT statement to change the format of the Date column:
SELECT
StationID
, CONVERT(varchar, [Date], 101) as Date
, Value
FROM my_table
But I was wondering if I can set the date format in general before running the SELECT statement, when I don't know the name of the date column in the following SELECT statement:
SELECT * from FROM my_table
Is any SET statement or other T-SQL that I can run before my SELECT statement so that I can change the Date format temporarily?
Thank you
No.
In particular, any date columns which you select are not actually formatted at all, but are instead returned down the wire as an actual piece of date data in a binary "format" which is used for dates. If you are seeing them formatted, it's because your client (either management studio or some other tool) is converting them to strings to display.
When you use SELECT *, there is obviously no way to tell SQL Server to do any conversions on any particular columns, so the data is going to be returned in whatever the data types of the underlying query returns. So regardless of whether your data types are really date or not, no manipulation is going to happen at that point anyway.
I'm pretty sure there's no way to do what you're asking. However, there are ways to format the date string when you output it using your programming language.

Resources