Int to date in SQL [duplicate] - sql

This question already has answers here:
Convert an Int to a date field
(2 answers)
Closed 3 years ago.
I try to convert the column [policyeffective_date] which looks like YYYYMMDD (for example 20190430) into a date in SQL Server.
I tried a few solutions find on the forum, but none worked. Currently I have the following code and errors:
SELECT
CONVERT(DATETIME, policyeffective_date, 104) AS test
FROM
[DATAWAREHOUSE].[dbo].[blabla]
Error:
Arithmetic overflow error converting expression to data type datetime.
I also tried:
SELECT
CONVERT(DATE, CONVERT(NVARCHAR(8), policyeffective_date), 104)
FROM
[DATAWAREHOUSE].[dbo].[blabla]
Error:
Operand type clash: int is incompatible with date
Thank you for your help

You would seem to have bad data in the column. Given that you are getting a run-time error and not a compile-time error, the types seem correct.
The simplest solution is try_convert():
SELECT try_convert(datetime, policyeffective_date, 104) AS test
FROM [DATAWAREHOUSE].[dbo].[blabla];
But to find the problem, you can use:
SELECT policyeffective_date
FROM [DATAWAREHOUSE].[dbo].[blabla]
WHERE try_convert(datetime, policyeffective_date, 104) IS NULL AND
policyeffective_date IS NOT NULL

This works for me in SQL Server 2017:
select cast(cast(20190430 as nvarchar) as DATE) AS DateResult
Output:
DateResult
2019-04-30

Related

Conversion from INT to varchar in sql

I have a table where there are values like 20170730 and also 0 values are there which is INT type
I am trying to convert it to value like 30/07/2017, for which i am using the below code,
Select convert(NVARCHAR(10),convert(date,convert(NCHAR(8),datecolumn)),103) from table
But for the zero values i am getting the below error
Conversion failed when converting date and/or time from character string.
If i delete all the zero this working fine but problem having with zero.
My requirement is to convert when there a date value and if 0 are there then it should be zero only like below,
Result
30/07/2017
0
Can u pls help
As already pointed out in the comments, you can try to use a CASE expression
SELECT CASE
WHEN nmuloc = 0 THEN
'0'
ELSE
convert(varchar(10),
convert(date,
convert(varchar(8),
nmuloc),
112),
103)
END
FROM elbat;
or try_convert() and coalesce().
SELECT coalesce(convert(varchar(10),
try_convert(date,
convert(varchar(8),
nmuloc),
112),
103),
'0')
FROM elbat;
db<>fiddle
The latter one will also correct other "malformed" data like 123 for example. The former will also fail in such cases. You may want that or not.
But, as also already pointed out in the comments, your real problem is that you use an inappropriate data type. Change the column's datatype to some date/time data type to really fix this.

SQL Server 2017 Convert Varchar to Date

I have a date stored as nvarchar which I cannot seem to convert to a date.
I have tried the following but get the error message
Conversion failed when converting date and/or time from character string
select cast([month-year] as date) from [Reporting].[AIM].[Hires_PBI]
select convert(date, [month-year], 103)from [Reporting].[AIM].[Hires_PBI]
Any ideas here?
Find the values that are causing the problem using try_convert():
select [month-year]
from [Reporting].[AIM].[Hires_PBI]
where try_convert(date, [month-year], 103) is null and
[month-year] is not null;
Once you see what values are causing the problem, you can adjust the conversion logic to convert all values. Or just use try_convert() to get NULL for the non-convertible values.

How to get year of character hijri date SQL Server

I searched and tried many examples unable to solve my hijri date is like,
19/07/1440
I tried this query
SELECT TOP 200
DATEPART(YEAR, EndDateHejri)
FROM
student
but I'm getting this error
Conversion failed when converting date and/or time from character string
I'm unable to solve error - hoping for your suggestions
I bit of Google-Fu and format 131 should help you convert Hijri dates into Gregorian Dates...
DECLARE #hijri DATETIME = CONVERT(datetime, ' 7/05/1421 12:14:35:727PM', 131)
SELECT #hijri
Unfortunately, all the date functions (DATEPART(), DATENAME(), even DATEADD(), etc) are all based on manipulating Gregorian dates. So you can't use them.
So, you're forced to use string manipulation.
DECLARE #hijri DATETIME = CONVERT(datetime, ' 7/05/1421 12:14:35:727PM', 131)
SELECT #hijri
SELECT DATEPART(year, #hijri)
-- Gives 2000 :(
SELECT RIGHT(CONVERT(VARCHAR(10), #hijri, 131), 4)
-- Gives 1421 :)
https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=901209ae0cdcf38cdcdea8afad4fd034
Posting a different answer. As the OP is only after the year part, and they've stated that it's always in the format 00/00/yyyy why not just use RIGHT? So:
SELECT RIGHT(EndDateHejri,4) as HejriYear;
I tried answer #Vishnu Chandel it's working for me .
SELECT DATEPART(YEAR,CONVERT(datetime2(0),convert(VARCHAR,EndDateHejri),103))
And full code is :
SELECT TOP 200
SELECT DATEPART(YEAR,CONVERT(datetime2(0),convert(VARCHAR,EndDateHejri),103)) as year
FROM
student
Please try below code to get the correct output.
SELECT DATEPART(YEAR,CONVERT(datetime2(0),convert(VARCHAR,EndDateHejri),103));

How to get alone time 11:21 (highlighted in red) with SQL Server database? [duplicate]

This question already has answers here:
SQL Server 2005 DateTime (return only hh:mm)
(2 answers)
Closed 6 years ago.
Done automatically add the date to the time of the database SQL Server 2005 Express. So when you add a new record in the database is added automatically date and time in the form of:
Create Table Baza_test
(
ID bigint IDENTITY (1,1) NOT NULL,
Client nvarchar (23)
address nvarchar (46)
DateOfAddmission datetime default CURRENT_TIMESTAMP
aaaaaadres nvarchar (46)
);
How to get alone time (highlighted in red):
with SQL Server database?
With the following query:
select CONVERT(VARCHAR(5), DateOfAddmission,108) AS [time] from Baza_test where ID = 1;
There is already similar question out there, please refer to the link bellow:
https://stackoverflow.com/a/7710495/4558361 by t-clausen.dk
The CONVERT(VARCHAR) answers are valid, if you want more flexibility and don't know the conversion codes like me :), you could do it with FORMAT:
DECLARE #d DATETIME = GETDATE();
SELECT FORMAT( #d, 'hh:mm') AS 'Time Result'
If 2012+
Select format(GetDate(),'HH:mm') --For the 24 clock
Select format(GetDate(),'hh:mm tt') --For the 12 clock
That said, I was informed that FORMAT() has a poor performance
Try like this,
12 HOURS FORMAT
SELECT LTRIM(RIGHT(CONVERT(VARCHAR(20), DateOfAddmission, 100), 7)) FROM Baza_test
24 HOURS FORMAT
SELECT CONVERT(VARCHAR(5),DateOfAddmission, 108) FROM Baza_test
Apart from the answer given here another way is this -
SELECT CAST(CAST(GETDATE() AS Time(0)) AS VARCHAR(5))
Output
05:48
And in your case its -
SELECT CAST(CAST(DateOfAddmission AS Time(0)) AS VARCHAR(5)) AS [time]
FROM Baza_test
WHERE ID = 1;

getdate() function [duplicate]

This question already has answers here:
How to return only the Date from a SQL Server DateTime datatype
(46 answers)
Closed 6 years ago.
How can I display only date while using getdate() function?
When I am using
select getdate() as Today's Date
It is giving me:
2016-05-18 08:32:07.100
But I only need:
2016-05-18
How to get this?
Use convert. The first argument truncates the length, and the last specifies the format you want the date in:
SELECT Convert(varchar(10), getdate(), 103) as [Today's Date]
If you are using SQL Server 2012+, you may also want to look at FORMAT - which in my opinion is a bit nicer to read/use.
Example: SELECT FORMAT(GETDATE(), 'dd/MM/yyyy')
See this msdn link for more info