Casting to Time in SQL fails after 1762 rows - sql

(Updated followings comments)
I got a strange behaviour on SQL server 2012, this request is ok :
select top 2002 cast(myTimeStr as time) from tbWithTime
order by ID
but
select top 2003 cast(myTimeStr as time) from tbWithTime
order by ID
failed with
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
Whereas line 2002 and 2003 both equals to '14:30'
Then when i try to specifically cast this 2003 line, it succeeded.
My configuration :
Name Version
Microsoft SQL Server Management Studio 10.50.1600.1
Microsft SQL Server 2012 11.0.3339.0

To find the actual row(s) causing you issues, try this:
SELECT * FROM tbWithTime WHERE TRY_CONVERT(time,myTimeStr) IS NULL
TRY_CONVERT:
Returns a value cast to the specified data type if the cast succeeds; otherwise, returns null.

You could try this to find the funny rows:
select myTimeStr
from tbWithTime
where myTimeStr not like '[0-1][0-9][:][0-5][0-9]'
and myTimeStr not like '2[0-3][:][0-5][0-9]'
Or you could just select the valid rows:
select cast(myTimeStr as time)
from tbWithTime
where myTimeStr like '[0-1][0-9][:][0-5][0-9]'
or myTimeStr like '2[0-3][:][0-5][0-9]'

Related

What causes error "Strings cannot be added or subtracted in dialect 3"

I have the query:
WITH STAN_IND
AS (
SELECT ro.kod_stanow, ro.ind_wyrob||' - '||ro.LP_OPER INDEKS_OPERACJA, count(*) ILE_POWT
FROM M_REJ_OPERACJI ro
JOIN M_TABST st ON st.SYMBOL = ro.kod_stanow
WHERE (st.KOD_GRST starting with 'F' or (st.KOD_GRST starting with 'T') ) AND ro.DATA_WYKON>'NOW'-100
GROUP BY 1,2)
SELECT S.kod_stanow, count(*) ILE_INDEKS, SUM(ILE_POWT-1) POWTORZEN
from STAN_IND S
GROUP BY S.kod_stanow
ORDER BY ILE_INDEKS
That should be working, but I get an error:
SQL Error [335544606] [42000]: Dynamic SQL Error; expression evaluation not supported; Strings cannot be added or subtracted in dialect 3 [SQLState:42000, ISC error code:335544606]
I tried to cast it into bigger varchar but still no success. What is wrong here? Database is a Firebird 2.1
Your problem is 'NOW'-100. The literal 'NOW' is not a date/timestamp by itself, but a CHAR(3) literal. Only when compared to (or assigned to) a date or timestamp column will it be converted, and here the subtraction happens before that point. And the subtraction fails, because subtraction from a string literal is not defined.
Use CAST('NOW' as TIMESTAMP) - 100 or CURRENT_TIMESTAMP - 100 (or cast to DATE or use CURRENT_DATE if the column DATA_WYKON is a DATE).

Float to varchar with style 3 is failing in sql server 2016

I am getting Arithmetic overflow error while executing following query on sql server 2016.
"select convert(varchar(20), cast('0' as float), 3)"
The same query works fine on sql server 2014.
you are getting this error cause casting '0' to float of style 3 returning 23 charter value which is unable to convert to varchar of length 20 only.
try something like,
select convert(varchar(23), cast('0' as float), 3)
Or
select convert(varchar(max), cast('0' as float), 3)
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
The same query works fine
on sql server 2014.
style in sql 2014
Other values are processed as 0.
A maximum of 6 digits. Use in scientific notation, when appropriate.
style in sql 2016
3 Always 17 digits. Use for lossless conversion. With this style, every distinct float or real value is guaranteed to convert to a
distinct character string.Applies to: Azure SQL Database, and starting in SQL Server 2016.
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql

SQL Server gives syntax error

I have following SQL for Microsoft SQL Server
SELECT *
FROM tblA
WHERE CREATION_DATE BETWEEN DATE'2015-09-12' AND DATE'2015-09-15'
But this throws a syntax error:
Error: Incorrect syntax near '2015-09-12'. SQLState: S0001 ErrorCode:
102
What is wrong? I want to use ANSI literal code.
This is not how you cast in MS SQL Server. Instead, you should try the cast syntax:
SELECT *
FROM tblA
WHERE creation_date BETWEEN CAST('2015-09-12' AS DATE) AND
CAST('2015-09-15' AS DATE)
You can simply use this query, without adding date:
SELECT *
FROM tblA
WHERE CREATION_DATE BETWEEN '2015-09-12' AND '2015-09-15'
Date literals are tricky... besides the problem with conversion (often depending on your system's culture) you must think of the day's end if you use between!
Here you find a system independent way to literally write dates: https://msdn.microsoft.com/en-us/library/ms187819.aspx?f=255&MSPPError=-2147217396 (look at "ODBC)
So you could write
BETWEEN {d'2015-09-12'} AND {ts'2015-09-12 23:59:59'}
SELECT *
FROM tblA
WHERE CREATION_DATE BETWEEN CAST('2015-09-12' AS DATE)
AND CAST('2015-09-15' AS DATE)

Type TIME is not a defined system type

We are using Microsoft Server 2012. I need to sum some variables from each day between given time gap.
Here is simplified query:
SELECT SUM(SomeVariable)
FROM MyTable
WHERE CAST([wrkActionDate] as TIME) BETWEEN '19:00:00' AND '07:00:00'
And i get error:
Type TIME is not a defined system type.
I guess data type doesn't exist in "Microsoft Server 2012" if so how to write query which would do what i want to do?
P.S: Sorry if I made some writing errors. I don't compute English :)
FROM and WHERE should be swapped.
The cast to time works - just tried it with SQL Server 2012 - but the range is invalid (starting value greater than the end one) - you should do something like this:
SELECT SUM(SomeVariable)
FROM MyTable
WHERE
CAST([wkActionDate] AS TIME) BETWEEN '19:00:00' AND '23:59:59' OR
CAST([wkActionDate] AS TIME) BETWEEN '00:00:00' AND '07:00:00'
You can also use DATEPART:
SELECT SUM(SomeVariable)
FROM MyTable
WHERE
DATEPART(hour,[wrkActionDate]) BETWEEN 19 AND 24 OR
DATEPART(hour,[wrkActionDate]) BETWEEN 0 AND 7

decimal to hex conversion in sql server 2008

the hex value of 2716455883 is A1E9D3CB but using
SELECT CONVERT(VARBINARY(8), 2716455883)
getting answer 0x0A000001CBD3E9A1
SELECT CONVERT(VARBINARY(8), cast(2716455883 as bigint))
It is due to the way SQL Server interprets literals without qualified types. Check this out
select sql_variant_property(2716455883, 'basetype'); -- numeric
select sql_variant_property(2716455883, 'precision'); -- 10
select sql_variant_property(2716455883, 'scale'); -- 0