How to cast varchar to datetime in sql - sql

I need to cast a string to a datetime so i can compare it later.
The varchar that i have is like this format:
29/11/2013 12:00:00 a.m.
And i need it to cast to:
2013-11-29 00:00:00.000
Im using MSSQL Server 2012
Thx for all

Please, have a look a CAST and CONVERT for more information.
Here are some examples:
-- converting from DD-MM-YYYY
select CONVERT(datetime, '29/11/2013 12:00:00 AM', 103)
-- converting from MM-DD-YYYY
select CONVERT(datetime, '11/29/2013 12:00:00 AM', 101)

IF Your date is of type varchar in the database then if you need to retrieve
then these cases arises::
case1:if your data is in the format of "dd-MM-yyyy"
then you need to use query as follows
Query::
select * from [yourTableName] where convert(datetime,YourDateColumn,103) between '2016-02-12'(yyyy-MM-dd) AND '2016-03-12'
case2:if your data is in the format of "dd-MM-yyyy HH:mm:ss"
then you need to use query as follows
Query::
select * from [yourTableName] where convert(datetime,YourDateColumn,105) between '2016-02-12'(yyyy-MM-dd) AND '2016-03-12'

Related

How can I convert to UTC from VARCHAR that's in AM/PM format?

I have a T-SQL VARCHAR column that looks like this:
APR-10-2018 11:33:39 AM
Dec-04-2017 11:14:24 AM
DEC-01-2017 07:24:54 PM
DEC-01-2017 07:20:28 PM
DEC-01-2017 07:19:52 PM
I want to convert it to UTC. I realize it will likely require using the CONVERT function. However, I have been unsuccessful in my attempts to do this. How?
Converting to a DateTime would be small matter. However, we have no idea what your timezone is, or more importantly, the timezone of the data
Example
Declare #YourTable table (SomeCol varchar(50))
Insert Into #YourTable values
('APR-10-2018 11:33:39 AM')
,('Dec-04-2017 11:14:24 AM')
,('DEC-01-2017 07:24:54 PM')
,('DEC-01-2017 07:20:28 PM')
,('DEC-01-2017 07:19:52 PM')
Select AsDateTime = try_convert(datetime,replace(SomeCol,'-',' '))
From #YourTable
Returns
AsDateTime
2018-04-10 11:33:39.000
2017-12-04 11:14:24.000
2017-12-01 19:24:54.000
2017-12-01 19:20:28.000
2017-12-01 19:19:52.000
You need to read up on cast() and convert(): https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-2017
[Hint: you have to specify the desired date/time style).

Cast Datetime offset to Date

I have the date column in datetimeoffset format.
2016-01-09 05:49:06.3744350 +00:00
I want to convert it to only date format, e.g. 2016-01-09.
I was able to convert datetimeoffset to datetime2 with this query.
convert(datetime2, sampleDate, 1) as date
Would be much obliged if I could know how to convert this to the desired format in MS SQL.
Simply:
SELECT CONVERT(DATE, CONVERT(DATETIMEOFFSET, '2016-01-09 05:49:06.3744350 +00:00'))
Returns:
2016-01-09
You can use CAST function
SELECT CAST('2016-01-09 05:49:06.3744350 +00:00' as date)

How to convert date from Germany to local date in sql?

I have a column in a table with time stamps from Germany (Utc + 6) in their local time (Utc -1). How do I convert all those datetimes in my local time?
I think the correct way of converting UTC datetime into local datetime is using CLR function. You can find an example below.
https://www.mssqltips.com/sqlservertip/2339/converting-utc-to-local-time-with-sql-server-clr/
There are -7 hour between germany and central time zone in us
CT (-6)
Berlin (+1)
If your column is datetime USE THE FOLLOWING
Select DATEADD(HOUR,-7,[DATECOLUMN])
Update 1
Consider that #bdate and #edate are the begin and end date of the daylight savings so you can use this query
Select case when DATEADD(HOUR,-7,[DATECOLUMN]) between #bdate and #edate Then
DATEADD(HOUR,-8,[DATECOLUMN]) else
DATEADD(HOUR,-7,[DATECOLUMN]) end
If your data is in a string format like "dd.mm.yy," then you would use the code page 104 to tell SQL how to parse your date from string to a DateTime format.
CONVERT(DateTime, DateField, 104)
If your string is in some other format, then look up the correct codepage in the table here (CAST and CONVERT in T-SQL):

How to assign current date with specific time to column?

How do i assign current date with a specific time?
let's say 8:00:00 AM to Column EXIT_DT of datatype datetime??
I have tried GETDATE() AS EXIT_DT but it gives me current datetime. I am using Sql server 2005. Any help?
Lets say Today is 1/3/2013 and i want my result to return as a datetime datatype with value 1/3/2013 8:00:00 AM. If i run the statement ytd, the result will be 1/2/2013 8:00:00 AM
This formula will always produce 08:00 for the day it is called, and avoids string manipulation:
select DATEADD(day,DATEDIFF(day,'20010101',GETDATE()),'2001-01-01T08:00:00')
Try to avoid solutions that convert to and from strings - treating datetime values as strings is one of the largest sources of bugs.
It works by computing the number of days (as an integer) that have elapsed since 1st January 2001. It then adds that same number of days to 08:00 on 1st January 2001.
You can try this :
DECLARE #dt datetime;
SET #dt=CONVERT(DateTime, CONVERT(VARCHAR,GETDATE(),101)+' 8:00:00')
SELECT CONVERT(VARCHAR, #dt, 101)+' '+ LTRIM(RIGHT(CONVERT(VARCHAR(20),#dt, 100), 7))
Visit http://www.sql-server-helper.com/tips/date-formats.aspx for datetime formats.
Use Convert along with getdate() to get specific formats.
ex:
SELECT CONVERT(VARCHAR(30),GETDATE(),113)
This is a bit stupid, but it works
select cast(cast(getdate() as date) as datetime) + '08:00:00'
it casts the getdate() to date thus losing the hours, than it casts it to datetime and adds 8 hours.
If you want to avoid implicit conversion of varchar to datetime, you could use this version:
select cast(cast(getdate() as date) as datetime)
+ convert(datetime,'08:00:00',114)
This is also working. (1). convert today's date to ISO format (yyyymmdd) (2). add the time, (3). convert back to datetime
Select convert(datetime, convert(varchar, getdate(),112) + ' ' + '8:00:00AM')
--Results
2013-01-03 08:00:00.000
If you need in specific format you need to convert back to varchar again.
-- AM/PM --
SELECT TO_CHAR(sysdate, 'MM/DD/YYYY HH:MI:SS AM') FROM dual
/
-- 24 hrs format --
SELECT TO_CHAR(sysdate, 'MM/DD/YYYY HH24:MI:SS') FROM dual
/

Which datetime format to use SQL Server

I have a date as a varchar in the form
DD/MM/YYYY HH:MM:SS AM
e.g.
16/3/2012 4:39:26 PM
I can't see a valid format option for CONVERT() in the MSDN page
Am I missing something or will I have to reformat the varchar field first?
Edit:
Corrected the format, sorry about that
Once you get your story straight, you'll want one of these:
-- if you really meant dd/mm/yyyy then:
SELECT CONVERT(DATETIME, '16/3/2012 4:39:26 PM', 103);
-- if you really meant mm/dd/yyyy then:
SELECT CONVERT(DATETIME, '3/16/2012 4:39:26 PM', 101);
But agreed with Madhivanan. Don't store dates using the wrong data type, and if you must, use an unambiguous format!
Always use proper DATETIME datatype to store dates
YYYYMMDD and YYYYMMDD HH:MM:SS are unambiguous date formats
For more information, refer this
http://beyondrelational.com/modules/2/blogs/70/posts/10898/understanding-datetime-column-part-ii.aspx
This will work.
DECLARE #dt varchar(100)='2012/3/16 4:39:26 PM'
select convert(datetime,#dt,101)