How to convert date from decimal to date - sql

I have this in my Excel file that I receive : 1/1/1980 2:34:52 PM and after importing it's now stored as 29221.6075462963.
The database field is a nvarchar (200). I need a query that just shows exactly like excel : 1/1/1980 2:34:52 PM.
I am using SQL server 17.

In general, you can use something like this:
select dateadd(second,
floor((v.edt - floor(v.edt)) * 60*60*24),
dateadd(day, floor(v.edt), '1899-12-30')
)
from (values (29221.6075462963)) v(edt);

Related

How can I have the right format of time with SQL Server?

Hello I have this column :
date1
6/19/2019 3:10:12 PM
I wanted just to extract the time so I used the following query :
select try_parse([date1] as time using 'en-US') from myTable
But the problem is I got this :
15:10:12.0000000
whereas I would like to just have this :
15:10:12
How can I solve this ?
Thank you very much !
You can use FORMAT:
SELECT FORMAT(try_parse([date1] as datetime using 'en-US'), 'HH:mm:ss') from myTable;
You can try
SELECT convert(varchar(max), try_parse('6/19/2019 3:10:12 PM' AS time USING 'en-US'), 108)
FROM mytable;
if you want the result as a string for displaying purposes. (But you shouldn't store a date/time as string. Use the proper data types.)

SQL Server Query: Convert to dd/mm/yyyy from datetime

I'm querying a max datetime to bring in the most recent record from another table surrounding a common ID coloumn. I would like to however format this datetime in the standard UK dd/mm/yyyy format. How would I do so? I just cant seem to get this...
Here is my code:
(SELECT TOP 1 MemberPayments.CoverFinishDay
FROM
Members LEFT JOIN MemberPayments
ON Members.MemberID = MemberPayments.MemberID
AND CoverFinishDay = (
SELECT MAX(CoverFinishDay)
FROM MemberPayments
WHERE Members.MemberID = MemberPayments.MemberID
))
CoverFinishDay is stored in standard american datetime. currently the query works, just in the wrong format. I need the output in dd/mm/yyyy
This will help you:
SELECT CONVERT(VARCHAR, GETDATE(), 105)
This is for SQL Sever
If you are using SQL Server 2012 you can convert any date using FORMAT function. In your case you can do like this:
SELECT FORMAT (MemberPayments.CoverFinishDay, 'd', 'en-GB')
For mor info you can access the page: http://msdn.microsoft.com/en-us/library/hh213505(v=sql.110).aspx

Converting a numeric value to date time

I am working in SQL Server 2012. My date column in a data set looks like this: 41547. The column is in nvarchar (255). I want to convert it to something like this: yyyy-mm-dd hh:mm:ss (Example: 2013-09-14 12:23:23.98933090). But I can not do this. I am using following code:
select convert(datetime, date_column, 6)
But this is giving following error:
Msg 241, Level 16, State 1, Line 1 Conversion failed when converting
date and/or time from character string.
What am I doing wrong?
Your date is actually a numeric value (float or integer), stored in a char column. So, you need to convert it to a numerical value (in this case, to float) first, like:
select convert(datetime, CONVERT(float,date_column))
A value of 41547.5 will result in:
`2013-10-02 12:00:00`
The style argument, in your case 6 is only necessary when converting from or to char-types. In this case it is not needed and will be ignored.
NB: The float value is the number of days since 1900-01-01.
e.g. select convert(datetime, CONVERT(float,9.0)) => 1900-01-10 00:00:00; the same as select dateadd(day,9.0,'1900-01-01') would.
The decimal part of the number also equates to days; so 0.5 is half a day / 12 hours.
e.g. select convert(datetime, CONVERT(float,.5)) => 1900-01-01 12:00:00. (Here our comparison to dateadd doesn't make sense, since that only deals with integers rather than floats).
There is an easier way to do it as well.
select convert(date,cast (date_Column+ 19000000 as nvarchar(10)))
as date_Column_Formated
from table_Name
I have just found the way to do this.
First I have to covert the nvarchar to int then I have to convert it to date time. I have used following code:
Select convert(datetime, (convert (int, [date_column])), 6) as 'convertedDateTime' from mytable
6 format is: "dd mon yy"
Like this: SELECT convert(datetime, '23 OCT 16', 6)
Other formats will cause your error
SELECT CONVERT(DATETIME,CONVERT(INT,date_column))

Parsing date in sql query

I am using SQL Server 2005.
My Trade_Date column is of datatype datetime.
It has values as: 2/12/2013 11:59:00 PM , 2/13/2013 11:59:00 PM
i.e. different dates with time.
I want to compare date [only date] in where clause.
I am trying with following query:
select *
from foclosing
where CONVERT(varchar(11), Trade_Date) like '2/12/2013 %'
or
select *
from foclosing
where Trade_Date = CONVERT(datetime, '2/12/2013')
but both of these queries are not working.
What can be the issue?
select * from foclosing
where Trade_Date >= '20130212' AND Trade_Date < '20130213'
Use yyyymmdd which is safe for SQL Server
Don't apply functions to columns, then compare (your 1st query)See mistake 2 here: https://www.simple-talk.com/sql/t-sql-programming/ten-common-sql-programming-mistakes/
Don't compare locale-based dates as varchar (your 1st query)
Trade_Date has a time element which is why your 2nd query fails
In summary, a date conversion should not be used for examples like this: datetime is a range

How to convert date and time in SQL Server

I have the following columns in a table:
Signed_In_Date Signed_Out_Time
11/1/2005 12:00:00 am 11/1/2005 10:27:00PM
I would like to convert them to the following output:
Signed_In_Date Signed_Out_Time
11/1/2005 10:27:00PM
Is there a function or conversion code in SQL Server that would do it?
Assuming that the columns you're referring to are DATETIME columns, I would use the code below:
Date Only
SELECT CONVERT(VARCHAR(10), GETDATE(), 101)
Time Only
SELECT LTRIM(RIGHT(CONVERT(VARCHAR(20), GETDATE(), 100), 7))
You can see the queries in action / play with them here.
For Sign_In_Date Use
select CONVERT(VARCHAR(10),'11/1/2005 10:27:00PM',108)
Ouput:
11/1/2005
For Sing_Out_Time
declare #time time
set #time=cast('11/1/2005 10:27:00PM' as Time)
select convert(varchar(10),#time,100)
Output:
10:27PM
try that :
select CONVERT(VARCHAR(10),Signed_Out_Time,108) ,-- 108 is d/M/yyyy if you want mm/dd/yyy you should use 101
CONVERT(VARCHAR(8),Signed_In_Date,103)
You can use CONVERT to change datetime format to your own desirable format:
SELECT CONVERT(VARCHAR(10),Signed_In_Date,101) as 'Signed_In_Date',
CONVERT(VARCHAR(10),Signed_Out_Time,108) as 'Signed_Out_Time';
For more date format, go over this link:
http://www.sql-server-helper.com/tips/date-formats.aspx