Converting smalldatetime datatype to varchar datatype - sql

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

Related

DATE conversion format in mssql

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.

Need to format date in SQL from string using charindex

I am trying to format a date in mm/dd/yyyy from string data using charindex, the date in the string is yyyymmdd. I have tried using convert, cast, castconvert.
If anyone could help that would be great! I have sample code
CONVERT (
VARCHAR (10)
, CASE
WHEN CHARINDEX ('^3507=', reg.BetaRequestString) = 0 THEN
''
ELSE
RIGHT(LEFT(reg.BetaRequestString, CHARINDEX ('^3507=', reg.BetaRequestString) + 13), 8)
END
, 101
declare #a nvarchar(10)
set #a='20180203'
select
convert(nvarchar(10),substring(#a,5,2)+'/'+substring(#a,7,2)+'/'
+substring(#a,1,4),101)
It appears you want this:
CASE WHEN CHARINDEX ('^3507=', reg.BetaRequestString) = 0
THEN ''
ELSE CONVERT(VARCHAR(10),RIGHT(LEFT(reg.BetaRequestString, CHARINDEX ('^3507=', reg.BetaRequestString) + 13), 8), 101)
END
When you know your input date format , you can split the parts and reorganise the date.
declare #date varchar(100)='20021230' ,
#Year int,#month int ,#day int
select #year=Substring(#date,1,4) ,#month=SUBSTRING(#date,5,2),#day=SUBSTRING(#date,7,2)
select convert(VARCHAR(10),(DATEFROMPARTS(#year,#month,#day)),101)
output-12/30/2002
You can also use datetime style in convert function:
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql#date-and-time-styles
DECLARE #yyyymmdd sysname = '20180101'
SELECT CONVERT(sysname, CONVERT(datetime, #yyyymmdd, 112), 101)

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)

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)