This question already exists:
Datatype conversion in SQL Server From Nvarchar to Datetime [closed]
Closed 3 months ago.
I was working on a timestamp query in SQL Server and have a column of data type nvarchar with values like:
01 02:52:12 , 01 03:00:25 ,01 03:08:09 ,01 03:35:20
Here
1 = Day of the month or date of the month
02:52:12 = Time of the day
All these are quoted as NVARCHAR.
My end goal is to extract DAY (using one query) in order to do cohort analysis, and then find the time difference between 2 timestamps.
In order to find DATEDIFF or any other date function.
I get an error
Conversion failed when converting date and/or time from character string
while importing the data from Excel; I tried to change the datatype but then it won't import the data.
I have used
SELECT DATEDIFF(Minute, [name 1], [name 2]) AS time
FROM table name
Related
We receive a csv file that has a column in this date format -- Wed Oct 14 08:00:00 CDT 2020, along with a column that has a count for each date/time
I am using an SSIS package to grab the file and import this data into a sql table, then I can format it the way I need to and then actually export the data in the format needed.
If there is a way to do this all within one SSIS package I am all ears but currently I am working on just getting the data into SQL and converted to the right format so that I can export it.
I need to get that file and convert that date format and split it up into two separate columns
One column will be just the date in this format 2020-10-14 00:00:00.000
One column will be just the time in this format 08:00:00.0000000
Updated to change the dates to match so it's not as confusing and also the error I am receiving when running the suggested code below.
Image of Error I'm recieving
Image of table with the data I am trying to convert
Image of table attributes
Screenshot of my screen when running a select * from the table I am pulling the data that I need converted
Screenshot of the error I receive when running the query by Aaron.
If this is the format it will always be in, and timezone is irrelevant, you can first try to convert it to a datetime, then you can extract the parts from that.
SET LANGUAGE us_english; -- important because not all languages understand "Oct"
;WITH src AS
(
SELECT dt = TRY_CONVERT(datetime, RIGHT(OpenedDateTime ,4)
+ SUBSTRING(OpenedDatetime, 4, 16))
--, other columns...
FROM [dbo].[VIRTUALROSTERIMPORT_Res_Import]
)
SELECT OpenedDateTime = CONVERT(datetime, CONVERT(date, dt)),
OnHour = CONVERT(time, dt)
--, other columns...
FROM src;
Results:
OpenedDateTime OnHour
-------------- ----------------
2020-10-14 08:00:00.0000000
If you need to shift from one timezone to another timezone, that's a different problem.
I was just showing the date formats, don't look so into the actual date examples I used. The time zone is irrelevant I just need the formats changed.
When I used The code Aaron suggested I got a conversion error: I'm assuming its because the columns are varchar in the table, but I cant get the dates to load as date formats bc SSIS keeps giving me truncated errors-- so I have to load it as varchar.
Below is the code I was running, I tweaked it to use the column and table names I am using.
SET LANGUAGE us_english; -- important because not all languages understand "Oct"
DECLARE #foo varchar(36) = 'Wed Oct 14 08:00:00 CDT 2020';
;WITH src(d) AS
(
SELECT TRY_CONVERT(datetime, RIGHT(#foo,4) + SUBSTRING(#foo, 4, 16))
)
SELECT OpenedDateTime = CONVERT(datetime, CONVERT(date, OpenedDateTime)),
onhour = CONVERT(time, OpenedDateTime)
FROM [dbo].[VIRTUALROSTERIMPORT_Res_Import];
This question already has answers here:
How to cast the DateTime to Time
(3 answers)
Closed 4 years ago.
I have a column in a SQL Server database of datatype DATETIME.
Currently the value is in this format: 2054-12-31T00:00:00.0000000
I want to convert this column values into this format : 2054-12-31T00:00:00
This conversion of value should happen while I select the column in SELECT query statement at run time
DATETIME as stored in SQL Server doesn't have any "format" associated with it - it's stored as a binary, 8 byte value.
In order to convert that binary value into a human-readable format, you need to check out the different styles for CONVERTing a DATETIME column into a string representation: https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-2017
You're probably looking for style #126 - so you can use this in your SELECT query:
SELECT
CONVERT(VARCHAR(50), YourDateTimeColumn, 126)
and that should do it
I have a timestamp column having values like the following in my database:
2017-01-01 00:00:58.538-05
2017-01-01 00:16:58.54-05
The data type is varchar since datetime, datetime2 formats did not work. I need to convert this column to datetime format now. I just discovered about datetimeoffset. :(
However, the conversion still does not work as is, and I get an error. datetimeoffset wants to see at least -05:0 not only -05.
DECLARE #datevar datetimeoffset = '2017-01-01 00:00:58.538-05';
SELECT #datevar
Msg 241, Level 16, State 1, Line 3
Conversion failed when converting date and/or time from character string.
What can be to solve this? Worst case scenario I tend use python to preprocess each file to add :0 at the end of each value, and then save the file, and then bulk upload every file in the folder at the end. However, each file has ~30 million rows, and there are 365 of them per year. So I really do not want to do that.
Again, the data is already uploaded, an easy and FAST way to do this would be appreciated. There are about 8 billion rows in the table now, so I do not know whether this can be done using SQL Server Management Studio.
You can do the :00 appending in sql itself
SELECT Cast(dates + ':00' AS DATETIMEOFFSET),
dates
FROM (VALUES ('2017-01-01 00:00:58.538-05'),
('2017-01-01 00:16:58.54-05')) tc (dates)
considering none of your dates has minutes part of timezone. If some dates has minutes part then it needs to be handled differently
In your table it should be something like
SELECT Cast(datecolumn + ':00' AS DATETIMEOFFSET),
datecolumn
FROM Yourtable
to update the table
update t set yourcol = Cast(yourcol + ':00' AS DATETIMEOFFSET)
FROM Yourtable
Then alter the table datatype to datetimeoffset and make sure you upload data with time part in offset
I have a problem inserting a date from a VB.net Program to a SqlServer2012 instance.
First here is how i generate the data (Vb.net)
ExitTime = CDate("1.1.1970 00:00:00").AddSeconds(currentField).ToLocalTime
We add this value to a stored procedure (Vb.net)
With comsql5.Parameters.AddWithValue("#ExitTime", ExitTime)
In the Sql Server stored procedure
#ExitTime datetime, [...]
[...]
Insert into [table] ([ExitTime]) VALUES (#ExitTime)
Here is the output of the exit time in the vb.net
Exit Time : 08/07/2014 2:06:31 PM
Here is the same row in the Sql server database
2014-08-07 14:06:31.000
What I would like to see in the database is 2014-07-08 14:06:31.00
Because another part in the program does a check on the field but as a String... and it does not match because it flip the month and day
EDIT: TO be clear, I can't change the other part that does the comparison as a string. I know this is a poor way to compare datetime.
Thank for your time
Have you tried using the Convert function?
SELECT CONVERT (VARCHAR, getdate(), 121);
Check this links for more information MSDN - CAST and CONVERT and SQL Server Datetime Format
This question already has answers here:
Closed 11 years ago.
To store only time from DateTime into SQL Server 2005. I am using vs.net 2010 and SQL Server 2005
Like this
MyCurrent DataType
id int
EnterDate DateTime
Date string
Time string
// I want to like this STORE [Future out put]
id .... EnterDate Date Time
1 .... 4/11/2011 10:25:00 4/11/2011 10:25:00
2 .... 4/11/2011 10:32:10 4/11/2011 10:32:10
3 .... 4/11/2011 10:41:37 4/11/2011 10:41:37
So how to store only time value and how define datatype in SQL Server?
You can get Only time like this
Select CONVERT(VARCHAR(8),GETDATE(),108) AS HourMinuteSecond
Refer Sql server DateTime for more
I guess that you want to have three columns, one with datetime stamp, one with date and one with time only. if thats the case then
SELECT DATEPART(dd/mm/yyyy,EnterDate) AS Date
from table
this will get you all the dates from date time stamp, you can accomodate this in your insert query
SELECT DATEPART(hh:mm:ss,EnterDate) AS Time
from table