T-SQL date conversion - sql-server

I have the following date format 92845 which represent hhmmss.
I would like to convert that to a datetime in SQL.
I could use something like this:
SELECT STUFF(STUFF('84936',3,0,'-'),6,0,'-')
but it appears T-SQL is not liking the hour part.
thank you!

Seems like you wrote the code in the afternoon and now it doesn't work in the morning. If you're going to stuff based on absolute positioning, you need to make sure the string is always the same length. One way to do this is by padding the string with a 0 and then taking the right-most 6 characters. Also, you need : not - for H/M separators.
SELECT STUFF(STUFF(RIGHT('0' + '84936', 6),3,0,':'),6,0,':');
Result:
08:49:36
Now it can be converted to a datetime:
SELECT CONVERT(DATETIME, STUFF(STUFF(RIGHT('0' + '84936', 6),3,0,':'),6,0,':'));
Result:
1900-01-01 08:49:36.000

I don't know where 92845 is currently stored. If it's in a variable, then something like this will work:
declare #t varchar(6) = '92845';
declare #fullt char(6) = RIGHT('000000' + #t,6)
select DATEADD(second,
SUBSTRING(#fullt,1,2) * 3600 +
SUBSTRING(#fullt,3,2) * 60 +
SUBSTRING(#fullt,5,2),0)
Result:
1900-01-01 09:28:45.000
If it's in a column of a result set, then you can do similar manipulations using e.g. subqueries.

try this:
declare #t varchar(6) = '134524'
select CASE WHEN len(#t)=5 then convert(datetime,convert(varchar(10),CAST(getdate() as date))+' '+'0'+LEFT(#t,1)+':'+SUBSTRING(#t,2,2)+':'+SUBSTRING(#t,4,2))
else convert(datetime,convert(varchar(10),CAST(getdate() as date))+' '+LEFT(#t,2)+':'+SUBSTRING(#t,3,2)+':'+SUBSTRING(#t,5,2)) end

Related

SQL Server : DATETIME to INT

The date I have is 10.01.2022, the value I want is 10012022 - how can I do that? Thanks
If you have a DateTime value and want to convert it to an int value with that format, you could cast it to varchar first using a format that gets you close, and then get rid of any unwanted symbols, and then convert to int
declare #d date = '20220110'
select convert(int, replace(convert(varchar, #d, 103), '/', ''))
I would suggest something like this...
Select Day(YourColumn) * 1000000 + Month(YourColumn) * 10000 + Year(YourColumn)
From YourTable
It's not clear from your question if you want month-day-year or day-month-year. Regardless, with this code it should be fairly easy to adjust it to meet your situation.
PS. This will be faster than converting to varchar and then to int.

How to get a time difference between two time columns for SSRS

I have two tables I am trying to get a difference from in SQL. Table A has a time in the following format: 07:40:06,
08:33:34,
13:42:09,
Table B is in the same format. I want to return in an actual time for example 7:40:00 in A and 7:50:00 in B result in new column 00:10:00.
Thanks,
I admit that this is sort of cheesy, but appears to work. You could use a constant date, and add the times to that as strings. Then subtract and format however you like. For example: HH:mm:ss. This will give the result of 00:10:00
DECLARE #contstantDate DATETIME = DATEFROMPARTS(1900,1,1)
DECLARE #date1 DATETIME = #contstantDate + '7:40:00'
DECLARE #date2 DATETIME = #contstantDate + '7:50:00'
SELECT FORMAT(#date2-#date1, 'HH:mm:ss')

Trouble casting string to date

I keep getting the following error when running my query and am having trouble finding a solution.
Conversion failed when converting date and/or time from character string.
select * from creditcard
where cast(left(expdate,2) + '/01/' + right(expdate,2) as date) < '08/01/17'
and paycode <> ''
All of the dates in the table look like this '07/17'
declare #myfakedate varchar(25) = '07/17'
declare #mydatestr varchar(25)
Select #mydatestr = stuff(#myfakedate,3,0,'/01')
select #mydatestr
will give you '07/01/17'
If 2012+ user try_convert() just in case you have bogus data. Otherwise, the standard convert() should do
Example
Select *
From YourTable
Where try_convert(date,replace(expdate,'/','/01/'))<'08/01/17'

How to convert date to 104 format without zero in month using T-SQL

For example have query :
declare #date datetime = '2014-06-18'
SELECT CONVERT(varchar,#date,104)
Response is : 18.06.2014 , but I want month without zero, it should be like : 18.6.2014 is it possible to do something like this ?
declare #date datetime = '2014-06-18'
SELECT CAST(DAY(#date) AS VARCHAR(2)) + '.' +
CAST(MONTH(#date) AS VARCHAR(2)) + '.' +
CAST(YEAR(#date) AS VARCHAR(4))
RESULT: 18.6.2014
It feels a little "kludgy", but the only place the sequence .0 can appear in the output date (assuming you're working with dates after 1000 A.D., which you are for datetime which cannot represent dates before 1753) is when there's a leading 0 on the month. So:
declare #date datetime = '2014-06-18'
SELECT REPLACE(CONVERT(varchar,#date,104),'.0','.')
Produces:
18.6.2014
If you are using SQL Server 2012 or above you can use the CONCAT function:
DECLARE #date DATETIME = '2014-06-18'
SELECT
CONCAT(DAY(#date),'.',MONTH(#date),'.',YEAR(#date))
This will return 18.6.2014
Also, if you want to strip the leading zero of the day (say for 2014-06-06), this will take care of that too (which damien_the_unbeliever's solution will not. Not that it is wrong, the behavior is just different so I wanted to make the distinction since it wasn't explicitly stated)
Reference: http://msdn.microsoft.com/en-us/library/hh231515(v=sql.110).aspx

Arithmetic overflow error converting expression to data type datetime. (while displaying date time..)

While executing following error is showing
declare #yr_mnth_dt as numeric;
set #yr_mnth_dt = 20130822;
select convert(datetime,#yr_mnth_dt,112) as YR_MNTH_DT
error shows
Arithmetic overflow error converting expression to data type datetime.
You issue is that you're trying to convert the numeric to a datetime, and this just isn't working.
You need to turn your numeric into a string first:
declare #yr_mnth_dt as numeric;
set #yr_mnth_dt = 20130822;
select yr_mnth_dt = cast(cast(#yr_mnth_dt as char(8)) as datetime);
SQL Fiddle with demo.
When you try and convert a numeric type to a datetime, SQL Server tries to add the numeric value as the number of days to the date 01-Jan-1900. In your case this is trying to add millions of days, and hence the overflow error.
CONVERT works fine, too, if you prefer:
select yr_mnth_dt = convert(datetime, convert(char(8), #yr_mnth_dt));
SQL Fiddle with demo.
I've only seen the conversion used for strings. I can't easily tell whether it's even designed to work with numbers. You could convert the number to a string, then the string to a date. However, I would personally just use DATEFROMPARTS:
SELECT DATEFROMPARTS(#yr_mnth_dt / 10000,
(#yr_mnth_dt / 100) % 100,
#yr_mnth_dt % 100) AS YR_MNTH_DT
Why numeric?
Try this
declare #yr_mnth_dt as varchar(10);
set #yr_mnth_dt = '20130822';
select convert(datetime,#yr_mnth_dt,112) as YR_MNTH_DT

Resources