Solved

help with sql

Posted on 2013-06-11
4
150 Views
Last Modified: 2013-06-27
I have a table where some of the columns have the value 'NULL' instead of  null values so my query below fails and I need a way to fix the query, I have limited control on data:

select * from temp where

FY1= (YEAR(DATEADD(month, 6 + DATEDIFF(month, 0, getdate()), 0)))

and tempid=3145

here the 3145 has the FY1 value as 'NUll' where as others have the value either like 2013 or null and the column is varchar(4)

all I am doing is bring in data which matched this fiscal year

and with the above query I am getting the error :

Conversion failed when converting the varchar value 'NULL' to data type int.

where as the same works for null values
0
Comment
Question by:welcome 123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 200 total points
ID: 39239059
Well,

You could add
and FY1<>'NULL'

Open in new window

In your where clause.

Giannis
0
 
LVL 4

Expert Comment

by:BAKADY
ID: 39239063
select * from temp where case FY1 when NULL then 0 else case when FY1= (YEAR(DATEADD(month, 6 + DATEDIFF(month, 0, getdate()), 0))) then 1 else 0 end end = 1

Open in new window

0
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 39239068
Or even change your where to

CASE WHEN FY1 = 'null' THEN NULL ELSE FY1 END = (YEAR(DATEADD(month, 6 + DATEDIFF(month, 0, getdate()), 0)))

Open in new window


Giannis
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39239933
I'd strongly recommend avoiding the implied conversion of the data to int by converting the single date calculation to varchar
select
*
from temp
where FY1= convert(varchar,(YEAR(DATEADD(month, 6 + DATEDIFF(month, 0, getdate()), 0))))
or FY1 is null
or FY1 = 'NULL'

Open in new window

That solves the conversion issue, but what do you do with (true) nulls and the string 'NULL'?? i.e. how do you know what fiscal year those records belong to? If you are simply going to ignore those, just remove the last 2 lines above.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question