4

I have got a problem while running the code Access Query directly in sql server which contains VAL([CoLUMN_NAME]). After googling alot I found a CAST(COLUMN AS DATATYPE) to replace VAL().

But When that column is Text type and if we write VAL([COLUMN_NAME]) we are getting only the number part from that text. For Example the column has this value 45-A45 and used VAL([COLUMN_NAME]) we will get only "4545".

If we want to achive this in SQL SERVER how to do ?

1 Answer 1

9

Val is just the numbers to the left of a string, so you could start with something very roughly like:

SELECT CASE 
         WHEN Patindex('%[^0-9]%', table_1.atext) > 0 THEN Cast( 
         LEFT(table_1.atext, Patindex('%[^0-9]%', table_1.atext) - 1) AS INT) 
         ELSE 0 
       END AS Val 
FROM   table_1; 

This would return 45 instead of "45 dozen", but needs to be much improved to return 45 instead of "45".

Edit re comments

@Andriy M's solution (below) works exactly like Val, as far as I can tell.

  SELECT CAST(LEFT(table_1.atext, Patindex('%[^0-9]%', table_1.atext + 'x') - 1) 
  AS INT) AS Val from table_1 

Edit #2

Table from MS Access:

AText   Val(Atext)  
45 dozen    45
ABC 45      0
45_ABC      45
45 ABC 34   45
45          45
ABC         0

Using @Andriy M's solution you get exactly the same result.

I have since found that @GMastros has a better solution

CAST(LEFT(atext, Patindex('%[^-.0-9]%', atext + 'x') - 1) AS Float) AS Val

Note that I changed the search pattern to include the negative sign and a decimal separator. I also changed the data type for the cast to a float. Val returns a vb6 double, which is the same as a SQL Server float. -- G. Mastros

6
  • 1
    Not much, perhaps. ELSE CAST(table_1.atext AS int) might do. On the other hand, something like CAST(LEFT(table_1.atext, Patindex('%[^0-9]%', table_1.atext + 'x') - 1) AS INT) AS Val (i.e. without CASE at all) might do as well.
    – Andriy M
    Jan 23, 2013 at 11:31
  • @AndriyM Great idea! And that would work with a purely character value as well, would it not?
    – Fionnuala
    Jan 23, 2013 at 11:42
  • I believe it would. In the sense that it would simply evaluate to 0. I'm not sure, though, if either your version or mine is an exact equivalent of the VAL() described by the OP. I mean, their example, '45-A45' -> 4545, seems to suggest different logic. Still, I find it more confusing. For instance, it is not clear whether . should be interpreted as part of a number as well, and if so, how the function would interpret e.g. '12A.B.34'.
    – Andriy M
    Jan 23, 2013 at 12:10
  • 1
    No, '45-A45' -> 4545 is not what Val does. ?val("45-A45") = 45. I believe they have expressed the question badly.
    – Fionnuala
    Jan 23, 2013 at 12:34
  • Ah, I see, it's all right then, thanks. (I don't have MS Access, so I couldn't verify.)
    – Andriy M
    Jan 23, 2013 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.