Sql Convert Text Field To Date Field - sql

I'm importing an access database to sql.
The original access database has a date field that imports nicely, but the time field is text (10:00 AM, for instance).
I have over 4000 records and assume there is a way to convert 10:00 AM to 10:00:00.000 (or 07:30 PM to 19:30:00.000, etc...) so that it can be combined with the pre-existing date field (which is now like 2011-11-11 00:00:00.000).
Also, if it's easy to do the conversion and concatenation in the same process, please note.

to convert the time from am or pm 10:00 PM format into time format 10:00:00.000:
select cast(timestring as time(7))

look this:
declare #timeField as varchar(10)
set #timeField = '07:30 PM'
declare #dateField as varchar(10)
set #dateField = '1900-01-01'
select CONVERT(datetime,#dateField + ' ' + CAST(CONVERT(time, #timeField ,121) AS VARCHAR(11)),121)

In your import scripts (I assume you use some sort of SQL to E.T.L your data from Access to SQL server), you can use the convert function as such:
declare #t as time = Convert(time, '10:00PM' )
print #t -- prints 22:00:00.0000000
OR
declare #t as time = Convert(time, '10:00AM' )
print #t -- prints 10:00:00.0000000
And of course if you want to control the precision as per your example:
Convert(time(3), '10:00PM' ) -- prints 22:00:00.000

Related

Removing millisecond from datetime throwing error

I have this code in SQL Server 2016
SELECT CONVERT (DATETIME, CONVERT(varchar(8), ExpiryDate))
I get this result:
ExpiryDate
------------------------
2020-08-03 00:00:00.000
How can I remove the .000 (the milliseconds part)?
Expected result should be:
2020-08-03 00:00:00
Please help
By not using a datetime, which is accurate to 1/300th of a second. Instead define your value as a datetime2(0), which is accurate to 1 second (due to having a precision of 0 on milliseconds):
SELECT CONVERT(datetime2(0),ExpiryDate)
FROM ...
You are confusing the internal format and the display format. If you want the value formatted in a particular way, then format it explicitly:
select CONVERT(VARCHAR(19), expirydate, 121)
You can add this into the table as a computed column:
alter table t add expirydate_display as (CONVERT(VARCHAR(19), expirydate, 121))

can datetime type of sql server hold time only?

I'm trying to store time in format
05:00 PM
in database. but when i insert the data it automatically stores date as well like
2016-07-20 17:00:00.000
All i want only
17:00
in database
You cant store Time only in a datetime field it stores default date,Use TIME data type if you are on 2008..
declare #t datetime
select #t=cast(getdate() as time)
print #t
---Jan 1 1900 9:17AM
declare #t time
select #t=cast(getdate() as time)
print #t
---09:17:48.3330000
for sql 2008
select cast(getdate() as time)
--09:17:25.4400000
for 2005
select convert(varchar(10),getdate(),108)
---09:17:33
your exact format
select cast(datepart(hour,getdate()) as varchar(10))+':'+ cast(datepart(minute,getdate()) as varchar(10))
---9:17
You also can use FORMAT to store only time (From SQl 2012)
select FORMAT(GETDATE(),'HH:MM')
--09:07
First, as suggested in the comments, you should use time type instead of datetime.
The format of time in SQL Server is : hh:mm:ss or hh:mm:ss.nnnnnnn. If you want time to be in AM PM format just use this :
CONVERT(varchar(15),CAST('17:00:00.000' AS TIME),100)

how to convert the 24 hr to 12 hr in sql

I have column of 24 hr and i need to change it to 12 hr, Please help .
Start time
174300
035800
023100
The result should be
Start time
05.43 PM
03.58 AM
02.31 AM
Use STUFF function to convert string to Time format
SELECT CONVERT(VARCHAR,CAST(STUFF(STUFF(ColumnName,3,0,':'),6,0,':') AS TIME),100)
Using one of the examples above - the following will work.
You need to split the data into hours/minutes and cast it to time format, than convert it to the relevant type:
declare #data int
set #data = 174300
select convert(VARCHAR(15),cast(cast(left(#data, 2 )as varchar(2)) + ':' + cast(substring(cast(#data as nvarchar(6)), 3,2 )as varchar(2) ) as time),100)
Don't store time as varchar, instead alter your table and change the column type to datetime.
SELECT right(convert(varchar(25), Start Time, 100), 7)
The 100 you see in the function specifies the date format mon dd yyyy hh:miAM (or PM), and from there we just grab the right characters.
You can see more about converting datetimes here.

Retrieve individual values from datetimeoffset(7) object

I have a datetimeoffset(7) object, I have to convert this object into nvarchar(255) in sql server 2008 r2.
I have source value in format like - '2014-07-01 06:00:00.0000000 +00:00' and desired format is like '07/01/2014 06:00:00 +00:00'.
Below are few ways i tried-
1) DECLARE #a VARCHAR(50) = '2019-10-24 06:00:00.0000000 +00:00'
SELECT (CONVERT(NVARCHAR(255), Cast(#a as DATE) ,101)) +' '+ (CONVERT(NVARCHAR(255),Cast(#a as time))) + ' ' + (CONVERT(NVARCHAR(255),Cast(#a as offset)))
but in code (CONVERT(NVARCHAR(255),Cast(#a as offset)) is causing error.
Is there is any way to get offset time zone value from datetimeoffset(7) objet ? or is there is any inbuild method in sql server using that i can get my desired format ?
datepart function is your friend. In particular, for the time zone offset, you want datepart(tz, #a). Note that the value returned is the number of minutes, so you still have to change that.

Char to DateTime Conversion

I have one column capturedatetime(Char(30)):
2006-04-25T15:50:59.997000 PM
And I want to convert it and load it at other table column which have is in DateTime. either by T-sql or SSIS which ever way.
I have tried with:
select CONVERT(datetime, '2006-04-25T15:50:59.997000 PM', 126)
But it creates an error:
Conversion failed when converting date and/or time from character string
Late update:
In this column I also have other data that is in a completely different format:
29-JAN-10 08.57.41.000000 PM
(1) STOP storing datetime data in string columns! This is nothing, nothing, nothing but trouble.
(2) Why on earth does your column get data in two different string formats that aren't even valid? Why does the string use 24 hour time and have AM/PM suffix? Why use a regional string format and Y2K disaster like 29-JAN-10?
Here is one way, but it's awfully ugly. I highly recommend you fix the SSIS process to give you valid datetime values in the first place, if not as datetimes, at least as valid ISO strings (yyyy-mm-ddThh:mm:ss.nnn):
DECLARE #x TABLE (d CHAR(30));
INSERT #x SELECT '2006-04-25T15:50:59.997000 PM'
UNION ALL SELECT '29-JAN-10 08.57.41.000000 PM';
SET LANGUAGE ENGLISH; -- this is important, else style 6 may not work
SELECT
CASE WHEN d LIKE '__[0-9]%' THEN
CONVERT(DATETIME, LEFT(d, 23))
WHEN d LIKE '[0-9][0-9]-%' THEN
CONVERT(DATETIME, CONVERT(CHAR(8),
CONVERT(DATETIME,REPLACE(LEFT(d,9),' ','-'),6),112)
+ ' ' + REPLACE(SUBSTRING(d,11,8),'.',':')
+ ' ' + RIGHT(RTRIM(d),2))
END
FROM #x;
The conversion for 126 requires no spaces ... I've got it to work like this:
declare #T varchar(50)
declare #dt datetime
set #T = '2006-04-25T15:50:59.997'
set #dt = convert(datetime,#t,126)
select #T, #dt
select convert(datetime,left('2006-04-25T15:50:59.997000 PM',23))
or
select convert(datetime,left(capturedatetime,23))
If you use cast, you do not even need to supply a format. Code snippet below tested on SQL 2012 Developer version.
declare #var_string varchar(50) = '2006-04-25T15:50:59.997';
declare #var_datetime datetime = cast(#var_string as datetime);
select #var_string as my_string, #var_datetime as my_variable;