Get date in date format from SQL Server - 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.

Related

How to take summary from a sql data Table using sql query

I have a table with some data like subtotal,tax,Grand,Date
I want to take summary report from this data using Sql query.
when I use query its getting all the selected date's data (example: if start date 31-07-20, end date 01-08-20) then result showing each days details. My goal is to get summary of each days.
What I am getting:
What I am looking for:
SELECT SUM(Sub) as Sub, SUM(VAT) as VAT, SUM(Grand) as Grand, Date
FROM <WHATEVER>
GROUP BY Date
This is SQL 101...

Get date from int (YYYYMMDD)

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)

Day/Month/Year date changed to Month/Day/Year in SQL

I have an old Classic ASP application that was worked perfectly, recently it was moved to window server 2012 server with IIS 8.5, the problem is that I have a form to add record with Day/Month/Year format, when the date entered as 23/06/2015 in SQL will be added correctly as 23/06/2015 but when date entered as 01/06/2015 : 1st june 2015 in SQL will be added as 06/01/2015 Month/Day/Year which will be incorrect
I checked the location and date format for the server and it was dd/mm/yyyy, and the database didn't changed since the old server SQL 2008.
I searched the net and found the i need to change the IIS culture under GLOBALIZATION, but I don't know what to choose even if I chose my country still I am facing the same issue.
If you don't want to get into changing globalization and localization settings, the simple answer is to use the universal date input YYYYMMDD when inserting dates as strings.
Update
To expland on your comment: when changing the format of the date string in an insert/update query it does not change the format within the sql table itself.
e.g.
UPDATE [table]
SET [column] = '20150716 10:22'
WHERE Id = 7489
When viewed displays the same as the other records in the table:
Try this:-
https://www.webwiz.co.uk/kb/asp-tutorials/date-time-settings.htm
Add an entry into global.asa as below...
'When a session starts on the server the following code will be run
Sub Session_OnStart
'Set the server locale
Session.LCID = 2057
End Sub
Somethimes when nothing fix this problem (or a global solution could damage old scripts), A workaround that you could use is the convert function, to change the format in your stored procedure.
For Example:
select getdate() --prints 2018-10-05 12:24:43.597
select Convert(char(10),getdate(),103) --To force the date to be dd/mm/yyyy
And not leting the asp page to format the date, just print raw.

Use sql server Convert Function to convert hijri to gregorian date

I have simple table on my sql server and in my table have a date field,and into date field save a hijri date,i want use the sql server convert function to convert hijri date to gregorian date.
how can i do this?
i use this query in sql server:
update k
set time=CONVERT(datetime,GETDATE(),101)
and i insert this "1392/4/21" in time field ,and when convert sql server return this "2014/11/5",when i use online date convertor,and insert this "2014/11/5",convert date is "1393/2/14" !!is this the correct resualt?
Here is a sample how you could do it:
select convert(datetime, value, 131)
from (
values ('13/01/1436 9:54:59:767AM')
) samples(value)
There are several blogs about this, likethis one:
http://blogs.msdn.com/b/wael/archive/2007/04/29/sql-server-hijri-hijra-dates.aspx
and this one:
http://raresql.wordpress.com/2013/05/08/sql-server-how-to-convert-gregorian-dates-to-hijri-date-with-formatting/

Search By Date in SQL Server 2012

We just upgraded to SQL Server 2012 from 2005. While I'm a novice, something this simple couldn't be this difficult. I used to be able to pull data from a table based on the date vs date and time. As it now stands I have:
Select * from receipts_table where receipt_cancel_date = '2013-09-20'
before we upgraded this would work fine. How can I run this and actually get the desired results as I know there's receipts with a cancel date of 2013-09-20.
Thanx
If you are passing string for a date parameter, best format is ISO (yyyymmdd) format. Otherwise even though your string work in some servers it might not work in another depending on the culture of the server. ISO format is culture independent.
Also remove the time part from receipt_cancel_date column by converting it to a DATE (if DATETIME) for comparison purpose.
Try this:
Select * from receipts_table
where convert(date, receipt_cancel_date) = convert(date,'20130920')
Or use 120 style with your format:
Select * from receipts_table
where convert(date, receipt_cancel_date) = convert(date,'2013-09-20',120)

Resources