DELETING NULL ERROR MSG APPEARING - sql

I am trying to delete away all the rows with NULL. I tried using the code:
DELETE FROM [FILENAME] WHERE [MONTH] = 'NULL'
But the error msg appears as:
Msg 241, Level 16, State 1, Line 1 Conversion failed when converting
date and/or time from character string.
I'm guessing i have to convert something but got no clue. Any help will be great. thanks

The error is because (NULL) is not a value and not a string value to put it between two single quotation marks('').
NULLs have special ways and special functions to deal with it, so you can use this code to delete away all the rows with NULL
DELETE FROM [FILENAME] WHERE [MONTH] IS NULL

Related

how to fix SQL server Json text is not properly formatted. Unexpected character is found at position 151

Im working on a table that has a column in JSON format and I want to extact the coordinate value from that column.
So I run the following code:
Select *,JSON_VALUE(field,'$."Coordinate-X"[0]') As coordinate INTO TABLE_1 FROM table
WHERE JSON_VALUE(field,'$."Coordinate-X"[0]') IS NOT NULL
And I get the following error after 5 mins of running
Msg 13609, Level 16, State 1, Line 27
Json text is not properly formatted. Unexpected character '"' is found at position 151.
When I try to check some rows with top 200 * below code returns results withour any error
Select TOP 200 *,JSON_QUERY(field,'$."Coordinate-X"[0]') As coordinate FROM table
WHERE JSON_VALUE(field,'$."Coordinate-X"[0]') IS NOT NULL
I guess I have a row that is causing the error but I dont know how to identify it or exclude that row and return the results.
I managed to run around the problem with this where statement in case someone has same problem I hope it helps!
Select *,JSON_QUERY(field,'$."Coordinate-X"[0]') As coordinate FROM table
WHERE field like '%Coordinate-X%'
and ISJSON(field)=1
seems like you have bad data( in your case doublequote) in your JSON field.
your TOP 200 runs successfully because that bad character is not showing on TOP 200..
I suggest if you don't have a check constraint on that column add one to avoid bad
ALTER TABLE tablename ADD CONSTRAINT JSON_CHECK CHECK (ISJSON(field) = 1 )
character in your JSON data:
.,to add double quote as part of values in your JSON it shuld look like this : "JSON Value with \" in the value"
for example [{"Id":3,"Title":"JSON Value with \" in the value"}]

Conversion failed when converting from a character string to uniqueidentifier in TSQL

I am having an issue with my T-SQL query here:
UPDATE a
SET a.application =
SUBSTRING(b.postdata,
NULLIF(
PATINDEX('%[A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9]-[A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9]-%',b.postdata),0),36)
FROM [Lionel1].[dbo].[CSL_Logging_STAGE] a
INNER JOIN [Lionel1].[dbo].[CSL_Logging_STAGE] b ON b.Id = a.Id
WHERE
a.application LIKE '%000%'
AND a.postdata != ''
AND a.postdata IS NOT NULL
AND a.http_method = 'post';
I have tried a handful of different castings for the GUID that is stored in the application column and I have also restructured the query to have a subquery but that makes no difference.
The error is
Msg 8169, Level 16, State 2, Line 3
Conversion failed when converting from a character string to uniqueidentifier.
Anyone point me in the right direction?
Some of the VALUES I am trying to set to application are
NULL
0dab3646-a4ed-4b63-9441-11e27fa36df8
f6a5c360-635a-48e0-bf71-76ef0b865441
It looks like I needed to do a better job matching the appkey. It was grabbing extra data on a few of the compares adding quotes and extra characters which are not valid in a GUID.

Issues when using SUBSTRING to trim the first 6 characters in SQL Server

I have a table named Articles, which suprisingly has a column named Article, which is the Primary Key.
In this column all articles used to be designated with an unnecessary prefix which I am tasked to remove.
Examples:
1-184-W21TK00032
1-154-MXA0074
In these examples both 184 and 154 designate distributors, they all have a few thousand Articles in this table.
I encountered zero problems when running this query:
UPDATE Articles
SET Article = SUBSTRING(Article,7,LEN(Article)-6)
WHERE SUBSTRING(Article,3,3) = '184'
I was unable to run this query without a WHERE designation. I altered the final line to switch to the next distributor: 154
WHERE SUBSTRING(Article,3,3) = '154'
And ran into this error:
Msg 536, Level 16, State 5, Line 1
Invalid length parameter passed to the SUBSTRING function.
I have also tried using LIKE in the WHERE line, to designate everything using '-154-' like so
WHERE Article LIKE '_-154-%'
but this would inexplicably lead to this, as would running the query without a WHERE as I started out trying:
Msg 2627, Level 14, State 1, Line 1
Violation of PRIMARY KEY constraint 'aaaaaArticles_PK'. Cannot insert duplicate key in object 'dbo.Articles'.
Any ideas or suggestions? I'm at a loss. Running it without a WHERE line is now impossible for the thousands of products using 184 have succesfully been altered.
The problem occurs because LEN(Article) is less than 6 for at least 1 row which means you are passing a negative value into the SUBSTRING. Filter them out using WHERE LEN(Article) > 6
It's probably not the WHERE you're changing that is the issue.
There are probably rows for "154" where the other SUBSTRING fails.
That is, there are probably (Len(Article) - 6) < 0.
Search for that to confirm and then add a CASE or such to manage it.
Try:
RIGHT(article,LEN(article)-6)
Instead of Substring, this will select everything to the right of the first 6 characters.

SQL Case statement error Msg 8114

Trying to build a simple case statement using SQL Server 2008 and running across an issue giving me the following error message:
Msg 8114, Level 16, State 5, Line 6
Error converting data type varchar to numeric.
I've isolated the script causing the issue to the following:
CASE WHEN tPersonLocationHist.LocationCode = 'DC'
AND (tPersonJobHist.FullTimeEquivalent = 1)
AND (MONTH(tPersonStatusHist.LatestHireDate) < 10)
THEN tPersonStatusHist.NormalHoursPerWeek / 5
The tPersonStatusHist.NormalHoursPerWeek is formatted as decimal; however, I can't get it to calculate.
Any tips? The resulting calculation needs to be in decimal form (to two decimal digits).
Even if I change the THEN statement to just '7.5', it then returns:
Msg 245, Level 16, State 1, Line 6
Conversion failed when converting the varchar value '7.5' to data type int.
I've tried using CONVERT(decimal(4,2),tPersonJobHist.NormalHoursPerWeek * 7.5), but no luck on that either.
Only thing that's working is to do it as:
CONVERT(int,tPersonJobHist.NormalHoursPerWeek * 7.5), but then it is dropping the decimals and just giving the whole integer.
As you can probably tell, I'm new into SQL and still learning the rope, so any help you can give is much appreciated.
Are your other cases/else returning varchar? They should all return the same datatype. You may want to cast/convert them to be sure.
CAST(CONVERT(decimal(4,2),tPersonJobHist.NormalHoursPerWeek * 7.5) AS varchar(50))
First convert to decimal then do the division:
THEN CONVERT(decimal(4,2), tPersonStatusHist.NormalHoursPerWeek) / 5
Or cast is an option:
THEN CAST(tPersonStatusHist.NormalHoursPerWeek AS decimal(4,2)) / 5

From varchar(36) to UNIQUEIDENTIFIER

I am trying to cast an AuctionId that is a UNIQUEIDENTIFIER to an varchar(36) and then back to an UNIQUEIDENTIFIER. Please help me.
CAST((SUBSTRING(CAST([AuctionId] as VARCHAR(36)), 0, 35) + '1') AS UNIQUEIDENTIFIER)
But I keep getting this error:
Msg 8169, Level 16, State 2, Line 647
Conversion failed when converting from
a character string to
uniqueidentifier.
Thanks in advance
The '1' is not the problem. You are obviously trying to change the last character of the GUID to a 1. I don't know why, but that's your requirement.
Your issue is with substring. In TSQL the substring uses an index starting at 1 not 0 like in C or C#. This means your substring statement is actually returning a 34 character string (+1 more character makes 35, and you're being told a 35 character string is not a GUID, which is right).
Just change the ,0,35 to 1,35
Your error is due to your +'1' and your SUBSTRING. What do you have that in there for?
This will work fine
SELECT cast((cast(NEWID() as varchar(36))) as UNIQUEIDENTIFIER)
EDIT: Ok, so if you want to replace the last char with a '1' then this is the solution
SELECT CAST(SUBSTRING(CAST(NEWID() AS VARCHAR(36)), 1, 35) + '1' AS UNIQUEIDENTIFIER)
The only difference is that SUBSTRING in SQL starts at position 1, not position 0 as you had it.
P.S. This is dangerous code. The output is no longer a GUID as it will not conform to the algorithm that was used to generate the GUID. This could (although unlikely) result in a collision with GUIDs which could potentially cause all manner of problems.
As others have observed, it's not clear why you want to do what you're doing.
An alternative to SUBSTRING is the STUFF command:
SELECT stuff(cast([AuctionId] as varchar(36)),36,1,'1')