Convert DATETIME to varchar in SQL - sql

I want to convert DATETIME to VARCHAR with Friday 07-Feb-20 8:30 AM this type of output. I tried to do it like this. But it didn't give the correct format that I want.
DECLARE #STARTDATE DATETIME,#SDATE VARCHAR(250)
SELECT #STARTDATE = SESSION_START FROM SESSION_INFO WHERE SESSION_ID = 2071 //#STARTDATE = 2013-01-28 14:00:00.000
SET #SDATE = CONVERT(VARCHAR,#STARTDATE,100)
PRINT #GOOGLE //Jan 28 2013 2:00PM
I want day-mon-year hh:min AM/PM (Friday 07-Feb-20 8:30 AM) this format. Thank you.

You want format for that e.g.
select format(current_timestamp, 'dddd dd-MMM-yy hh:mm tt');
Note: format doesn't perform as well as convert or cast but it has the added flexibility you need.

You can use the format as follows:
FORMAT (SESSION_START, 'dddd dd-MMM-yy hh:mm tt')
DB<>Fiddle with the example output

Related

How to convert NVarchar date time to date time format in SQL Server?

How to convert this date to date time format
20180206 00:00
or
20180206 00:00
SELECT CAST('20180206 00:00' AS DATETIME)
Could you please try this

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):

Converting datetime variable format

I need help in converting a DateTime variable with format mmm dd yyyy
to a Datetime variable with format yyyy-mm-dd
How about this?
Select CAST('Sep 07 2016' AS DATE)
Returns
2016-09-07
I'm assuming your datetime variable is a VARCHAR - this can be directly cast via CONVERT:
Declare #YourVariable Varchar (15) = 'JAN 01 1996'
Select Convert(Date, #YourVariable)
1996-01-01

How to cast varchar to datetime in 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'