SQL Finding the date difference when two formats are different - sql

I have a date stored in format like
18/1/2012 18:51:35
I have to find the difference in days between one date and current date. To get current date i am using getdate() which returns the below
2015-10-30 10:01:25.493
Currently i am using charindex and substring function to make to formats equal in datediff(). Is there a better way

I think this has been answered here:
Converting a date in MySQL from string field
where (i think) you basically would just, (haven't tested :/)
datediff(STR_TO_DATE(your_column_to_convert, '%d/%m/%Y %H:%M:%S'), CURRENT_DATE())

Assuming you using SQL Server, as the GETDATE() and CHARINDEX() are specific to MSSQL.
If the date 18/1/2012 18:51:35 is stored in VARCHAR, you could probably use
DECLARE #DateFromTable VARCHAR(20)
SET #DateFromTable = '18/1/2012 18:51:35'
SELECT DATEDIFF(DAY, CONVERT(DATETIME,#DateFromTable, 103), GETDATE());
Refer to Cast and Convert functions here.

Below is the MSSQL code which you are looking for.
DECLARE #column_date VARCHAR(50)
SET #column_date = '18/1/2012 18:51:35'
SELECT DATEDIFF(DAY,CAST(CONVERT(DATETIME,#column_date,103) AS DATETIME),GETDATE())
Try replacing your column with #column_date.
Hope it will help you.

Related

How can I switch the Month and Day when the column format is M/D/Y?

I am not sure if this has already been answered but I could not find anything.
I am trying to convert the column of dates from MM/DD/YYYY to DD/MM/YYYY.
I must confess that I don't even know where to begin on this. I was thinking I might try an update statement but I am unsure as to how to format it. Any help would be appreciated.
You need to do two level of conversation :
select convert(varchar(12), convert(date, datecol, 101), 103)
In general, you need to fix the data-type (if that is in your hand) instead of doing conversation in SQL. Date formatting should be done at the presentation layer.
You can try this by fixing the data type in the actual table. For displaying purpose either on a webpage, reports or whatever.
SQL Server provides a number of options you can use to format a date/time string. In select, you can try one of the suggested methods as here.
For all these conversions you need to pass the date values in proper data type which may be the date or date-time.
Here is one of the examples of your illustration.
declare #DateInString varchar(20) = Cast(getdate() as Varchar(20))
select convert(varchar(12), convert(date, #DateInString, 101), 103)
You could also do Select Format(datecol, 'dd/MM/yyyy')
This will return your datetime field as a varchar - you should then be able to insert that into your target.

SSMS 2012: Convert DATETIME to Excel serial number

I can't seem to find an answer to this anywhere --- I want to convert a datetime in SQL to the excel serial number.
I'm essentially looking for the DATEVALUE function from excel but for use in SQL
Any ideas on how to do this? thanks
Assuming the desired date is 2016-05-25
Select DateDiff(DD,'1899-12-30','2016-05-25')
Returns
42515
If you want the time portion as well
Declare #Date datetime = '2016-05-25 20:00'
Select DateDiff(DD,'1899-12-30',#Date)+(DateDiff(SS,cast(#Date as Date),#Date)/86400.0)
Returns
42515.8333333
I ran into this issue and found the most elegant solution to be the following (as others have mentioned, adding the +2 is essential due to the differences between SQL and Excel dates):
Assuming the "Column" is a DateTimeOffset:
SELECT CAST(CAST(COLUMN_TO_BE_CONVERTED as datetime)+2 as float) as EXCEL_DATE_FLOAT
If the column is not already a DateTimeOffset and is just a DateTime, you would not need the double cast; you'd just need something like:
SELECT CAST(COLUMN_TO_BE_CONVERTED+2 as float) as EXCEL_DATE_FLOAT
The resultant float value in either case is what excel recognizes as the date and time and you can easily extract what you need from there. Upon manually verifying the results in excel, I confirmed that the serial numbers matched the date and time exactly as I expected.
You just need to convert your datetime value to int and add 1:
SELECT CONVERT(INT,YourDate) + 1
FROM dbo.SomeTable;
With collation: SQL_Latin1_General_CP1_CI_AS
you should use
Select DateDiff(DD,'18991230','20160525')
Returns
42515
You could replace '20160525' for getdate() or you date field

T-SQL Dates using Convert() function?

I am bit confusing here?
declare #date1 datetime = '2016-01-21 14:10:47.183'
I want to convert '2016-01-21 14:10:47.183' To '21-01-2016'
when I tried: select convert(date,#date1,105)
I am getting: 2016-01-21
But with: select convert(varchar(10),#date1,105)
I am getting: 21-01-2016
Why I am not having same results with above code?
Why should I convert to varchar?
Thanks in advance
This is just presentation matter and should be done in application layer. If you cannot do it in application you could use FORMAT (SQL Server 2012+):
declare #date1 datetime = '2016-01-21 14:10:47.183'
SELECT FORMAT(#date1, 'dd-mm-yyyy');
LiveDemo
Why I am not having same results with above code?
select convert(date,#date1,105)
-- DATETIME -> DATE
-- vs
select convert(varchar(10),#date1,105)
-- DATETIME -> VARCHAR(10) using specific style
If you only to skip time part use SELECT CAST(#date1 AS DATE) and do not bother how it is presented. It is still DATE.
To sum up: in SQL query use DATE as date, in application display it with desired format.
The reason why is because once you put a value in a datetime column (or date or any of the other variations on date-time datatypes) in SQL Server. SQL Server ceases to think of that date as having any particular format. It translates it into numbers, and stores it that way internally.
So when you select a date from a date time column, SQL Server displays it in the default format that you have selected based on your environment/local settings.
If you want to display it in any other format, you have to first convert it to a string, because as far as SQL Server is concerned, dates don't have formats. They are just numbers. The 21st day of March is the 21st day of March, whether you write it as 3/21 or 21/3.
So when you try to convert a date to a date with a different format, SQL Server just ignores you because dates don't have formats. However, if you want to convert that date to a string, SQL Server will be happy to help you display that string in any format you like.
Hope this helps, but sounds like some further research into how SQL Server stores dates would help your understanding.

Convert TEXT to Date in SQL Server 2012

I have a TEXT in this format 31/10/15.
How do I convert this into a DATE format?
As I need to let the user search from data using a date range.
example: From 15/7/13 to 31/10/15
Or is there a way to so without converting to date?
You can use CONVERT() for this:
DECLARE #d VARCHAR(50) = '31/10/50'
SELECT CONVERT(DATE, #d,3)
Note that with a 2-digit year SQL Server will make the year start with '19' for 50 and up, and 49 and below will be '20'
Storing as a DATE field will allow easier comparisons, otherwise you'll have to perform this conversion at each step.
Use CONVERT; example:
SELECT [Date] = CONVERT(date, '31/10/15', 3);
And yes, it's possible to search dates in the same format as the examples you provide, but don't do that – use the proper data types in both your queries and your table columns.

how to convert datetime value to another date only format?

my datetime field looks like this:
2011-02-07 06:51:32.000 (yyyy-mm-dd)
User input is in this format:
02-07-2011 (only the date)
This should be converted to:
02-07-2011 00:00:00.000
i tried CONVERT:
CONVERT(VARCHAR(10),myDateTimeField,113)
but this only works with date values not with datetime.
please help!
I used the following function:
CONVERT(datetime,Cast(myDateInput AS Char (10)), 105)
thanks for all your answers! and my apoligizes for changing my question!
select convert(varchar, myDateTimeField, 105) ...
Source: http://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/ and http://msdn.microsoft.com/en-us/library/aa226054(v=sql.80).aspx
Crystal has a function called Date; it takes either YYYY, MM, DD or a datetime as arguments and returns a date.
So I would suggest you make a formula to change the date to a datetime and display it.
date( mytable.mydatetimevar )
You should then be able to format this date as you want with the normal date field formatting options.
An alternative method of doing it is using Crystal's DatePart function to pull out the month, year, day and then restructure as required.
Try this:
select DATEPART(mm, myDateTimeField) + '-' +
DATEPART(dd, myDateTimeField) + '-' +
DATEPART(year, myDateTimeField)
I don't think it's surprising that a DATETIME field carries time information ;)
In fact, you shouldn't alter the field itself, as you would lose the database engine's ability to use date/time related functions with this field.
(the format used is ISO 8601 which helps the engine to easily identify the single parts)
As others said, it's good practice to format the date when you are retrieving it, either in your SELECT statement or in your application. (for more tips regarding this we would indeed need the information what sql server and what appliccation language you are using).
try converting with Style ID - 110
CONVERT(VARCHAR(10),myDateTimeField,110)
Result will be: 02-07-2011