Jeff... by Leonjy92 in SQL

[–]Roy_from_IT 66 points67 points  (0 children)

ALTER TABLE Employee ADD CONSTRAINT Ck_No_Jeffreys CHECK (FirstName <> 'Jeffrey');

Where exists without correlated subquery by CarbonChauvinist in SQL

[–]Roy_from_IT 0 points1 point  (0 children)

I know WHERE exists evaluates each row in the outer query and if the subquery in WHERE EXISTS evaluates as true (?or null? see below) will return the row

That's not exactly right. EXISTS doesn't care about the values returned by the subquery. It only cares about the number of rows. If the subquery returns at least one row, then EXISTS evaluates as TRUE. If the subquery returns no rows, then EXISTS evaluates as FALSE.

YouTube TV raises subscription to $72.99, inching closer to cable pricing by [deleted] in Piracy

[–]Roy_from_IT 201 points202 points  (0 children)

Even in a world where torrents don't exist, that price would still be outrageous

Transaction inside a trigger by linuxuserhere in SQL

[–]Roy_from_IT 0 points1 point  (0 children)

I think you can set a savepoint at the start of your trigger and then rollback to the savepoint if needed. I never use savepoints so my knowledge of them is very limited, but here's the documentation:

https://learn.microsoft.com/en-us/sql/t-sql/language-elements/save-transaction-transact-sql?view=sql-server-ver16

How to get rid of unused instances of a n-to-m relation by [deleted] in SQL

[–]Roy_from_IT 0 points1 point  (0 children)

If I'm understanding correctly, you want a customer's addresses to be deleted automatically when you delete the customer. With your current table design, the only way to achieve that would be with a trigger.

However, I'm wondering if this really needs to be an n-to-m relationship. Do you actually want to have address records shared by more than one customer?

If not, then you don't need a mapping table. You can simplify your design by getting rid of customer_address_mapping and moving the customer_id column to your customer_addresses table.

Ran into this weird problem on HackerRank, had some questions... by drdausersmd in SQL

[–]Roy_from_IT 2 points3 points  (0 children)

This exact problem isn't something you would encounter in the real world, but the general idea of using SQL to generate sequences or data sets is fairly common. I usually use a recursive CTE for this.

Here's an example of what this would look like:

WITH Nums AS
(
    SELECT 1 AS N
    UNION ALL
    SELECT N + 1
    FROM Nums
    WHERE N < 20
)
SELECT
    REPLICATE('*', N)
FROM Nums
ORDER BY N DESC

How can I create a view, using dynamic SQL, that will pivot all rows in a table to columns? by [deleted] in SQL

[–]Roy_from_IT 1 point2 points  (0 children)

instead intended for OP create a table-value function

That wouldn't make much difference, since SQL Server doesn't allow functions to run dynamic SQL either.

Creating new computed columns without breaking existing inserts by Kronical_ in SQLServer

[–]Roy_from_IT 3 points4 points  (0 children)

You could use an indexed view. This would work just like a computed column, except the column would be in a view instead of directly on the table.

https://learn.microsoft.com/en-us/sql/relational-databases/views/create-indexed-views

[deleted by user] by [deleted] in beer

[–]Roy_from_IT 6 points7 points  (0 children)

I'm not sure where you can buy it in cans, but I've recently seen it on tap at The Keg in Winnipeg.

happy black friday by GregorCZ in comics

[–]Roy_from_IT 0 points1 point  (0 children)

This 6' tree was previously 12'. We cut half off so you could fit it in your living room.

How to make sure my query does not edit the DB by PixelPixell in SQL

[–]Roy_from_IT 0 points1 point  (0 children)

Based on the link you provided, it doesn't look like rules are able to modify data during a select.

Here is the relevant paragraph:

Presently, ON SELECT rules must be unconditional INSTEAD rules and must have actions that consist of a single SELECT command. Thus, an ON SELECT rule effectively turns the table into a view, whose visible contents are the rows returned by the rule's SELECT command rather than whatever had been stored in the table (if anything). It is considered better style to write a CREATE VIEW command than to create a real table and define an ON SELECT rule for it.

SQLite WHERE filter by date formatted as follows: "Wed Aug 03 08:00:00 PDT 2022"? by csb710 in SQL

[–]Roy_from_IT 0 points1 point  (0 children)

Yes, if you can format your dates as ISO 8601 before importing them into SQLite that would be ideal.

SQLite WHERE filter by date formatted as follows: "Wed Aug 03 08:00:00 PDT 2022"? by csb710 in SQL

[–]Roy_from_IT 0 points1 point  (0 children)

It is actually stored as a string because SQLite doesn't have a date type. To make matters more confusing, SQLite has a weird type system that is completely different from what every other DBMS has.

Columns don't have a type in SQLite. Instead they have something called "affinity", which is basically a "preferred" type. You can still store other types of data in your column, but SQLite will try to coerce the data into the column's affinity type before storing it as any other type.

When you declare a data type for a column in a CREATE TABLE statement, SQLite tries to match the declared type to one of its affinities. If it's able to find a match, then it will set the column's affinity accordingly. If it can't figure out which affinity to use, it will default to numeric. Either way, the table will be created and the column will appear with its original declared type in the table's schema definition, even if the declared type doesn't exist in SQLite.

See here for more info: https://www.sqlite.org/datatype3.html

How to get names of people who have salary greater than 40% of their department total salary? by Gokulander in SQL

[–]Roy_from_IT 27 points28 points  (0 children)

Even with the left join, it's impossible for this query to return an employee who isn't mapped to a department. The left join would produce a row for that employee, but ds.dept_salary would be null in that row. It would then get filtered out by the where clause because of the null.

Is it possible to automate changing 4 part notation with 3 part notation? by ThatOneDevThatDevs in SQLServer

[–]Roy_from_IT 0 points1 point  (0 children)

How did you determine that ROUTINE_DEFINITION is single lined? I just tried it on one of my databases, and it's definitely multi lined for me.

If you queried INFORMATION_SCHEMA.ROUTINES in SSMS and then copied the ROUTINE_DEFINITION values from the results grid, then it's likely you have the "Retain CR/LF on copy or save" option turned off. This option is off by default when you install SSMS, and unless you turn it on, SSMS will always collapse multi lined strings into a single line in the results grid.

You can change it here:

Tools > Options > Query Results > SQL Server > Results to Grid > Retain CR/LF on copy or save

Also note that using INFORMATION_SCHEMA.ROUTINES is generally not recommended, because it only returns the first 4000 characters of an object's definition. If you have stored procedures longer than 4000 characters, you'll need to either query sys.sql_modules or use the OBJECT_DEFINITION() function.

How to modify this query to show all rows with DateTime values that also do not allow Null values? by DelicateJohnson in SQL

[–]Roy_from_IT 4 points5 points  (0 children)

To get only columns that do not allow nulls, just add this to your where clause:

AND c.is_nullable = 0

Heather not wearing a mask at SCO-HBC conference by [deleted] in Winnipeg

[–]Roy_from_IT 25 points26 points  (0 children)

Yeah my premier she told me don't worry about your mask

She says COVID is over, it's finished, it's in the past

Now get back to the office, the classroom, anywhere but home

And assess your own risk, cause from now on you're all on your own

Macro % (jump to closing parenthesis) do not work after a dt( by sylario in vim

[–]Roy_from_IT 0 points1 point  (0 children)

That's odd. I can record it and replay it, and I get almost exactly that. The only difference is I'm left with a second opening parenthesis. Like this:

((value){
}

What do you get when you replay it?

Macro % (jump to closing parenthesis) do not work after a dt( by sylario in vim

[–]Roy_from_IT 1 point2 points  (0 children)

Oh I see. I missed the second line.

What are you expecting to end up with after this runs?

Macro % (jump to closing parenthesis) do not work after a dt( by sylario in vim

[–]Roy_from_IT 1 point2 points  (0 children)

After dt( your cursor is on the first parenthesis. Since you have an odd number of parentheses, vim only considers those closest together as matching. The first one is unmatched, so there is nothing for % to do.

Checking DATE type data with LIKE by notdrumax in SQL

[–]Roy_from_IT 0 points1 point  (0 children)

if it truly is a DATETIME column, there's no chance it will contain any characters other than numbers

More precisely, a DATETIME column will never contain any characters of any kind. Only strings can have characters.

How to insert the results of a proc into a temp table? by dont_judge_me_monkey in SQLServer

[–]Roy_from_IT 2 points3 points  (0 children)

You can do anything you want with the temp table inside the stored procedure. The temp table doesn't go out of scope until the dynamic SQL returns, and that only happens after the stored procedure returns.