convert datetime (mm/dd/YYYY) to decimal(YYYYmmDD) - sql

I am trying to get the following result in Sql:
example: 23/05/2014 to 20142305
but get this:
select convert(decimal, convert(datetime, '5/23/2014'))
result:41780
anyone know how you can get the following format?? (if possible ??)
regards and thanks

In many databases (including SQL Server), you can just do:
select 10000 * year(datetime) + 100 * month(datetime) + day(datetime)
Some databases don't support these functions, so you might need extract(datepart from datetime) or a similar function.

You can try this
SELECT CAST(CONVERT(VARCHAR(8), datetime, 112) AS DECIMAL)

CREATE FUNCTION DateToYYYYMMDD
(
#Date DateTime
)
RETURNS decimal
AS
BEGIN
DECLARE #ResultVar decimal
SET #ResultVar=Year(#Date)*10000+Month(#Date)*100+Day(#Date)
RETURN #ResultVar
END
You can use it like this
Select dbo.DateToYYYYMMDD(GetDate())
Remeber,when calling a user function yoy MUST include the schema in this case dbo.

select convert(decimal, format(convert(datetime, '5/23/2014'),'yyyyMMdd'))

SELECT
CONVERT(NUMERIC,
CONCAT(
LEFT('23/05/2014',2)
,RIGHT(LEFT('23/05/2014',5),2)
,RIGHT('23/05/2014',4)
)
)
The CONVERT(NUMERIC part may/not be necessary depending on how the value will be used.

Related

Incremental next month dates in SQL

I have similar requirement, i have file feeding to SQL Server 2012 for a month but i wanted to convert the same file as a next month file.
For Example I have below
02/22/2018 02/23/2018 02/24/2018
and I want 03/22/2018 03/23/2018 02/24/2018
I am able to get upto this point but facing issue when i have below situation.
02/28/2018 02/24/2018
and I want 03/31/2018 03/31/2018
I have a normal calendar set up which I can use Can somebody please help.
you can use DATEADD() function for this like below :
SELECT DATEADD(month, 1, '02/22/2018') AS DateAdd;
I guess you are looking for EOMONTH function
somethink like this might help you solve your issue
DECLARE #date VARCHAR(10) = '02/28/2019'
SELECT CASE WHEN (EOMONTH(#date) = FORMAT(CAST(#date AS DATETIME),'yyyy-MM-dd'))
THEN EOMONTH(DATEADD(month,1,#date))
ELSE DATEADD(month,1,#date)
END
Note: I have used format function because EOMONTH returns date in 'yyyy-MM-dd' format
UPDATE
DECLARE #dates TABLE
(
FromDate DATETIME,
ToDate DATETIME
)
INSERT INTO #dates (FromDate)
VALUES('12/31/2017'),('1/31/2018'),('2/28/2018'),('3/31/2018')
select * from #dates
UPDATE #dates
SET ToDate =CASE WHEN (EOMONTH(FromDate) = FORMAT(CAST(FromDate AS DATETIME),'yyyy-MM-dd'))
THEN EOMONTH(DATEADD(month,1,FromDate))
ELSE DATEADD(month,1,FromDate)
END
SELECT * FROM #dates
Run this and you can see the final output

SQL Server date conversion is 68 years behind the actual date

I want to convert an integer to a date format. I am using the following script:
SELECT CAST(7549 as datetime)
It returns me a date 1920-09-02
However, 7549 actual date is 1988-08-31
Can anyone please help me how to perform correct mapping?
See the reverse
DECLARE #YourDate AS Datetime
SET #YourDate = '1920-09-02'
SELECT CAST(#YourDate AS INT) --7549
SET #YourDate = '1988-08-31'
SELECT CAST(#YourDate AS INT) -- 32384
If you want the result as '1988-08-31 00:00:00.000', try with the following scripts.
SELECT CAST(7549 as datetime)-CAST('1832-01-03' as datetime)
OR
SELECT CAST(7549 as datetime)+CAST(24835 as datetime)

Convert actual time to float in SQL

How can I convert a specific time or actual time, in SQL, to a float number? E.g., if it is 09:59:57 AM I want this float number: 0.416634556 like it is in Excel.
Thank you all.
take a look at this, you may find your answer here: CAST and CONVERT (Transact-SQL)
SELECT CAST(CONVERT(DATETIME, Col1) AS FLOAT) AS NewValue
FROM Table1
SELECT CAST(cast(Col1 as datetime) AS FLOAT) - CAST(cast(Col1 as datetime) AS int) AS decPart
FROM yourTable
try this one
None of the other answers worked for me, but this does: SELECT CAST(CAST(GETDATE() AS DATETIME) AS FLOAT) - FLOOR(CAST(CAST(GETDATE() AS DATETIME) AS FLOAT)) And then you can swap out the GETDATE() calls for your column.

how to format getdate into YYYYMMDDHHmmSS

In SQL Server how do I format getdate() output into YYYYMMDDHHmmSS where HH is 24 hour format?
I've got the YYYYMMDD done with
select CONVERT(varchar,GETDATE(),112)
but that is as far as I got.
Thanks.
Just for anyone searching for this functionality that has SQL Server 2012 you can use the FORMAT function:
SELECT FORMAT ( GETDATE(), 'yyyyMMddHHmmss') AS 'Custom DateTime'
This allows any .NET format strings making it a useful new addition.
select replace(
replace(
replace(convert(varchar(19), getdate(), 126),
'-',''),
'T',''),
':','')
Close but not exactly what you are asking for:
select CONVERT(varchar, GETDATE(), 126)
e.g.
2011-09-23T12:18:24.837
(yyyy-mm-ddThh:mi:ss.mmm (no spaces), ISO8601 without timezone)
Ref: CAST and CONVERT
There is no way to specify a custom format with CONVERT(). The other option is to perform string manipulation to create in the format you desire.
Try this:
select CONVERT(varchar, GETDATE(), 120)
e.g.
2011-09-23 12:18:24
(yyyy-mm-dd hh:mi:ss (24h) ,ODBC canonical).
Hth.
Another option!
SELECT CONVERT(nvarchar(8), GETDATE(),112) +
CONVERT(nvarchar(2),DATEPART(HH,GETDATE())) +
CONVERT(nvarchar(2),DATEPART(MI,GETDATE())) +
CONVERT(nvarchar(2),DATEPART(SS,GETDATE()));
converting datetime that way requires more than one call to convert. Best use for this is in a function that returns a varchar.
select CONVERT(varchar,GETDATE(),112) --YYYYMMDD
select CONVERT(varchar,GETDATE(),108) --HH:MM:SS
Put them together like so inside the function
DECLARE #result as varchar(20)
set #result = CONVERT(varchar,GETDATE(),112) + ' ' + CONVERT(varchar,GETDATE(),108)
print #result
20131220 13:15:50
As Thinhbk posted you can use select CONVERT(varchar,getdate(),20) or select CONVERT(varchar,getdate(),120) to get quite close to what you want.
select CONVERT(nvarchar(8),getdate(),112) +
case when Len(CONVERT(nvarchar(2),DATEPART(HH,getdate()))) =1 then '0' + CONVERT(nvarchar(2),DATEPART(HH,getdate())) else CONVERT(nvarchar(2),DATEPART(HH,getdate())) end +
case when Len( CONVERT(nvarchar(2),DATEPART(MI,getdate())) ) =1 then '0' + CONVERT(nvarchar(2),DATEPART(MI,getdate())) else CONVERT(nvarchar(2),DATEPART(MI,getdate())) end

How can I transform this Lambda Expression into SQL statement?

I have a lambda expression that has this:
Convert.ToDateTime(a.startTime).TimeOfDay >= Convert.ToDateTime(startTime).TimeOfDay
But, I have to create a procedure in SQL Server and how should be the statement above to SQL statement?
I've tried to use some kinda 'convert(startime, getdate(),8) but it didn't work.
And I forgot to say that 'startTime' is a DateTime field and I'm trying to compare only the time part (forget about the date part).
Thanks!!!
From here:
CREATE FUNCTION dbo.TIMEVALUE
(
#Datetime datetime
)
/*******************************************************************************
* AUTHOR: Luciano Evaristo Guerche *
*******************************************************************************/
RETURNS datetime
AS
BEGIN
RETURN (#Datetime - CAST(ROUND(CAST(#Datetime AS float), 0, 1) AS datetime))
END
GO
Have you tried:
CAST(starttime as time)
Have a look at this for more detail time (Transact-SQL)