Convert date from dd-mm-yyyy to yyyy-mm-dd in SQL Server - 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

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.

SQL convert date dmmyyyy and ddmmyy to yyyy-mm-dd

I have the following problem.
I have some dates with the following format '15122019' and I need it in this format 2019-12-15, which I already solved it in the following way.
select convert (date, Stuff(Stuff('15122018',5,0,'.'),3,0,'.'),104)
The real problem is when the dates come like this '3122019' the conversion can not be done because the length is shorter. Is ther e another way to do it? I've been trying to solve it for several hours. And another question, can this query be parameterized?
Try this:
DECLARE #date VARCHAR(20)
SET #date ='3122019'
IF(LEN(#date) = 8)
BEGIN
SET #date = Stuff(Stuff(#date,5,0,'.'),3,0,'.');
SELECT CONVERT(DATE, #date , 103);
END
ELSE IF(LEN(#date) = 7)
BEGIN
SET #date = Stuff(Stuff(#date,4,0,'.'),2,0,'.');
IF(ISDATE(#date)=1)
BEGIN
SELECT CONVERT(DATE, #date , 103);
END
ELSE
BEGIN
SET #date = Stuff(Stuff(#date,4,0,'.'),3,0,'.');
SELECT CONVERT(DATE, #date , 103);
END
END
ELSE IF(LEN(#date) = 6)
BEGIN
SET #date = Stuff(Stuff(#date,3,0,'.'),2,0,'.');
SELECT CONVERT(DATE, #date , 103);
END
You can add 0 to the left and take 8 chars with right. like RIGHT('0'+'15122018',8). it work with 15122018 and 3122018
select convert (date, Stuff(Stuff( RIGHT('0'+'15122018',8) ,5,0,'.'),3,0,'.'),104)
Such conversation can be achieved by:
Casting integer value to DATE using intermediate FORMAT transformation to a recognizable for conversation string pattern.
style 105 applied to match the input as dd-mm-yyyy
style 05 to match dd-mm-yy
SQL:
-- input format: dmmyyyy
SELECT CONVERT(DATE, FORMAT(3012019, '##-##-####'), 105)
-- result: 2019-01-03
-- input format: dmmyy
SELECT CONVERT(DATE, FORMAT(30119, '##-##-##'), 05)
-- result: 2019-01-03
This will work fine with a single (and double) digit day number, however, it indeed requires a double-digit month

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

How to solve the date comparison issue in SQL Server?

I am using the following way to compare two dates:
if CONVERT(varchar(20), #ScheduleDate, 101) >= CONVERT(varchar(20), #CurrentDateTime, 101)
This is working fine for the current year, but when the comes in yearly like one date is 12/31/2012 and 1/1/2013 then its not working.
Please help me how can I resolve this.
why do you comparing strings?
you can compare dates
if #ScheduleDate >= #CurrentDateTime
but if your date contains time, I usually do
if convert(nvarchar(8), #ScheduleDate, 112) >= convert(nvarchar(8), #CurrentDateTime, 112)
112 datetime format is YYYYMMDD so it's good for compare dates
You have to remember that string comparison is from left to right, so "1/...." is smaller than "12/...".
You need to use DATETIME comparisons, not string comparison.
Something like
DECLARE #ScheduleDate DATETIME = '1/1/2013',
#CurrentDateTime DATETIME = '12/31/2012'
IF (#ScheduleDate >= #CurrentDateTime)
BEGIN
SELECT #ScheduleDate, #CurrentDateTime
END
DECLARE #ScheduleDateString VARCHAR(20) = '1/1/2013',
#CurrentDateTimeString VARCHAR(20) = '12/31/2012'
IF (CONVERT(DATETIME,#ScheduleDateString,101)>=CONVERT(DATETIME,#CurrentDateTimeString,101))
BEGIN
SELECT CONVERT(DATETIME,#ScheduleDateString,101),CONVERT(DATETIME,#CurrentDateTimeString,101)
END
SQL Fiddle DEMO
Note that if the variables are already datetimes, you do not need to convert them.
Assuming that both variables are currently DateTime variables, can't you just compare them without converting to strings?
declare #ScheduleDate DATETIME, #CurrentDateTime DATETIME
SET #ScheduleDate = '1 Jan 2013'
SET #CurrentDateTime = GetDate()
IF (#ScheduleDate >= #CurrentDateTime)
BEGIN
SELECT 'Do Something'
END
ELSE
BEGIN
SELECT 'Do Something Else'
END
when you use CONVERT(nvarchar(8), #ScheduleDate, 112) function it's return string instead of date.
so,
Use "112" DateFormat in Sql Server it's return string in "YMD" format without any sepration.
compare that string in your query and get desire output.
Such as "if CONVERT(nvarchar(8), #ScheduleDate, 112) >= CONVERT(nvarchar(8), #CurrentDateTime, 112)"
I would not use CONVERT to compare formatted strings. It is slow (well, more like microseconds, but still)
I use a UDF for SQL prior to version 2008
CREATE FUNCTION [dbo].[DateOnly] (#Date DateTime)
RETURNS Datetime AS
BEGIN
Return cast (floor (cast (#Date as float)) as DateTime)
END
and for versions >=2008 this approach
select convert(#MyDateTime as DATE)
Of course, you can compare datetime values directly, but to know whether two datetime values are on the same date (ignoring the time component), the above versions have proven to be effectivy.
Date : From and To with following format
from_Date# = #dateformat("#form.from#", "mm/dd/yyyy")
to_Date# = #dateformat("#now()#" + 1, "mm/dd/yyyy")
In SQL Statement
WHERE a.DateCreated >= CAST ('#from_date#' AS DATE) and a.DateCreated <= CAST('#to_date#' AS DATE)
This is working fine without any cast of original date time column

Resources