DATE conversion format in mssql - sql

my hiredate column is in yyyy-mm-dd hh:mm:ss format now i want the output format in DD-Mon-YYYY HR:MM format..
for example :-
hiredate (datatype is varchar)
2018-04-16 12:19:10
required output if 16-Apr-2018 12:20.

You need to CAST the field to DATETIME,then use FORMAT function.
There are three parameter in FORMAT function
Your datetime [requird]
FormatType for this case you need to set dd-MMM-yyyy hh:mm [requird]
culture [Optional]
From your question you need to check there are seconds in your datetime.
So add A CASE expression to make it.
DECLARE #Dt VARCHAR(50) = '2018-04-16 12:19:10'
SELECT
CASE
WHEN FORMAT(CAST(#Dt as datetime),'ss') > 0
THEN FORMAT(DATEADD(MINUTE,1,CAST(#Dt as datetime)),'dd-MMM-yyyy hh:mm', 'en-US')
ELSE FORMAT(CAST(#Dt as datetime),'dd-MMM-yyyy hh:mm', 'en-US')
END
sqlfiddle:http://sqlfiddle.com/#!18/22a72/2
Format

Try this
DECLARE #d varchar(50) = '2018-04-16 12:19:10'
SET #d = FORMAT(CONVERT(datetime, #d), 'dd-MMM-yyyy HH:MM', 'en-us')
SELECT #d
or this...
SELECT FORMAT(CONVERT(datetime, '2018-04-16 12:19:10'), 'dd-MMM-yyyy HH:MM', 'en-us')
Result

try This
DECLARE #MyDt NVARCHAR(50)='2018-04-16 12:19:10'
SELECT
InputDt = #MyDt,
NewDt = REPLACE(LTRIM(RTRIM(CONVERT(CHAR,CAST(#MyDt AS DATETIME),106))),' ','-')
+' '+CAST(CAST(#MyDt AS TIME) AS NVARCHAR(20))
My Result :

As the comments said, you should import your dates and times into datetime or datetime2 column. However, if you insist on the data as you have it, this is probably closest you can get:
SELECT CONVERT(varchar(50), DATEADD(minute, DATEDIFF(minute, 0, DATEADD(second, 30, CAST('2018-04-16 12:19:10' as datetime2(0)))), 0), 113)
, CONVERT(varchar(50), DATEADD(minute, DATEDIFF(minute, 0, DATEADD(second, 30, CAST('2018-04-16 12:19:31' as datetime2(0)))), 0), 113);
You could throw in some string manipulation to add your dashes for the date part of it.
Notice, the second CONVERT is for 12:19:31 time which shows the result gets rounded up when needed (in this case to 12:20).
For more CONVERT formats check out this source.

Related

Concatenate date and string to create datetime in SQL

I need to concatenate a datetime and a time field in SQL to a single date time.
e.g I have a datetime of 2017-09-05 00:00:00.000 and a string time of 11:00. What I want is a single field in a view of 2017-09-05 11:00:00.000
I have tried casting the datetime to a date and then concatenate the new date and string date field together but this doesn't work.
To cast the datetime I am using: CAST(dtDate AS DATE) AS dtNewDate which works fine. When I then use: CAST(dtNewDate + szTime AS datetime) AS dtNewDateTime the creation of the view works fine but selecting the top 1000 returns a "conversion failed when converting date and/or time from character string."
Is there an easier way to do this or can anyone offer some advise (other than storing the date and time in a single datetime field in the first place as it is populated by a third party application which I do not have access to change)
You can add two datetime values together, so try:
CAST(dtDate AS DATETIME) + CAST(CAST(szTime AS TIME) as DATETIME)
Assuming 11:00 stands for 11:00:00, you can do something like this:
SELECT dtDate + CONVERT(DateTime, szTime, 108)
FROM...
See a live demo on rextester
You can try following.
DECLARE #YourDate AS DATETIME
SET #YourDate = CONVERT(VARCHAR(10), '2017-09-05 00:00:00.000', 111) + ' 11:00'
PRINT #YourDate
another way is to get the hour from your stringfield, convert it to int, and add that as hours to the datetime
declare #date datetime = '20170905'
declare #stringtime varchar(5) = '11:00'
select left(#stringtime, 2),
dateadd(hour, convert(int, left(#stringtime, 2)), #date)
If you also need the minutes you can do it like this :
declare #date datetime = '20170905'
declare #stringtime varchar(5) = '11:05'
select left(#stringtime, 2),
right(#stringtime, 2),
dateadd(minute, convert(int, right(#stringtime, 2)), dateadd(hour, convert(int, left(#stringtime, 2)), #date))
This will only work if the stringfield is always in format hh:mm
If you care about precision with DATETIME2,
DECLARE #D DATETIME = '2017-01-01';
DECLARE #T varchar(7) = '11:00';
SELECT DATEADD(day, DATEDIFF(day, '19000101', #D), CAST(CAST(#T AS TIME) AS DATETIME2(7)));
Running example here

How can I handle a SQL query by using convert decimal string to datetime in SQL Server?

I am trying to convert a decimal string to datetime:
20160709.0000000 => 09-07-2016 (dd-MM-YYYY)
but the code here returns an error:
Conversion failed when converting date and/or time from character string
Code:
select
CONVERT(DATETIME, CONVERT(nvarchar(50), MyBirthDate), 112) BirthDate, Test, Test2
from
tbl
This code also does not work:
select
CONVERT(DATETIME, CONVERT(VARCHAR(50), '20160709.0000000'), 112)
The unseparated format you show (yyyymmdd) will be casted implicitly. I hope I do understand your convert decimal string correctly. Assuming, the value is of string type, you can cast this directly, just cut off the part you need:
DECLARE #s VARCHAR(100)='20160709.0000000';
SELECT CAST(LEFT(#s,8) AS DATETIME);
The result
2016-07-09 00:00:00.000
Hint
This solution would just work the same with the argument as decimal value, due to the implicit cast to string while passing into LEFT:
DECLARE #s DECIMAL(20,10)=20160709.0000000;
SELECT CAST(LEFT(#s,8) AS DATETIME);
At first you need to convert string to int or else you can use replace function
SELECT CONVERT(DATETIME, REPLACE('20160709.0000000', '.0000000', ''))
or
SELECT CONVERT(DATETIME, CONVERT(VARCHAR(50), CAST(20160709.0000000 AS INT )), 112)
or
SELECT CONVERT(DATETIME, CONVERT(VARCHAR(50), CAST(20160709.0000000 AS INT )))
SELECT CONVERT(DATETIME, CONVERT(VARCHAR(50), CAST(20160709.0000000 AS INT )), 112)

Converting smalldatetime datatype to varchar datatype

How to convert smalldatetime to varchar? I've tried everything from http://msdn.microsoft.com/en-us/library/ms187928.aspx, but it didn't work.
I want to convert smalldatetime into varchar, because I want to use it in select like this:
select 'Some text'+#Date
thanks in advance
'121' is the format of the date in this case 'yyyy-mm-dd hh:mi:ss.mmm(24h)',
char(16) is the number of characters you wish to include, first 16 in this case.
select 'Some text'+convert(char(16), #date, 121)
Cast and Convert
SELECT CONVERT(VARCHAR(20), YourDateColumn, 103) as NewColumnName
here 103 make the date format as dd/mm/yyyy
if you want mm/dd/yyyy, you have to use 100
Here is a more up to date link.
The expression
'Some text ' + CONVERT(VarChar(20), [Data])
works fine, fiddle here, what style did you want?
Prueba con esto
DECLARE #FechaDesde datetime2
SET #FechaDesde = ''
SELECT LEFT(CONVERT(VARCHAR, #FechaDesde, 120), 10)
print #FechaDesde
---FECHA HASTA
DECLARE #FechaHasta datetime2
SET #FechaHasta = ''
SELECT LEFT(CONVERT(VARCHAR, #FechaHasta, 120), 10)
print #FechaHasta
SELECT * from compras where fec_emis between #FechaDesde and #FechaHasta

ddmmyyyy to sql datetime in SQL

I need to convert a nvarchar value to datetime in T-SQL. The value is in ddmmyyyy format, e.g. 23072009
I need to convert to datetime in T-SQL.
I tried
select convert(datetime, '23072009', 103)
But it is throwing error.
"The conversion of a nvarchar data type to a datetime data type
resulted in an out-of-range value."
Any idea
Thanks
Rebuild your format to yyyymmdd.
declare #D varchar(8)
set #D = '23072009'
select cast(right(#D, 4)+substring(#D, 3, 2)+left(#D, 2) as datetime)
The style 103 will accept strings with dd/mm/yyyy format. So your code should be
declare #date varchar(8)
set #date='23072009'
select convert(datetime,stuff(stuff(#date,5,0,'/'),3,0,'/') , 103)
You can define custom function like this:
CREATE FUNCTION [dbo].[GetCustomDate] (#customDateString NVARCHAR(MAX))
RETURNS DATETIME
AS
BEGIN
RETURN CONVERT(DATETIME, RIGHT(#customDateString, 4) + RIGHT(LEFT(#customDateString, 4), 2) + LEFT(#customDateString, 2))
END
Change VARCHAR (dd-mm-yyyy) to date (yyyy-mm-dd) in SQL Server.
DECLARE #VarDate VARCHAR(10)
SET #VarDate = '22-02-1994'
SELECT CONVERT(DATETIME, CONVERT(VARCHAR, CONVERT(DATE, #VarDate, 103), 102))
You need to cast a string and not a int. Put some quotes:
convert(datetime, '23072009', 103)
And 103 gets the string like 'dd/mm/yyyy' and not 'ddmmyyyy'.

SQL VarChar to Date

hi
i am trying to convert a VarChar date field (e.g. 20100320) to a real date field like
'dd/mm/yyyy' (e.g. 20/03/2010).
I have tried two ways:
a)
(SELECT MIN(CAST(A.DateOfAction AS Date)) AS Expr1
FROM ResAdm.Action A
WHERE (A.PersonID = P.PersonID))
AS 'Period From',
b)
(SELECT MIN(CONVERT(DATE, A.DateOfAction, 103)) AS Expr1
FROM ResAdm.Action A
WHERE (A.PersonID = P.PersonID))
AS 'Period From',
both producing the result like
yyyy-mm-dd (e.g. 2010-03-20)
but i want the result like
dd/mm/yyyy (e.g. 20/03/2010)
any help will be appreciated.
thanks.
Try this:
select convert(varchar(8), convert(datetime, min(a.DateOfAction), 112), 103)
Your problem is that once you have a date format, SQL Server will dump it out in its default date format, which you've discovered is yyyy-mm-dd. You need to convert from date to varchar to get the format you want. But to convert from date, you need to first convert to date! 112 is the format for yyyymmdd, and 103 is the format for dd/mm/yyyy, so this is why you need these formats. (Books Online reference for date formats)
Declare #date nvarchar(100)
set #date = '20100320'
select convert(varchar, CONVERT(datetime, #date, 109), 103)
You can use
convert(varchar, CONVERT(datetime, A.DateOfAction, 109), 103)