I have a sysdate() value - 2016.10.18. I need a SQL to convert it to int in MS SQL SERVER.
INPUT - 2016.10.18
OUTPUT reqd. - 20161018
I tried this -
SELECT CONVERT(int, '2016.10.18')
but it throws the following error:
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value '2016.10.18' to data type int.
Also tried this -
select CONVERT(int,CONVERT(decimal(19,2),'2016.10.18'))
Getting the following error:
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric.
Could someone help me out!?
Select Convert(int,Convert(varchar,cast('2016.10.18' as date),112))
Returns
20161018
Either of the following will work. If you have a use the replace() method, you will need to strip the time portion off via LEFT(...,10)
Select Convert(int,Convert(varchar,cast('2016.10.18 14:24:49' as date),112))
Select cast(Replace(Left('2016.10.18 14:24:49',10),'.','') as int)
Updated answer.
Select Cast(Convert(varchar(8), Cast(Left('2016.10.18 14:24:49',10) as Date), 112) as int)
Related
I am facing below error while running the query
SELECT (0.01 * CAST(instruct_to_bal_avl_aft AS INT) )
FROM [FullYear].[dbo].[aa_momo_log]
Msg 245, Level 16, State 1, Line 12 Conversion failed when converting the varchar value '3.0' to data type int.
Since you are multiplying by a float value you can cast as DECIMAL instead of INT:
SELECT (0.01 * CAST(instruct_to_bal_avl_aft AS DECIMAL) )
FROM [FullYear].[dbo].[aa_momo_log]
I want to convert the julian date in a date format.
Below, please find an example to create my error. All my dates have 5 digits I need to convert.
create table #test (dateR int)
insert into #test (dateR)
values (39596),(39596),(39595),(39595),(39593),(39592),(39592),(39589),(38104),(38104),(37957)
SELECT * from #test
select *
, dateadd (year, dateR/1000 - 1900, dateR %1000 - 1) as Rd
from #test
Getting the error:
Msg 517, Level 16, State 1, Line 2 Adding a value to a 'datetime'
column caused an overflow.
Also tried:
SELECT DATEADD(dd, CONVERT(int, RIGHT(dateR,3)) - 1, CONVERT(datetime,SUBSTRING(dateR,1,2)+'0101', 212))
This returns:
Msg 8116 level 16 state 1 line 1 nullArgument data type int is invalid for argument of substring function
You can just cast the INT value to a DATETIME.
For example:
SELECT CAST(39596 AS DATETIME); -- Returns 2008-05-30 00:00:00.000
I want to convert from CRSE_EDTE (string) to datetime. Current CRSE_EDTE date format is YYYYMM, so I decided to put 28 as day for every date (DD).
select
try_convert(datetime, [CRSE_EDTE], 112) + CRSE_EDTE + '28' as new_CRSE_EDTE
FROM
[SMBM_DBPELJ].[DBPELJ].[MSTUMASTER]
I get an error:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
I don't have your database to play with, but would something like this work?
select try_convert(datetime,LEFT([CRSE_EDTE], 4)+'-'+RIGHT([CRSE_EDTE],2)+'-28',111) as new_CRSE_EDTE
FROM [SMBM_DBPELJ].[DBPELJ].[MSTUMASTER]
How to convert an int column ,Birthdate( sample value 20090301) to a date column in the format(01/03/2009)
Sql Server 2000
I was trying to convert some thing like below
select * from tabl
where
cast(dob as datetime)>='01/03/2009'
which gives an error
Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type datetime.
SQL Server doesn't allow you to cast int -> datetime directly, but if you're sure you can cast it to nvarchar first and then cast it to date.
For your question here's an example
declare #test int = 20090301
select CONVERT(datetime, CAST(#test as nvarchar), 112)
112 is the format you mentioned, here's the list of all possible formats for Convert function.
try this !
select * from tabl
where
cast(convert(char(8),dob, 112) as date)>='2009-03-01'
I need to select from a table and filter on dates greater than a
specified date.
The problem I'm having is that the dates are stored as nchar(20) and I
can't seem to get it converted to date in the where clause.
SELECT CONVERT(DATETIME,log_time,20) from dbo.logs
where CONVERT(DATETIME,log_time,20) > '10/20/2008'
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting datetime from character string.
This should work.
select convert(datetime, cast(rtrim(log_time), nvarchar)) from dbo.logs
where convert(datetime, cast(rtrim(log_time), nvarchar)) > '10/20/2008'
I talked to our SQL Server DBA. He said the problem was with bad data. He said on some the dates the m was left off of the am or pm. He wrote me a view where he handles the problem for me and now I can select from the view and the date is a datetime data type also.
Thanks for everyone's help.
Billy
try this:
select convert(datetime, cast(trim(log_time), nvarchar)) from dbo.logs
where convert(datetime, cast(trim(log_time), nvarchar)) > '10/20/2008'
I think the trick is that you need to trim the string and cast to nvarchar
Msg 195, Level 15, State 10, Line 1
'trim' is not a recognized built-in function name.
Msg 1035, Level 15, State 10, Line 1
Incorrect syntax near 'cast', expected 'AS'.
Try
select convert(datetime, cast(rtrim(log_time) AS nvarchar)) from dbo.logswhere convert(datetime, cast(rtrim(log_time) AS nvarchar)) > '10/20/2008'
or simply
select CAST(log_time AS DATETIME) from dbo.logs
where CAST(log_time AS DATETIME) > '10/20/2008'