|
|
|
|
|
|
Question of the Day |
Today's question (by Steve Jones - SSC Editor): | |
The Best Query | |
I have a table full of various candies.
CREATE TABLE Candy ( CandyName VARCHAR(100) , CountryName VARCHAR(50) ) GO INSERT dbo.Candy (CandyName, CountryName) VALUES ('Meiji Kinoko no Yama', 'Japan'), ('Shokolad Para Pitzputzim', 'Israel'), ('Guayabitas', 'Costa Rica'), ('Caramello koalas', 'Australia'), ('Marabou Mjolkchoklad', 'Sweden'), ('Ptasie Mleczko', 'Poland'), ('Coffee Crisp', 'Canada'), ('Saft Goldbären', 'Germany'), ('Nestle Damak', 'Turkey'), ('Indy Dedos', 'Mexico'), ('Fazer''s Marianne', 'Finland'), ('Pascal Pineapple Lumps', 'New Zealand'), ('Percy Pig gummies', 'United Kingdom'), ('Salmiakki', 'Denmark'), ('Dorina Riža', 'Croatia'), ('Strawberry Kiss Yupi', 'Indonesia'), ('Regina', 'Portugal'), ('Mozart Kugeln', 'Austria'), ('ION', 'Greece'), ('Kinder Bueno', 'Italy') GOI have another table for pranks. CREATE TABLE Prank (PrankName VARCHAR(200)) GO INSERT dbo.Prank (PrankName) VALUES ('Give out a burger that looks like a cupcake'), ('Gift wrap a rock'), ('Shoot silly string'), ('Play recording of dog attacking the door'), ('Drop balloons from upper window'), ('Give out resume paper and pens'), ('Drop slime into their hand'), ('Turn out all lights') GOI run this code to decide whether to give out a trick or treat to each person that comes to my door. How many people can I serve on Halloween night? SELECT CASE WHEN RAND(CHECKSUM(NEWID())) > .5 THEN c.CandyName ELSE p.PrankName END FROM dbo.Candy AS c CROSS JOIN dbo.Prank AS p; | |
Think you know the answer? Click here, and find out if you are right. |
Yesterday's Question of the Day (by Steve Jones - SSC Editor) |
Funny code I have this code in a script file that someone gave me at work. DECLARE @List TABLE (Number INT); DECLARE @number INTEGER = 1234; DECLARE @strNumber VARCHAR(8); WHILE NOT EXISTS (SELECT * FROM @List WHERE Number = @Number) BEGIN INSERT INTO @List (Number) VALUES (@Number); SET @strNumber = @Number * @Number; SET @strNumber = SUBSTRING(RIGHT('00000000' + @strNumber, 8), 3, 4); SET @number = @strNumber; END; SELECT * FROM @List; What does this produce? Answer: A pseudo random list of numbers between 0 and 9999 Explanation: This produces a pseudo random set of numbers between 0 and 9999. The seed value is 1234, and this can be changed to change the sequence. Ref: Random Thoughts - https://www.sqlservercentral.com/articles/random-thoughts |
Database Pros Who Need Your Help |
Here's a few of the new posts today on the forums. To see more, visit the forums. |
SQL Server 2016 - Administration |
Moving DB file - I have Always ON setup between Server_A and Server_B (Primary server is Server_A and DR server is Server_B) I have DB called ManagementDB which has 3 Data file 2 on D drive and 1 on C drive. I need to moved Data file from C to d Drive. Since it has Always on Setup I […] |
Read-only error in Primary AG replica, but the db is not read-only - Hello experts, I am trying to add db permissions (read/write) on the primary replica of an AOAG setup. For some reason I am getting this error: Failed to update database "MyDB" because the database is read-only. (Microsoft SQL Server, Error: 3906) I checked the MyDB options and Database Read-Only is set to False. Does anyone […] |
identify sql backup strategies - how do I identify if the dbs are getting backed up (full,diff,log) without looking through sqljobs /maintenance plans etc..as they maybe getting backed up from a 3rd party tool. Any tables I can look through |
schedule a job in CMS - Is it possible to schedule a sql job in CMS ? As of now I have a query which captures data from all servers listed in CMS, but its is a manual process. Can I schedule it against multiple servers and capture that data and dump that data into a single server/db/table.(Without using powershell or […] |
SQL Server 2016 - Development and T-SQL |
Trying to get a arithmetic operation from an Variable - Hi, I am not sure if this can be done, but I seem to remember there is some format thing that can be done with quates. Thant would pull out the value so that I can use it in something like this: Declare @dd Varchar(2) = '<' SELECT *FROM SalesWhere [sale_id] + @dd + 2 […] |
Dividing data equally in 4 sessions - Hi All I have a table which contains the transactions done by cards. Below is the table structure and sample data: create table CardTransactions(CardNo int, Transactionid int, Processed bit) Insert into CardTransactions values(1,1001,0) Insert into CardTransactions values(1,1002,0) Insert into CardTransactions values(1,1003,0) Insert into CardTransactions values(1,1004,0) Insert into CardTransactions values(1,1005,0) Insert into CardTransactions values(2,1006,0) Insert into […] |
Unique value constraint among active records - I have a lookup table that implements a system-generated Id for DRI purposes. The lookup table has columns defining the effective and obsolete dates of the record i.e. the period during which a lookup term is valid for business use. Normally I would create a unique index on the lookup term to ensure that it […] |
Development - SQL Server 2014 |
what does it mean for the following SQL Scripts - in a stored procedure, I found two line sql scripts " if @@microsoftversion > 0x07320000 exec sp_MSreplraiserror 20598 " , what does it "@@microsoftversion > 0x07320000" and "exec sp_MSreplraiserror 20598 " ? and when executing this "exec sp_MSreplraiserror 20598 ", what will be done by SQL Server ? thanks so much! |
SQL 2012 - General |
'xp_sqlagent_notify' error on job run - Recently, our corporate office forced us to downgrade some non-prod servers from enterprise edition to dev (don't ask, long story). We did so, but now our QA team is having some problems running jobs on the servers. More specifically, a job set up by a vendor to backup & restore a database from server A […] |
SQL Server 2012 - T-SQL |
leave entitlement and balance - Hi all I need your advise for the below case: I’ve a leave request system that users request leaves through it and it differentiate between working days, weekends & holidays means it counts only the working days I’ve already two tables one for employees data like (name number title dept.) and the other table for […] |
SQL Server 2019 - Administration |
Migration step-by-step from SQL Server 2014 to 2019 - We are going to migrate all our SQL Servers from 2014 to 2019. Where I can find step-by-step guide before, during, and after migration? Besides Database Migration Assistant, I could not find any. We have 80+ databases, BLOB images, using AlwaysOn, replication, SSIS packages, SSRS reports, some 3rd-party tools. We will use backup/restore methodology, but […] |
SQL Server 2019 - Development |
Textfield comparator. - A client would like to be able to recognize similar free-texts in a text field. So based on a field find similar texts in the same column. Result could be something like a percentual equalness. Could anybody point me towards a SQL-server geared solution. Or "search words" for Google for this. The free-texts are recipe […] |
Other methods of getting the true length of UTF8 String? - Something I've been playing around with lately is the UTF8 functionality in 2019, and it dawned on me that getting the "true" length a string in a UTF8 string is actually not as easy as for a "normal" varchar (and nvarchar). Let's take the following table: CREATE TABLE dbo.TestTable (VarcharString varchar(30) COLLATE Latin1_General_100_CI_AS, NvarcharString nvarchar(30) […] |
Integration Services |
SSIS projects connection manager driver - Hi I am starting using the project deployment mode. I am using SQL server 2019 And I see when I add a connection in connection manager for a package, I cannot see the sql server native client driver any more. What connection manager type do you recommend for SQL server connections? Thanks |
Hardware |
Are there any free resources/ebooks to develop database design and modeling ? - I want to develop my skills in database design and modeling. I found out some books like "The data model resource book revised edition volume 1". But they are not free. Are there any books to learn these skills. |
| |
©2019 Redgate Software Ltd, Newnham House, Cambridge Business Park, Cambridge, CB4 0WZ, United Kingdom. All rights reserved. webmaster@sqlservercentral.com |