Date format issue float to mm.yyyy to yyyy-mm-dd - sql-server

Declare #col float =01.2022
Mm/yyyy format
I need output as date as
2022-01-01
YYYY-MM-DD
And YYYY/MM/DD FORMAT

Related

convert varchar yyyy-mm-dd T hh:mm to datetime2 in sql server

If the varchar is 2021-08-15T14:00:06Z format, I can convert it with the following code
SELECT CONVERT(datetime2, '2021-08-15T14:00:06Z')
Result: 2021-08-15 14:00:06.0000000
If the data format is yyyy-mm-dd T hh:mm
SELECT CONVERT(datetime2, '2021-08-15T14:03')
Result:
Conversion failed when converting date and/or time from character string.
Question: how to convert varchar(50): yyyy-mm-dd T hh:mm to datetime2: yyyy-mm-dd hh:mm:ss.000
varchar(50): '2021-08-15T14:03'
Expect result: datetime2: 2021-08-15 14:03:00.000
the format is close enough to ISO8601 that it's probably easiest to use that...
declare #somedate char(16) = '2021-08-15T14:03';
select convert(datetime2, #somedate + ':00', 126);
Alternatively just cut out the "T"...
declare #somedate char(16) = '2021-08-15T14:03';
select convert(datetime2, replace(#somedate, 'T', ' '), 120);
Just another option if the format is yyyy-MM-dd-THH:mm, you can simply concat the seconds.
SELECT CONVERT(datetime2, '2021-08-15T14:03'+':00')
Results
2021-08-15 14:03:00.0000000

How change this date format "2020-04-30T18:11:39+00:00" to IST format in SQL SERVER

Hi I am stuck in converting this date format "2020-04-30T18:11:39+00:00" into IST datetime format.
declare #fiveandhalf datetime = '05:30'
print(#fiveandhalf)
declare #datetime datetime = (SELECT CONVERT(datetime, '2020-04-30 18:11:39'))
print(#datetime-#fiveandhalf)
I am getting this output
Apr 30 2020 12:41PM
but I need output like "2020-04-30 23:41: "
Assuming you are using the appropriate data type for a value with a timezone component, a datetimeoffset, just use AT TIME ZONE:
DECLARE #YourDate datetimeoffset(0) = '2020-04-30T18:11:39+00:00';
SELECT #YourDate AT TIME ZONE 'India Standard Time';
Which returns the datetimeoffset value 2020-04-30 23:41:39 +05:30.

Convert a date format as text - day month

Insert a column: received_by as text in the format of Day Month.
i.e. 25/06/2018 should be inserted as 25 June. The format dd/mm/yyyy should be converted into day month - whereby month should be written out.
You can use below select statement to get the desired return
select FORMAT(convert(datetime, '25/06/2018', 103), 'dd MMMM')
Or You can create the custom function in SQL server which will take a date in 'dd/mm/yyyy' format and return day and month as required. use below code to achieve the desired result.
create function GetDateDaynMonth(#date varchar(20))
returns varchar(20)
as
begin
declare #DaynMonth varchar (20)
SELECT #DaynMonth = FORMAT (convert(datetime, #date, 103), 'dd MMMM')
return #DaynMonth;
end
go
select dbo.GetDateDaynMonth('25/06/2018')

SQL Server convert number to date

How do I convert a numbers in the columns with values like 20160912 into date formats of the form 09/12/2016 and order them by the dates in the date format.
You can use cast and convert built-in functions. Depending on what type is 20160912 you can do following.
A) int
declare #d int=20160912
select convert(varchar(20),convert(date,convert(varchar,#d)),101)
--step by step
declare #dStr varchar(20)
set #dStr = convert(varchar,#d) --'20160912'
-- or = cast(#d as varchar)
declare #dDate date --or datetime
set #dDate = convert(date, #dStr) --2016-09-12 (this is external representation)
--show in MM/dd/yyyy format
select convert(varchar(20), #dDate, 101) --magic 101 for MM/dd/yyyy
--09/12/2016
B) varchar just omit innermost conversion

Convert date from dd-mm-yyyy to yyyy-mm-dd in SQL Server

I want to convert given date into the format YYYY-MM-DD.
Given date:
DECLARE #Date1 VARCHAR(50) = '30-01-2015'
Now I want to convert it into the 2015-01-30.
My try:
Try1:
SELECT CONVERT(VARCHAR(50),'30-01-2015',126)
Try2:
SELECT CONVERT(VARCHAR(50),'30-01-2015',120)
For both try the result remain same that is 30-01-2015.
Try this:
DECLARE #Date1 VARCHAR(50) = '30-01-2015'
SELECT CONVERT(VARCHAR(10), CONVERT(date, #Date1, 105), 23)
Try this way
SELECT CONVERT(date,'30-01-2015',103)
Convert date from dd-mm-yyyy hh:mm:ss to yyyy-mm-dd hh:mm:ss in SQL Server
convert(datetime,'18-11-2019 00:00:00',105) // will return 2019-11-18 00:00:00.000

Resources